From 78833672469f7beb0c4a195aa0a76f7ca4133057 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 15 Jan 2016 15:28:22 +0100 Subject: Fix `findOneByUrl` side effect in tests Fix #1566 --- .../Tests/Controller/EntryControllerTest.php | 20 ++++++++++---------- .../CoreBundle/Tests/WallabagCoreTestCase.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src/Wallabag/CoreBundle/Tests') diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index 3a775182..1d1620dc 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -38,7 +38,7 @@ class EntryControllerTest extends WallabagCoreTestCase $form = $crawler->filter('button[type=submit]')->form(); $data = array( - 'entry[url]' => 'https://www.wallabag.org/blog/2016/01/08/wallabag-alpha1-v2', + 'entry[url]' => $this->url, ); $client->submit($form, $data); @@ -82,7 +82,7 @@ class EntryControllerTest extends WallabagCoreTestCase ->get('doctrine.orm.entity_manager'); $entry = $em ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $em->remove($entry); $em->flush(); } @@ -202,7 +202,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $client->request('GET', '/view/'.$content->getId()); @@ -223,7 +223,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); // empty content $content->setContent(''); @@ -237,7 +237,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->assertNotEmpty($content->getContent()); } @@ -250,7 +250,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -268,7 +268,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -298,7 +298,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $client->request('GET', '/archive/'.$content->getId()); @@ -320,7 +320,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $client->request('GET', '/star/'.$content->getId()); @@ -342,7 +342,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrl($this->url); + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $client->request('GET', '/delete/'.$content->getId()); diff --git a/src/Wallabag/CoreBundle/Tests/WallabagCoreTestCase.php b/src/Wallabag/CoreBundle/Tests/WallabagCoreTestCase.php index ce3cabe8..c4905478 100644 --- a/src/Wallabag/CoreBundle/Tests/WallabagCoreTestCase.php +++ b/src/Wallabag/CoreBundle/Tests/WallabagCoreTestCase.php @@ -31,4 +31,21 @@ abstract class WallabagCoreTestCase extends WebTestCase $this->client->submit($form, $data); } + + /** + * Return the user id of the logged in user. + * You should be sure that you called `logInAs` before. + * + * @return int + */ + public function getLoggedInUserId() + { + $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); + + if (null !== $token) { + return $token->getUser()->getId(); + } + + throw new \RuntimeException('No logged in User.'); + } } -- cgit v1.2.3