From 3b815d2de5a852fe2ebad5827bd4c9070aa175ea Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 8 Feb 2015 23:05:51 +0100 Subject: [PATCH] Add some fixtures Improve test, so user can login Fix some leftJoin Cleanup EntryController --- app/AppKernel.php | 1 + app/build.xml | 7 + composer.json | 1 + composer.lock | 140 ++++++++++++++++-- .../CoreBundle/Controller/EntryController.php | 22 ++- .../DataFixtures/ORM/LoadEntryData.php | 35 +++++ .../DataFixtures/ORM/LoadUserData.php | 34 +++++ .../CoreBundle/Repository/EntryRepository.php | 63 +++++--- .../Tests/Controller/EntryControllerTest.php | 38 +++-- .../Controller/SecurityControllerTest.php | 40 +++++ .../CoreBundle/Tests/WallabagTestCase.php | 34 +++++ 11 files changed, 361 insertions(+), 54 deletions(-) create mode 100644 src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php create mode 100644 src/Wallabag/CoreBundle/DataFixtures/ORM/LoadUserData.php create mode 100644 src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php create mode 100644 src/Wallabag/CoreBundle/Tests/WallabagTestCase.php diff --git a/app/AppKernel.php b/app/AppKernel.php index 515c79ecb..3dfefd584 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -28,6 +28,7 @@ class AppKernel extends Kernel $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); + $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(); } return $bundles; diff --git a/app/build.xml b/app/build.xml index 6427867c4..a8e43f0d8 100644 --- a/app/build.xml +++ b/app/build.xml @@ -34,5 +34,12 @@ + + + + + + + diff --git a/composer.json b/composer.json index 8771ea90b..0136ed2e4 100644 --- a/composer.json +++ b/composer.json @@ -83,6 +83,7 @@ "wallabag/Fivefilters_Libraries": "dev-master" }, "require-dev": { + "doctrine/doctrine-fixtures-bundle": "dev-master", "sensio/generator-bundle": "~2.5", "phpunit/phpunit": "~4.4" }, diff --git a/composer.lock b/composer.lock index dded6dd76..a7890ac2d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "5005a650aa4368bd3485939efd9e24ac", + "hash": "16c2143e6e1977d625f5557f07dfc118", "packages": [ { "name": "doctrine/annotations", @@ -1666,21 +1666,20 @@ }, { "name": "robmorgan/phinx", - "version": "v0.4.1", + "version": "v0.4.2.1", "source": { "type": "git", "url": "https://github.com/robmorgan/phinx.git", - "reference": "357210707c000f50edea802d84b74724ad122478" + "reference": "1bc1396392d4073b8b29ee5289e445889cbc12b5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/robmorgan/phinx/zipball/357210707c000f50edea802d84b74724ad122478", - "reference": "357210707c000f50edea802d84b74724ad122478", + "url": "https://api.github.com/repos/robmorgan/phinx/zipball/1bc1396392d4073b8b29ee5289e445889cbc12b5", + "reference": "1bc1396392d4073b8b29ee5289e445889cbc12b5", "shasum": "" }, "require": { "php": ">=5.3.2", - "symfony/class-loader": "~2.6.0", "symfony/config": "~2.6.0", "symfony/console": "~2.6.0", "symfony/yaml": "~2.6.0" @@ -1694,8 +1693,8 @@ ], "type": "library", "autoload": { - "psr-0": { - "Phinx": "src/" + "psr-4": { + "Phinx\\": "src/Phinx" } }, "notification-url": "https://packagist.org/downloads/", @@ -1708,6 +1707,12 @@ "email": "robbym@gmail.com", "homepage": "http://robmorgan.id.au", "role": "Lead Developer" + }, + { + "name": "Woody Gilk", + "email": "woody.gilk@gmail.com", + "homepage": "http://shadowhand.me", + "role": "Developer" } ], "description": "Phinx makes it ridiculously easy to manage the database migrations for your PHP app.", @@ -1719,7 +1724,7 @@ "migrations", "phinx" ], - "time": "2014-12-23 06:06:14" + "time": "2015-02-08 03:41:44" }, { "name": "sensio/distribution-bundle", @@ -2871,6 +2876,120 @@ } ], "packages-dev": [ + { + "name": "doctrine/data-fixtures", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "b4a135c7db56ecc4602b54a2184368f440cac33e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/b4a135c7db56ecc4602b54a2184368f440cac33e", + "reference": "b4a135c7db56ecc4602b54a2184368f440cac33e", + "shasum": "" + }, + "require": { + "doctrine/common": ">=2.2,<2.5-dev", + "php": ">=5.3.2" + }, + "require-dev": { + "doctrine/orm": ">=2.2,<2.5-dev" + }, + "suggest": { + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\DataFixtures": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "database" + ], + "time": "2013-07-10 17:04:07" + }, + { + "name": "doctrine/doctrine-fixtures-bundle", + "version": "dev-master", + "target-dir": "Doctrine/Bundle/FixturesBundle", + "source": { + "type": "git", + "url": "https://github.com/doctrine/DoctrineFixturesBundle.git", + "reference": "c5ff0542772102ddd4e2fbe173e9ad40ad67c22f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/c5ff0542772102ddd4e2fbe173e9ad40ad67c22f", + "reference": "c5ff0542772102ddd4e2fbe173e9ad40ad67c22f", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "~1.0", + "doctrine/doctrine-bundle": "~1.0", + "php": ">=5.3.2", + "symfony/doctrine-bridge": "~2.1" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Bundle\\FixturesBundle": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Doctrine Project", + "homepage": "http://www.doctrine-project.org" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony DoctrineFixturesBundle", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "Fixture", + "persistence" + ], + "time": "2015-01-19 02:21:37" + }, { "name": "doctrine/instantiator", "version": "1.0.4", @@ -3834,7 +3953,8 @@ "wallabag/phpepub": 20, "wallabag/php-readability": 20, "wallabag/phpmobi": 20, - "wallabag/fivefilters_libraries": 20 + "wallabag/fivefilters_libraries": 20, + "doctrine/doctrine-fixtures-bundle": 20 }, "prefer-stable": true, "prefer-lowest": false, diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 2dfe2f513..e0697ca3e 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -19,9 +19,7 @@ class EntryController extends Controller */ public function addEntryAction(Request $request) { - $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:User'); - $user = $repository->find(1); - $entry = new Entry($user); + $entry = new Entry($this->getUser()); $form = $this->createFormBuilder($entry) ->add('url', 'url') @@ -61,10 +59,10 @@ class EntryController extends Controller */ public function showUnreadAction() { - $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); - // TODO don't give the user ID like this // TODO change pagination - $entries = $repository->findUnreadByUser(1, 0); + $entries = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findUnreadByUser($this->getUser()->getId(), 0); return $this->render( 'WallabagCoreBundle:Entry:entries.html.twig', @@ -80,10 +78,10 @@ class EntryController extends Controller */ public function showArchiveAction() { - $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); - // TODO don't give the user ID like this // TODO change pagination - $entries = $repository->findArchiveByUser(1, 0); + $entries = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findArchiveByUser($this->getUser()->getId(), 0); return $this->render( 'WallabagCoreBundle:Entry:entries.html.twig', @@ -99,10 +97,10 @@ class EntryController extends Controller */ public function showStarredAction() { - $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); - // TODO don't give the user ID like this // TODO change pagination - $entries = $repository->findStarredByUser(1, 0); + $entries = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findStarredByUser($this->getUser()->getId(), 0); return $this->render( 'WallabagCoreBundle:Entry:entries.html.twig', diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php new file mode 100644 index 000000000..fccd06be1 --- /dev/null +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -0,0 +1,35 @@ +getReference('admin-user')); + $entry1->setUrl('http://0.0.0.0'); + $entry1->setTitle('test title'); + $entry1->setContent('This is my content /o/'); + + $manager->persist($entry1); + $manager->flush(); + + $this->addReference('entry1', $entry1); + } + + /** + * {@inheritDoc} + */ + public function getOrder() + { + return 20; + } +} diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadUserData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadUserData.php new file mode 100644 index 000000000..da7882183 --- /dev/null +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadUserData.php @@ -0,0 +1,34 @@ +setUsername('admin'); + $userAdmin->setPassword('test'); + + $manager->persist($userAdmin); + $manager->flush(); + + $this->addReference('admin-user', $userAdmin); + } + + /** + * {@inheritDoc} + */ + public function getOrder() + { + return 10; + } +} diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f4c803f99..5ae1337a8 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -11,19 +11,20 @@ class EntryRepository extends EntityRepository /** * Retrieves unread entries for a user * - * @param $userId - * @param $firstResult - * @param int $maxResults + * @param int $userId + * @param int $firstResult + * @param int $maxResults + * * @return Paginator */ public function findUnreadByUser($userId, $firstResult, $maxResults = 12) { $qb = $this->createQueryBuilder('e') - ->select('e') ->setFirstResult($firstResult) ->setMaxResults($maxResults) + ->leftJoin('e.user', 'u') ->where('e.isArchived = false') - ->andWhere('e.userId =:userId')->setParameter('userId', $userId) + ->andWhere('u.id =:userId')->setParameter('userId', $userId) ->andWhere('e.isDeleted=false') ->orderBy('e.createdAt', 'desc') ->getQuery(); @@ -36,9 +37,10 @@ class EntryRepository extends EntityRepository /** * Retrieves read entries for a user * - * @param $userId - * @param $firstResult - * @param int $maxResults + * @param int $userId + * @param int $firstResult + * @param int $maxResults + * * @return Paginator */ public function findArchiveByUser($userId, $firstResult, $maxResults = 12) @@ -47,8 +49,9 @@ class EntryRepository extends EntityRepository ->select('e') ->setFirstResult($firstResult) ->setMaxResults($maxResults) + ->leftJoin('e.user', 'u') ->where('e.isArchived = true') - ->andWhere('e.userId =:userId')->setParameter('userId', $userId) + ->andWhere('u.id =:userId')->setParameter('userId', $userId) ->andWhere('e.isDeleted=false') ->orderBy('e.createdAt', 'desc') ->getQuery(); @@ -61,9 +64,10 @@ class EntryRepository extends EntityRepository /** * Retrieves starred entries for a user * - * @param $userId - * @param $firstResult - * @param int $maxResults + * @param int $userId + * @param int $firstResult + * @param int $maxResults + * * @return Paginator */ public function findStarredByUser($userId, $firstResult, $maxResults = 12) @@ -72,9 +76,10 @@ class EntryRepository extends EntityRepository ->select('e') ->setFirstResult($firstResult) ->setMaxResults($maxResults) + ->leftJoin('e.user', 'u') ->where('e.isStarred = true') - ->andWhere('e.userId =:userId')->setParameter('userId', $userId) - ->andWhere('e.isDeleted=false') + ->andWhere('u.id =:userId')->setParameter('userId', $userId) + ->andWhere('e.isDeleted = false') ->orderBy('e.createdAt', 'desc') ->getQuery(); @@ -83,22 +88,34 @@ class EntryRepository extends EntityRepository return $paginator; } - public function findEntries($userId, $isArchived, $isStarred, $isDeleted, $sort, $order) + /** + * Find Entries + * + * @param int $userId + * @param bool $isArchived + * @param bool $isStarred + * @param bool $isDeleted + * @param string $sort + * @param string $order + * + * @return ArrayCollection + */ + public function findEntries($userId, $isArchived = null, $isStarred = null, $isDeleted = null, $sort = 'created', $order = 'ASC') { $qb = $this->createQueryBuilder('e') - ->select('e') - ->where('e.userId =:userId')->setParameter('userId', $userId); + ->leftJoin('e.user', 'u') + ->where('u.id =:userId')->setParameter('userId', $userId); - if (!is_null($isArchived)) { - $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', $isArchived); + if (null !== $isArchived) { + $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); } - if (!is_null($isStarred)) { - $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', $isStarred); + if (null !== $isStarred) { + $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); } - if (!is_null($isDeleted)) { - $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', $isDeleted); + if (null !== $isDeleted) { + $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', (bool) $isDeleted); } if ('created' === $sort) { diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index fde210c99..5d8daea39 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -2,13 +2,24 @@ namespace Wallabag\CoreBundle\Tests\Controller; -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Wallabag\CoreBundle\Tests\WallabagTestCase; -class EntryControllerTest extends WebTestCase +class EntryControllerTest extends WallabagTestCase { + public function testLogin() + { + $client = $this->getClient(); + + $crawler = $client->request('GET', '/new'); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('login', $client->getResponse()->headers->get('location')); + } + public function testGetNew() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $crawler = $client->request('GET', '/new'); @@ -20,7 +31,8 @@ class EntryControllerTest extends WebTestCase public function testPostNewEmpty() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $crawler = $client->request('GET', '/new'); @@ -37,7 +49,8 @@ class EntryControllerTest extends WebTestCase public function testPostNewOk() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $crawler = $client->request('GET', '/new'); @@ -55,13 +68,14 @@ class EntryControllerTest extends WebTestCase $crawler = $client->followRedirect(); - $this->assertCount(1, $alert = $crawler->filter('h2 a')->extract(array('_text'))); + $this->assertGreaterThan(1, $alert = $crawler->filter('h2 a')->extract(array('_text'))); $this->assertContains('Mailjet', $alert[0]); } public function testArchive() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $crawler = $client->request('GET', '/archive'); @@ -70,7 +84,8 @@ class EntryControllerTest extends WebTestCase public function testStarred() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $crawler = $client->request('GET', '/starred'); @@ -79,13 +94,18 @@ class EntryControllerTest extends WebTestCase public function testView() { - $client = static::createClient(); + $this->logIn(); + $client = $this->getClient(); $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') ->findOneByIsArchived(false); + if (!$content) { + $this->markTestSkipped('No content found in db.'); + } + $crawler = $client->request('GET', '/view/'.$content->getId()); $this->assertEquals(200, $client->getResponse()->getStatusCode()); diff --git a/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php new file mode 100644 index 000000000..54cf5073e --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php @@ -0,0 +1,40 @@ +getClient(); + + $crawler = $client->request('GET', '/new'); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('login', $client->getResponse()->headers->get('location')); + } + + public function testLoginFail() + { + $client = $this->getClient(); + + $crawler = $client->request('GET', '/login'); + + $form = $crawler->filter('button[type=submit]')->form(); + $data = array( + '_username' => 'admin', + '_password' => 'admin', + ); + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('login', $client->getResponse()->headers->get('location')); + + $crawler = $client->followRedirect(); + + $this->assertContains('Bad credentials', $client->getResponse()->getContent()); + } +} diff --git a/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php b/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php new file mode 100644 index 000000000..5f0923181 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/WallabagTestCase.php @@ -0,0 +1,34 @@ +client; + } + + public function setUp() + { + $this->client = static::createClient(); + } + + public function logIn() + { + $crawler = $this->client->request('GET', '/login'); + $form = $crawler->filter('button[type=submit]')->form(); + $data = array( + '_username' => 'admin', + '_password' => 'test', + ); + + $this->client->submit($form, $data); + } +}