Compare commits

..

1 commit

Author SHA1 Message Date
Alexey Skobkin 574320b6b7 composer update doctrine/doctrine-fixtures-bundle and corresponding changes. 2019-01-19 04:30:15 +03:00
17 changed files with 771 additions and 1458 deletions

View file

@ -9,4 +9,4 @@ security:
security: false security: false
default: default:
anonymous: true anonymous: ~

View file

@ -10,6 +10,10 @@ services:
resource: '../../src/Skobkin/Bundle/PointToolsBundle/*' resource: '../../src/Skobkin/Bundle/PointToolsBundle/*'
exclude: '../../src/Skobkin/Bundle/PointToolsBundle/{DataFixtures,DependencyInjection,DQL,DTO,Entity,Exception,Repository,Twig}' exclude: '../../src/Skobkin/Bundle/PointToolsBundle/{DataFixtures,DependencyInjection,DQL,DTO,Entity,Exception,Repository,Twig}'
Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM\:
resource: '../../src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/*'
tags: ['doctrine.fixture.orm']
# HTTP clients # HTTP clients
# Default # Default
GuzzleHttp\ClientInterface: GuzzleHttp\ClientInterface:

View file

@ -13,7 +13,6 @@
}, },
"require": { "require": {
"php": ">=7.1.0", "php": ">=7.1.0",
"ext-json": "*",
"symfony/symfony": "^3.4", "symfony/symfony": "^3.4",
"doctrine/orm": "^2.5", "doctrine/orm": "^2.5",
"doctrine/annotations": "^1.3.0", "doctrine/annotations": "^1.3.0",
@ -39,7 +38,7 @@
"require-dev": { "require-dev": {
"symfony/phpunit-bridge": "^3.0", "symfony/phpunit-bridge": "^3.0",
"phpunit/phpunit": "^5.7", "phpunit/phpunit": "^5.7",
"doctrine/doctrine-fixtures-bundle": "^2.3" "doctrine/doctrine-fixtures-bundle": "^3"
}, },
"scripts": { "scripts": {
"symfony-scripts": [ "symfony-scripts": [
@ -58,7 +57,10 @@
] ]
}, },
"config": { "config": {
"bin-dir": "bin" "bin-dir": "bin",
"platform": {
"php": "7.1.23"
}
}, },
"extra": { "extra": {
"symfony-app-dir": "app", "symfony-app-dir": "app",

1818
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -19,17 +19,6 @@
</testsuite> </testsuite>
</testsuites> </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> <filter>
<whitelist> <whitelist>
<directory>src</directory> <directory>src</directory>

View file

@ -228,7 +228,7 @@ class UpdateSubscriptionsCommand extends Command
); );
/** @var Subscription $subscription */ /** @var Subscription $subscription */
foreach ($serviceUser->getSubscribers() as $subscription) { foreach ((array) $serviceUser->getSubscribers() as $subscription) {
$usersForUpdate[] = $subscription->getSubscriber(); $usersForUpdate[] = $subscription->getSubscriber();
} }
} }

View file

@ -12,19 +12,11 @@ class PostController extends AbstractController
{ {
/** /**
* @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post") * @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post")
*
* @return Response
*/ */
public function showAction(Post $post, PostRepository $postRepository): 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', [ return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [
'post' => $postRepository->getPostWithComments($post->getId()), 'post' => $postRepository->getPostWithComments($post->getId()),
]); ]);

View file

@ -2,14 +2,13 @@
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM; namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\{Comment, Post};
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
class LoadCommentsData extends AbstractFixture implements OrderedFixtureInterface class LoadCommentsData extends Fixture implements OrderedFixtureInterface
{ {
public function load(ObjectManager $om) public function load(ObjectManager $om)
{ {

View file

@ -2,64 +2,33 @@
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM; namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\{AbstractFixture, OrderedFixtureInterface}; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
use Skobkin\Bundle\PointToolsBundle\Entity\{Blogs\Post, User}; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
class LoadPostData extends AbstractFixture implements OrderedFixtureInterface class LoadPostData extends Fixture 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) public function load(ObjectManager $om)
{ {
/** @var User $mainUser */ /** @var User $testUser */
$mainUser = $this->getReference('test_user_'.LoadUserData::USER_MAIN_ID); $testUser = $this->getReference('test_user_99999');
/** @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(self::POST_ID_LONG, $mainUser, new \DateTime(), Post::TYPE_POST)) $longPost = (new Post('longpost', $testUser, new \DateTime(), Post::TYPE_POST))
->setText('Test post with many comments') ->setText('Test post with many comments')
->setPrivate(false) ->setPrivate(false)
->setDeleted(false) ->setDeleted(false)
; ;
$shortPost = (new Post(self::POST_ID_SHORT, $mainUser, new \DateTime(), Post::TYPE_POST)) $shortPost = (new Post('shortpost', $testUser, new \DateTime(), Post::TYPE_POST))
->setText('Test short post') ->setText('Test short post')
->setPrivate(false) ->setPrivate(false)
->setDeleted(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($longPost);
$om->persist($shortPost); $om->persist($shortPost);
$om->persist($privateUserPost);
$om->persist($wlUserPost);
$om->persist($privateWlUserPost);
$om->flush(); $om->flush();
$this->addReference('test_post_longpost', $longPost); $this->addReference('test_post_longpost', $longPost);

View file

@ -2,7 +2,7 @@
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM; namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription; use Skobkin\Bundle\PointToolsBundle\Entity\Subscription;
@ -12,7 +12,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
/** /**
* Load user subscriptions * Load user subscriptions
*/ */
class LoadSubscribersData extends AbstractFixture implements OrderedFixtureInterface class LoadSubscribersData extends Fixture implements OrderedFixtureInterface
{ {
public function load(ObjectManager $om) public function load(ObjectManager $om)
{ {

View file

@ -2,34 +2,32 @@
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM; namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface class LoadUserData extends Fixture 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 = [ private $users = [
['id' => self::USER_MAIN_ID, 'login' => 'testuser', 'name' => 'Test User 1', 'private' => false, 'whitelist-only' => false], // 99999
['id' => self::USER_SCND_ID, 'login' => 'testuser2', 'name' => 'Test User 2 for autocomplete test', 'private' => false, 'whitelist-only' => false], ['login' => 'testuser', 'name' => 'Test User 1'],
['id' => self::USER_PRIV_ID, 'login' => 'private_user', 'name' => 'Test User 3', 'private' => true, 'whitelist-only' => false], // 99998
['id' => self::USER_WLON_ID, 'login' => 'whitelist_only_user', 'name' => 'Test User 4', 'private' => false, 'whitelist-only' => true], ['login' => 'testuser2', 'name' => 'Test User 2'],
['id' => self::USER_PRWL_ID, 'login' => 'private_whitelist_only_user', 'name' => 'Test User 4', 'private' => false, 'whitelist-only' => true], // 99997
['id' => self::USER_UNNM_ID, 'login' => 'unnamed_user', 'name' => null, 'private' => false, 'whitelist-only' => false], ['login' => 'testuser3', 'name' => 'Test User 3'],
// 99996
['login' => 'testuser4', 'name' => 'Test User 4'],
//99995
['login' => 'testuser5', 'name' => null],
]; ];
public function load(ObjectManager $om) public function load(ObjectManager $om)
{ {
$userId = 99999;
foreach ($this->users as $userData) { foreach ($this->users as $userData) {
$user = new User($userData['id'], new \DateTime(), $userData['login'], $userData['name']); $user = new User($userId--, new \DateTime(), $userData['login'], $userData['name']);
$user->updatePrivacy(!$userData['private'], $userData['whitelist-only']);
$om->persist($user); $om->persist($user);

View file

@ -4,11 +4,14 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
abstract class AbstractFactory class AbstractFactory
{ {
/** @var LoggerInterface */ /**
* @var LoggerInterface
*/
protected $logger; protected $logger;
public function __construct(LoggerInterface $logger) public function __construct(LoggerInterface $logger)
{ {
$this->logger = $logger; $this->logger = $logger;

View file

@ -10,9 +10,9 @@ use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException;
class UserFactory extends AbstractFactory class UserFactory extends AbstractFactory
{ {
public const DATE_FORMAT = 'Y-m-d_H:i:s'; /**
* @var UserRepository
/** @var UserRepository */ */
private $userRepository; private $userRepository;
@ -55,9 +55,6 @@ class UserFactory extends AbstractFactory
return $user; return $user;
} }
/**
* @return User[]
*/
public function findOrCreateFromDTOArray(array $usersData): array public function findOrCreateFromDTOArray(array $usersData): array
{ {
// @todo LOG // @todo LOG

View file

@ -2,12 +2,11 @@
namespace Tests\Skobkin\PointToolsBundle\Controller; namespace Tests\Skobkin\PointToolsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MainControllerTest extends WebTestCase class MainControllerTest extends WebTestCase
{ {
public function testUserSearch(): void public function testUserSearch()
{ {
$client = static::createClient(); $client = static::createClient();
$crawler = $client->request('GET', '/'); $crawler = $client->request('GET', '/');
@ -20,7 +19,7 @@ class MainControllerTest extends WebTestCase
$this->assertTrue($client->getResponse()->isRedirect('/user/testuser'), 'Redirect to testuser\'s page didn\'t happen'); $this->assertTrue($client->getResponse()->isRedirect('/user/testuser'), 'Redirect to testuser\'s page didn\'t happen');
} }
public function testNonExistingUserSearch(): void public function testNonExistingUserSearch()
{ {
$client = static::createClient(); $client = static::createClient();
$crawler = $client->request('GET', '/'); $crawler = $client->request('GET', '/');
@ -50,7 +49,7 @@ class MainControllerTest extends WebTestCase
$this->assertEquals(' Login not found', $firstError->text(), 'Incorrect error text'); $this->assertEquals(' Login not found', $firstError->text(), 'Incorrect error text');
} }
public function testUserStats(): void public function testUserStats()
{ {
$client = static::createClient(); $client = static::createClient();
$crawler = $client->request('GET', '/'); $crawler = $client->request('GET', '/');
@ -76,10 +75,14 @@ class MainControllerTest extends WebTestCase
/** /**
* Tests AJAX user search autocomplete and returns JSON response string * Tests AJAX user search autocomplete and returns JSON response string
*
* @return string
*/ */
public function testAjaxUserAutoComplete(): string public function testAjaxUserAutoComplete()
{ {
$client = $this->createClientForAjaxUserSearchByLogin('testuser'); $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');
$this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"'); $this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
@ -88,22 +91,26 @@ class MainControllerTest extends WebTestCase
/** /**
* @depends testAjaxUserAutoComplete * @depends testAjaxUserAutoComplete
*
* @param $json
*/ */
public function testAjaxUserAutoCompleteHasOptions(string $json): array public function testAjaxUserAutoCompleteHasOptions($json)
{ {
$data = json_decode($json, true); $data = json_decode($json);
$this->assertNotNull($data, 'JSON data successfully decoded and not empty'); $this->assertNotNull($data, 'JSON data successfully decoded and not empty');
$this->assertTrue(is_array($data), 'JSON data is array'); $this->assertTrue(is_array($data), 'JSON data is array');
$this->assertCount(2, $data, 'Array has 2 elements'); $this->assertCount(5, $data, 'Array has 5 elements');
return $data; return $data;
} }
/** /**
* @depends testAjaxUserAutoCompleteHasOptions * @depends testAjaxUserAutoCompleteHasOptions
*
* @param array $users
*/ */
public function testAjaxUserAutoCompleteHasValidUserObjects(array $users): void public function testAjaxUserAutoCompleteHasValidUserObjects(array $users)
{ {
foreach ($users as $key => $user) { foreach ($users as $key => $user) {
$this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key)); $this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key));
@ -111,43 +118,7 @@ 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 = static::createClient();
$client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah'); $client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah');
@ -157,15 +128,7 @@ class MainControllerTest extends WebTestCase
$data = json_decode($client->getResponse()->getContent()); $data = json_decode($client->getResponse()->getContent());
$this->assertNotNull($data, 'JSON data successfully decoded and not empty'); $this->assertNotNull($data, 'JSON data successfully decoded and not empty');
$this->assertInternalType('array', $data, 'JSON data is array'); $this->assertTrue(is_array($data), 'JSON data is array');
$this->assertCount(0, $data, 'Array has no elements'); $this->assertEquals(0, count($data), 'Array has no elements');
}
private function createClientForAjaxUserSearchByLogin(string $login): Client
{
$client = static::createClient();
$client->request('GET', '/ajax/users/search/'.$login);
return $client;
} }
} }

View file

@ -2,15 +2,15 @@
namespace Tests\Skobkin\PointToolsBundle\Controller; namespace Tests\Skobkin\PointToolsBundle\Controller;
use Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM\LoadPostData; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\{Client, Test\WebTestCase};
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
class PostControllerTest extends WebTestCase class PostControllerTest extends WebTestCase
{ {
public function testNonExistingPostPage() public function testNonExistingPostPage()
{ {
$client = $this->createClientForPostId('nonexistingpost'); $client = static::createClient();
$client->request('GET', '/nonexistingpost');
$this->assertTrue($client->getResponse()->isNotFound(), '404 response code for non-existing post'); $this->assertTrue($client->getResponse()->isNotFound(), '404 response code for non-existing post');
} }
@ -20,11 +20,12 @@ class PostControllerTest extends WebTestCase
*/ */
public function testShortPostPageIsOk() public function testShortPostPageIsOk()
{ {
$client = $this->createClientForPostId(LoadPostData::POST_ID_SHORT); $client = static::createClient();
$crawler = $client->request('GET', '/shortpost');
$this->assertTrue($client->getResponse()->isOk(), '200 response code for existing post'); $this->assertTrue($client->getResponse()->isOk(), '200 response code for existing post');
return $client->getCrawler(); return $crawler;
} }
/** /**
@ -57,33 +58,4 @@ class PostControllerTest extends WebTestCase
$this->assertEquals(1, $p->count(), '.post-text has zero or more than one paragraphs'); $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'); $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;
}
} }

View file

@ -32,7 +32,7 @@ class UserRepositoryTest extends KernelTestCase
{ {
$users = $this->userRepo->findAll(); $users = $this->userRepo->findAll();
$this->assertCount(6, $users, 'Not exactly 6 users in the databas'); $this->assertCount(5, $users, 'Not exactly 5 users in the databas');
} }
public function testFindOneByLogin() public function testFindOneByLogin()
@ -58,14 +58,14 @@ class UserRepositoryTest extends KernelTestCase
// Searching LIKE %stus% (testuserX) // Searching LIKE %stus% (testuserX)
$users = $this->userRepo->findUsersLikeLogin('stus'); $users = $this->userRepo->findUsersLikeLogin('stus');
$this->assertCount(2, $users, 'Repository found not exactly 5 users'); $this->assertCount(5, $users, 'Repository found not exactly 5 users');
} }
public function testGetUsersCount() public function testGetUsersCount()
{ {
$count = $this->userRepo->getUsersCount(); $count = $this->userRepo->getUsersCount();
$this->assertEquals(6, $count, 'Counted not exactly 5 users'); $this->assertEquals(5, $count, 'Counted not exactly 5 users');
} }
public function testFindUserSubscribersById() public function testFindUserSubscribersById()

View file

@ -1,141 +0,0 @@
<?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()]];
}
}