Fix findOneByUrl side effect in tests

Fix #1566
This commit is contained in:
Jeremy Benoist 2016-01-15 15:28:22 +01:00
parent 790573d458
commit 7883367246
8 changed files with 33 additions and 18 deletions

View file

@ -49,8 +49,7 @@ class EntryController extends Controller
if ($form->isValid()) { if ($form->isValid()) {
// check for existing entry, if it exists, redirect to it with a message // check for existing entry, if it exists, redirect to it with a message
$existingEntry = $this->get('wallabag_core.entry_repository') $existingEntry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
->existByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
if (false !== $existingEntry) { if (false !== $existingEntry) {
$this->get('session')->getFlashBag()->add( $this->get('session')->getFlashBag()->add(

View file

@ -235,10 +235,9 @@ class EntryRepository extends EntityRepository
* *
* @return array|bool * @return array|bool
*/ */
public function existByUrlAndUserId($url, $userId) public function findByUrlAndUserId($url, $userId)
{ {
$res = $this->createQueryBuilder('e') $res = $this->createQueryBuilder('e')
->select('e.id, e.createdAt')
->where('e.url = :url')->setParameter('url', $url) ->where('e.url = :url')->setParameter('url', $url)
->andWhere('e.user = :user_id')->setParameter('user_id', $userId) ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
->getQuery() ->getQuery()

View file

@ -38,7 +38,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$form = $crawler->filter('button[type=submit]')->form(); $form = $crawler->filter('button[type=submit]')->form();
$data = array( $data = array(
'entry[url]' => 'https://www.wallabag.org/blog/2016/01/08/wallabag-alpha1-v2', 'entry[url]' => $this->url,
); );
$client->submit($form, $data); $client->submit($form, $data);
@ -82,7 +82,7 @@ class EntryControllerTest extends WallabagCoreTestCase
->get('doctrine.orm.entity_manager'); ->get('doctrine.orm.entity_manager');
$entry = $em $entry = $em
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$em->remove($entry); $em->remove($entry);
$em->flush(); $em->flush();
} }
@ -202,7 +202,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/view/'.$content->getId()); $client->request('GET', '/view/'.$content->getId());
@ -223,7 +223,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
// empty content // empty content
$content->setContent(''); $content->setContent('');
@ -237,7 +237,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$this->assertNotEmpty($content->getContent()); $this->assertNotEmpty($content->getContent());
} }
@ -250,7 +250,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$crawler = $client->request('GET', '/edit/'.$content->getId()); $crawler = $client->request('GET', '/edit/'.$content->getId());
@ -268,7 +268,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$crawler = $client->request('GET', '/edit/'.$content->getId()); $crawler = $client->request('GET', '/edit/'.$content->getId());
@ -298,7 +298,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/archive/'.$content->getId()); $client->request('GET', '/archive/'.$content->getId());
@ -320,7 +320,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/star/'.$content->getId()); $client->request('GET', '/star/'.$content->getId());
@ -342,7 +342,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$content = $client->getContainer() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($this->url); ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/delete/'.$content->getId()); $client->request('GET', '/delete/'.$content->getId());

View file

@ -31,4 +31,21 @@ abstract class WallabagCoreTestCase extends WebTestCase
$this->client->submit($form, $data); $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.');
}
} }

View file

@ -214,7 +214,7 @@ class PocketImport implements ImportInterface
$existingEntry = $this->em $existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->existByUrlAndUserId($url, $this->user->getId()); ->findByUrlAndUserId($url, $this->user->getId());
if (false !== $existingEntry) { if (false !== $existingEntry) {
++$this->skippedEntries; ++$this->skippedEntries;

View file

@ -127,7 +127,7 @@ class WallabagV1Import implements ImportInterface
foreach ($entries as $importedEntry) { foreach ($entries as $importedEntry) {
$existingEntry = $this->em $existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->existByUrlAndUserId($importedEntry['url'], $this->user->getId()); ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
if (false !== $existingEntry) { if (false !== $existingEntry) {
++$this->skippedEntries; ++$this->skippedEntries;

View file

@ -248,7 +248,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase
->getMock(); ->getMock();
$entryRepo->expects($this->exactly(2)) $entryRepo->expects($this->exactly(2))
->method('existByUrlAndUserId') ->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true)); ->will($this->onConsecutiveCalls(false, true));
$tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag') $tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag')

View file

@ -53,7 +53,7 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
->getMock(); ->getMock();
$entryRepo->expects($this->exactly(3)) $entryRepo->expects($this->exactly(3))
->method('existByUrlAndUserId') ->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, false)); ->will($this->onConsecutiveCalls(false, true, false));
$this->em $this->em