Compare commits
21 commits
update_doc
...
master
Author | SHA1 | Date | |
---|---|---|---|
07f89712d9 | |||
fd09f389f7 | |||
b6f7ac8ec5 | |||
f598864d4d | |||
44c4158602 | |||
aa751bbbc1 | |||
9e5f59a2b2 | |||
7fcdcbf728 | |||
60dcc5e955 | |||
c3605b2db1 | |||
b455a6c8e7 | |||
5e8935ce66 | |||
d9c0673445 | |||
0c004085fd | |||
184030ebd5 | |||
cbd24e85f9 | |||
5eb81e87be | |||
d4b5367c27 | |||
e5d5059c30 | |||
5d2ee61b49 | |||
3805ec366f |
|
@ -9,4 +9,4 @@ security:
|
|||
security: false
|
||||
|
||||
default:
|
||||
anonymous: ~
|
||||
anonymous: true
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
},
|
||||
"require": {
|
||||
"php": ">=7.1.0",
|
||||
"ext-json": "*",
|
||||
"symfony/symfony": "^3.4",
|
||||
"doctrine/orm": "^2.5",
|
||||
"doctrine/annotations": "^1.3.0",
|
||||
|
@ -57,10 +58,7 @@
|
|||
]
|
||||
},
|
||||
"config": {
|
||||
"bin-dir": "bin",
|
||||
"platform": {
|
||||
"php": "7.1.23"
|
||||
}
|
||||
"bin-dir": "bin"
|
||||
},
|
||||
"extra": {
|
||||
"symfony-app-dir": "app",
|
||||
|
|
1802
composer.lock
generated
1802
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -19,6 +19,17 @@
|
|||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<listeners>
|
||||
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
|
||||
<arguments>
|
||||
<array>
|
||||
<!-- set this option to 0 to disable the DebugClassLoader integration -->
|
||||
<element key="debug-class-loader"><integer>0</integer></element>
|
||||
</array>
|
||||
</arguments>
|
||||
</listener>
|
||||
</listeners>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>src</directory>
|
||||
|
|
|
@ -228,7 +228,7 @@ class UpdateSubscriptionsCommand extends Command
|
|||
);
|
||||
|
||||
/** @var Subscription $subscription */
|
||||
foreach ((array) $serviceUser->getSubscribers() as $subscription) {
|
||||
foreach ($serviceUser->getSubscribers() as $subscription) {
|
||||
$usersForUpdate[] = $subscription->getSubscriber();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,19 @@ class PostController extends AbstractController
|
|||
{
|
||||
/**
|
||||
* @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post")
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function showAction(Post $post, PostRepository $postRepository): Response
|
||||
{
|
||||
if ((!$post->getAuthor()->isPublic()) || $post->getAuthor()->isWhitelistOnly()) {
|
||||
/**
|
||||
* Throwing 404 instead of 403 because of
|
||||
* @see \Symfony\Component\Security\Http\Firewall\ExceptionListener::handleAccessDeniedException()
|
||||
* starts to replace 403 by 401 exceptions for anonymous users and tries to authenticate them.
|
||||
*/
|
||||
throw $this->createNotFoundException('Author\'s blog is private.');
|
||||
//throw $this->createAccessDeniedException('Author\'s blog is private.');
|
||||
}
|
||||
|
||||
return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [
|
||||
'post' => $postRepository->getPostWithComments($post->getId()),
|
||||
]);
|
||||
|
|
|
@ -2,33 +2,64 @@
|
|||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
|
||||
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
||||
use Doctrine\Common\DataFixtures\{AbstractFixture, OrderedFixtureInterface};
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\{Blogs\Post, User};
|
||||
|
||||
class LoadPostData extends AbstractFixture implements OrderedFixtureInterface
|
||||
{
|
||||
public const POST_ID_LONG = 'longpost';
|
||||
public const POST_ID_SHORT = 'shortpost';
|
||||
public const POST_ID_PR_USER = 'prusrpst';
|
||||
public const POST_ID_WL_USER = 'wlusrpst';
|
||||
public const POST_ID_PR_WL_USER = 'prwlusrpst';
|
||||
|
||||
public function load(ObjectManager $om)
|
||||
{
|
||||
/** @var User $testUser */
|
||||
$testUser = $this->getReference('test_user_99999');
|
||||
/** @var User $mainUser */
|
||||
$mainUser = $this->getReference('test_user_'.LoadUserData::USER_MAIN_ID);
|
||||
/** @var User $privateUser */
|
||||
$privateUser = $this->getReference('test_user_'.LoadUserData::USER_PRIV_ID);
|
||||
/** @var User $wlUser */
|
||||
$wlUser = $this->getReference('test_user_'.LoadUserData::USER_WLON_ID);
|
||||
/** @var User $prWlUser */
|
||||
$prWlUser = $this->getReference('test_user_'.LoadUserData::USER_PRWL_ID);
|
||||
|
||||
$longPost = (new Post('longpost', $testUser, new \DateTime(), Post::TYPE_POST))
|
||||
$longPost = (new Post(self::POST_ID_LONG, $mainUser, new \DateTime(), Post::TYPE_POST))
|
||||
->setText('Test post with many comments')
|
||||
->setPrivate(false)
|
||||
->setDeleted(false)
|
||||
;
|
||||
|
||||
$shortPost = (new Post('shortpost', $testUser, new \DateTime(), Post::TYPE_POST))
|
||||
$shortPost = (new Post(self::POST_ID_SHORT, $mainUser, new \DateTime(), Post::TYPE_POST))
|
||||
->setText('Test short post')
|
||||
->setPrivate(false)
|
||||
->setDeleted(false)
|
||||
;
|
||||
|
||||
$privateUserPost = (new Post(self::POST_ID_PR_USER, $privateUser, new \DateTime(), Post::TYPE_POST))
|
||||
->setText('Post from private user. Should not be visible in the public feed.')
|
||||
->setPrivate(false)
|
||||
->setDeleted(false)
|
||||
;
|
||||
|
||||
$wlUserPost = (new Post(self::POST_ID_WL_USER, $wlUser, new \DateTime(), Post::TYPE_POST))
|
||||
->setText('Post from whitelist-only user. Should only be visible for whitelisted users.')
|
||||
->setPrivate(false)
|
||||
->setDeleted(false)
|
||||
;
|
||||
|
||||
$privateWlUserPost = (new Post(self::POST_ID_PR_WL_USER, $prWlUser, new \DateTime(), Post::TYPE_POST))
|
||||
->setText('Post from private AND whitelist-only user. Should not be visible in the public feed.')
|
||||
->setPrivate(false)
|
||||
->setDeleted(false)
|
||||
;
|
||||
|
||||
$om->persist($longPost);
|
||||
$om->persist($shortPost);
|
||||
$om->persist($privateUserPost);
|
||||
$om->persist($wlUserPost);
|
||||
$om->persist($privateWlUserPost);
|
||||
$om->flush();
|
||||
|
||||
$this->addReference('test_post_longpost', $longPost);
|
||||
|
|
|
@ -9,25 +9,27 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
|||
|
||||
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
|
||||
{
|
||||
public const USER_MAIN_ID = 99999;
|
||||
public const USER_SCND_ID = 99998;
|
||||
public const USER_PRIV_ID = 99997;
|
||||
public const USER_WLON_ID = 99996;
|
||||
public const USER_PRWL_ID = 99995;
|
||||
public const USER_UNNM_ID = 99994;
|
||||
|
||||
private $users = [
|
||||
// 99999
|
||||
['login' => 'testuser', 'name' => 'Test User 1'],
|
||||
// 99998
|
||||
['login' => 'testuser2', 'name' => 'Test User 2'],
|
||||
// 99997
|
||||
['login' => 'testuser3', 'name' => 'Test User 3'],
|
||||
// 99996
|
||||
['login' => 'testuser4', 'name' => 'Test User 4'],
|
||||
//99995
|
||||
['login' => 'testuser5', 'name' => null],
|
||||
['id' => self::USER_MAIN_ID, 'login' => 'testuser', 'name' => 'Test User 1', 'private' => false, 'whitelist-only' => false],
|
||||
['id' => self::USER_SCND_ID, 'login' => 'testuser2', 'name' => 'Test User 2 for autocomplete test', 'private' => false, 'whitelist-only' => false],
|
||||
['id' => self::USER_PRIV_ID, 'login' => 'private_user', 'name' => 'Test User 3', 'private' => true, 'whitelist-only' => false],
|
||||
['id' => self::USER_WLON_ID, 'login' => 'whitelist_only_user', 'name' => 'Test User 4', 'private' => false, 'whitelist-only' => true],
|
||||
['id' => self::USER_PRWL_ID, 'login' => 'private_whitelist_only_user', 'name' => 'Test User 4', 'private' => false, 'whitelist-only' => true],
|
||||
['id' => self::USER_UNNM_ID, 'login' => 'unnamed_user', 'name' => null, 'private' => false, 'whitelist-only' => false],
|
||||
];
|
||||
|
||||
public function load(ObjectManager $om)
|
||||
{
|
||||
$userId = 99999;
|
||||
|
||||
foreach ($this->users as $userData) {
|
||||
$user = new User($userId--, new \DateTime(), $userData['login'], $userData['name']);
|
||||
$user = new User($userData['id'], new \DateTime(), $userData['login'], $userData['name']);
|
||||
$user->updatePrivacy(!$userData['private'], $userData['whitelist-only']);
|
||||
|
||||
$om->persist($user);
|
||||
|
||||
|
|
|
@ -4,14 +4,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class AbstractFactory
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
/** @var LoggerInterface */
|
||||
protected $logger;
|
||||
|
||||
|
||||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
|
|
|
@ -10,9 +10,9 @@ use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException;
|
|||
|
||||
class UserFactory extends AbstractFactory
|
||||
{
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
public const DATE_FORMAT = 'Y-m-d_H:i:s';
|
||||
|
||||
/** @var UserRepository */
|
||||
private $userRepository;
|
||||
|
||||
|
||||
|
@ -55,6 +55,9 @@ class UserFactory extends AbstractFactory
|
|||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*/
|
||||
public function findOrCreateFromDTOArray(array $usersData): array
|
||||
{
|
||||
// @todo LOG
|
||||
|
|
|
@ -2,11 +2,12 @@
|
|||
|
||||
namespace Tests\Skobkin\PointToolsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Client;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class MainControllerTest extends WebTestCase
|
||||
{
|
||||
public function testUserSearch()
|
||||
public function testUserSearch(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/');
|
||||
|
@ -19,7 +20,7 @@ class MainControllerTest extends WebTestCase
|
|||
$this->assertTrue($client->getResponse()->isRedirect('/user/testuser'), 'Redirect to testuser\'s page didn\'t happen');
|
||||
}
|
||||
|
||||
public function testNonExistingUserSearch()
|
||||
public function testNonExistingUserSearch(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/');
|
||||
|
@ -49,7 +50,7 @@ class MainControllerTest extends WebTestCase
|
|||
$this->assertEquals(' Login not found', $firstError->text(), 'Incorrect error text');
|
||||
}
|
||||
|
||||
public function testUserStats()
|
||||
public function testUserStats(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/');
|
||||
|
@ -75,14 +76,10 @@ class MainControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* Tests AJAX user search autocomplete and returns JSON response string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function testAjaxUserAutoComplete()
|
||||
public function testAjaxUserAutoComplete(): string
|
||||
{
|
||||
$client = static::createClient();
|
||||
// We need to search all test user with 'testuser5' included which will test the code against null-string problem in User#getName()
|
||||
$client->request('GET', '/ajax/users/search/testuser');
|
||||
$client = $this->createClientForAjaxUserSearchByLogin('testuser');
|
||||
|
||||
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
|
||||
|
||||
|
@ -91,26 +88,22 @@ class MainControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* @depends testAjaxUserAutoComplete
|
||||
*
|
||||
* @param $json
|
||||
*/
|
||||
public function testAjaxUserAutoCompleteHasOptions($json)
|
||||
public function testAjaxUserAutoCompleteHasOptions(string $json): array
|
||||
{
|
||||
$data = json_decode($json);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
||||
$this->assertTrue(is_array($data), 'JSON data is array');
|
||||
$this->assertCount(5, $data, 'Array has 5 elements');
|
||||
$this->assertCount(2, $data, 'Array has 2 elements');
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAjaxUserAutoCompleteHasOptions
|
||||
*
|
||||
* @param array $users
|
||||
*/
|
||||
public function testAjaxUserAutoCompleteHasValidUserObjects(array $users)
|
||||
public function testAjaxUserAutoCompleteHasValidUserObjects(array $users): void
|
||||
{
|
||||
foreach ($users as $key => $user) {
|
||||
$this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key));
|
||||
|
@ -118,7 +111,43 @@ class MainControllerTest extends WebTestCase
|
|||
}
|
||||
}
|
||||
|
||||
public function testAjaxUserAutoCompleteForNonExistingUser()
|
||||
/**
|
||||
* Tests AJAX user search autocomplete for unnamed user and returns JSON response string
|
||||
*/
|
||||
public function testAjaxUserAutoCompleteForUnnamedUser(): string
|
||||
{
|
||||
$client = $this->createClientForAjaxUserSearchByLogin('unnamed_user');
|
||||
|
||||
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
|
||||
|
||||
return $client->getResponse()->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAjaxUserAutoCompleteForUnnamedUser
|
||||
*/
|
||||
public function testAjaxUserAutoCompleteHasOptionsForUnnamedUser(string $json): array
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
|
||||
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
||||
$this->assertInternalType('array', $data, 'JSON data is array');
|
||||
$this->assertCount(1, $data, 'Array has 1 elements');
|
||||
|
||||
return reset($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAjaxUserAutoCompleteHasOptionsForUnnamedUser
|
||||
*/
|
||||
public function testAjaxUserAutoCompleteHasValidUserObjectsForUnnamedUser(array $user): void
|
||||
{
|
||||
$this->assertTrue(array_key_exists('login', $user), 'Result has \'login\' field');
|
||||
$this->assertTrue(array_key_exists('name', $user), 'Result has \'name\' field');
|
||||
$this->assertEquals(true, ('' === $user['name'] || null === $user['name']), 'User name is empty string or null');
|
||||
}
|
||||
|
||||
public function testAjaxUserAutoCompleteIsEmptyForNonExistingUser(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah');
|
||||
|
@ -128,7 +157,15 @@ class MainControllerTest extends WebTestCase
|
|||
$data = json_decode($client->getResponse()->getContent());
|
||||
|
||||
$this->assertNotNull($data, 'JSON data successfully decoded and not empty');
|
||||
$this->assertTrue(is_array($data), 'JSON data is array');
|
||||
$this->assertEquals(0, count($data), 'Array has no elements');
|
||||
$this->assertInternalType('array', $data, 'JSON data is array');
|
||||
$this->assertCount(0, $data, 'Array has no elements');
|
||||
}
|
||||
|
||||
private function createClientForAjaxUserSearchByLogin(string $login): Client
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', '/ajax/users/search/'.$login);
|
||||
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace Tests\Skobkin\PointToolsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM\LoadPostData;
|
||||
use Symfony\Bundle\FrameworkBundle\{Client, Test\WebTestCase};
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class PostControllerTest extends WebTestCase
|
||||
{
|
||||
public function testNonExistingPostPage()
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', '/nonexistingpost');
|
||||
$client = $this->createClientForPostId('nonexistingpost');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isNotFound(), '404 response code for non-existing post');
|
||||
}
|
||||
|
@ -20,12 +20,11 @@ class PostControllerTest extends WebTestCase
|
|||
*/
|
||||
public function testShortPostPageIsOk()
|
||||
{
|
||||
$client = static::createClient();
|
||||
$crawler = $client->request('GET', '/shortpost');
|
||||
$client = $this->createClientForPostId(LoadPostData::POST_ID_SHORT);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isOk(), '200 response code for existing post');
|
||||
|
||||
return $crawler;
|
||||
return $client->getCrawler();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,4 +57,33 @@ class PostControllerTest extends WebTestCase
|
|||
$this->assertEquals(1, $p->count(), '.post-text has zero or more than one paragraphs');
|
||||
$this->assertEquals('Test short post', $p->text(), '.post-text has no correct post text');
|
||||
}
|
||||
|
||||
public function testPrivateUserPostForbidden()
|
||||
{
|
||||
$client = $this->createClientForPostId(LoadPostData::POST_ID_PR_USER);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isNotFound(), '404 response code for private user\'s post');
|
||||
}
|
||||
|
||||
public function testWhitelistOnlyUserPostForbidden()
|
||||
{
|
||||
$client = $this->createClientForPostId(LoadPostData::POST_ID_WL_USER);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isNotFound(), '404 response code for whitelist-only user\'s post');
|
||||
}
|
||||
|
||||
public function testPrivateWhitelistOnlyUserPostForbidden()
|
||||
{
|
||||
$client = $this->createClientForPostId(LoadPostData::POST_ID_PR_WL_USER);
|
||||
|
||||
$this->assertTrue($client->getResponse()->isNotFound(), '404 response code for private whitelist-only user\'s post');
|
||||
}
|
||||
|
||||
private function createClientForPostId(string $id): Client
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', '/'.$id);
|
||||
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ class UserRepositoryTest extends KernelTestCase
|
|||
{
|
||||
$users = $this->userRepo->findAll();
|
||||
|
||||
$this->assertCount(5, $users, 'Not exactly 5 users in the databas');
|
||||
$this->assertCount(6, $users, 'Not exactly 6 users in the databas');
|
||||
}
|
||||
|
||||
public function testFindOneByLogin()
|
||||
|
@ -58,14 +58,14 @@ class UserRepositoryTest extends KernelTestCase
|
|||
// Searching LIKE %stus% (testuserX)
|
||||
$users = $this->userRepo->findUsersLikeLogin('stus');
|
||||
|
||||
$this->assertCount(5, $users, 'Repository found not exactly 5 users');
|
||||
$this->assertCount(2, $users, 'Repository found not exactly 5 users');
|
||||
}
|
||||
|
||||
public function testGetUsersCount()
|
||||
{
|
||||
$count = $this->userRepo->getUsersCount();
|
||||
|
||||
$this->assertEquals(5, $count, 'Counted not exactly 5 users');
|
||||
$this->assertEquals(6, $count, 'Counted not exactly 5 users');
|
||||
}
|
||||
|
||||
public function testFindUserSubscribersById()
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Skobkin\PointToolsBundle\Service\Factory;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Skobkin\Bundle\PointToolsBundle\DTO\Api\User as UserDTO;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException;
|
||||
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
|
||||
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
|
||||
|
||||
class UserFactoryTest extends TestCase
|
||||
{
|
||||
private const LOCAL_USER_ID = 1;
|
||||
private const LOCAL_USER_LOGIN = 'test-local';
|
||||
private const LOCAL_USER_NAME = 'Test Local Name';
|
||||
private const LOCAL_USER_CREATED = '1999-01-01_01:02:03';
|
||||
|
||||
private const REMOTE_USER_ID = 1;
|
||||
private const REMOTE_USER_LOGIN = 'test-remote';
|
||||
private const REMOTE_USER_NAME = 'Test Remote Name';
|
||||
private const REMOTE_USER_CREATED = '2000-01-01_01:02:03';
|
||||
|
||||
public function testCreateFactory(): UserFactory
|
||||
{
|
||||
$testUser = new User(
|
||||
self::LOCAL_USER_ID,
|
||||
\DateTime::createFromFormat(UserFactory::DATE_FORMAT, self::LOCAL_USER_CREATED),
|
||||
self::LOCAL_USER_LOGIN,
|
||||
self::LOCAL_USER_NAME
|
||||
);
|
||||
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$userRepository = $this->createMock(UserRepository::class);
|
||||
$userRepository->expects($this->any())
|
||||
->method('find')
|
||||
->willReturnCallback(
|
||||
function ($id) use ($testUser) {
|
||||
if (1 === $id) {
|
||||
return $testUser;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
);
|
||||
|
||||
$userFactory = new UserFactory($logger, $userRepository);
|
||||
|
||||
$this->assertInstanceOf(UserFactory::class, $userFactory);
|
||||
|
||||
return $userFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider userDtoProvider
|
||||
* @depends testCreateFactory
|
||||
*/
|
||||
public function testFindOrCreateFromDTO(UserDTO $userDto, UserFactory $userFactory)
|
||||
{
|
||||
$foundUser = $userFactory->findOrCreateFromDTO($userDto);
|
||||
|
||||
$this->assertInstanceOf(User::class, $foundUser);
|
||||
|
||||
$this->assertEquals(self::LOCAL_USER_ID, $foundUser->getId());
|
||||
$this->assertEquals(self::REMOTE_USER_NAME, $foundUser->getName());
|
||||
$this->assertEquals(self::REMOTE_USER_LOGIN, $foundUser->getLogin());
|
||||
|
||||
$testDate = \DateTime::createFromFormat(UserFactory::DATE_FORMAT, self::REMOTE_USER_CREATED);
|
||||
|
||||
$this->assertEquals($testDate, $foundUser->getCreatedAt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider userDtoArrayProvider
|
||||
* @depends testCreateFactory
|
||||
*/
|
||||
public function testFindOrCreateFromDTOArray(array $userData, UserFactory $userFactory)
|
||||
{
|
||||
$foundUsers = $userFactory->findOrCreateFromDTOArray($userData);
|
||||
|
||||
$this->assertCount(2, $foundUsers);
|
||||
$this->assertContainsOnlyInstancesOf(User::class, $foundUsers);
|
||||
|
||||
$testDate = \DateTime::createFromFormat(UserFactory::DATE_FORMAT, self::REMOTE_USER_CREATED);
|
||||
|
||||
$this->assertEquals(self::REMOTE_USER_ID, $foundUsers[0]->getId());
|
||||
$this->assertEquals(self::REMOTE_USER_LOGIN, $foundUsers[0]->getLogin());
|
||||
$this->assertEquals(self::REMOTE_USER_NAME, $foundUsers[0]->getName());
|
||||
$this->assertEquals($testDate, $foundUsers[0]->getCreatedAt());
|
||||
|
||||
$this->assertEquals(self::REMOTE_USER_ID + 1, $foundUsers[1]->getId());
|
||||
$this->assertEquals(self::REMOTE_USER_LOGIN, $foundUsers[1]->getLogin());
|
||||
$this->assertEquals(self::REMOTE_USER_NAME, $foundUsers[1]->getName());
|
||||
$this->assertEquals($testDate, $foundUsers[1]->getCreatedAt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidUserDtoProvider
|
||||
* @depends testCreateFactory
|
||||
*/
|
||||
public function testFindOrCreateFromDTOWithInvalidDTO(UserDTO $userDto, UserFactory $userFactory)
|
||||
{
|
||||
$this->expectException(InvalidUserDataException::class);
|
||||
|
||||
$foundUser = $userFactory->findOrCreateFromDTO($userDto);
|
||||
}
|
||||
|
||||
public function userDtoProvider(): array
|
||||
{
|
||||
$userDto = new UserDTO();
|
||||
$userDto->setId(self::REMOTE_USER_ID);
|
||||
$userDto->setLogin(self::REMOTE_USER_LOGIN);
|
||||
$userDto->setName(self::REMOTE_USER_NAME);
|
||||
$userDto->setCreated(self::REMOTE_USER_CREATED);
|
||||
|
||||
return [[$userDto]];
|
||||
}
|
||||
|
||||
public function userDtoArrayProvider(): array
|
||||
{
|
||||
$userDto1 = new UserDTO();
|
||||
$userDto1->setId(self::REMOTE_USER_ID);
|
||||
$userDto1->setLogin(self::REMOTE_USER_LOGIN);
|
||||
$userDto1->setName(self::REMOTE_USER_NAME);
|
||||
$userDto1->setCreated(self::REMOTE_USER_CREATED);
|
||||
|
||||
$userDto2 = new UserDTO();
|
||||
$userDto2->setId(self::REMOTE_USER_ID + 1);
|
||||
$userDto2->setLogin(self::REMOTE_USER_LOGIN);
|
||||
$userDto2->setName(self::REMOTE_USER_NAME);
|
||||
$userDto2->setCreated(self::REMOTE_USER_CREATED);
|
||||
|
||||
return [[[$userDto1, $userDto2]]];
|
||||
}
|
||||
|
||||
public function invalidUserDtoProvider(): array
|
||||
{
|
||||
return [[new UserDTO()]];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue