From 7ab5eb9508921d84b4b4ec84a59135d536da748e Mon Sep 17 00:00:00 2001 From: adev Date: Mon, 15 May 2017 20:47:59 +0200 Subject: Isolated tests Use https://github.com/dmaicher/doctrine-test-bundle to have test isolation. --- .../Controller/DeveloperControllerTest.php | 40 +++++++++++++++------- .../Controller/EntryRestControllerTest.php | 37 +++++++++++++------- .../ApiBundle/Controller/TagRestControllerTest.php | 33 +++++++++--------- tests/Wallabag/ApiBundle/WallabagApiTestCase.php | 4 +-- 4 files changed, 71 insertions(+), 43 deletions(-) (limited to 'tests/Wallabag/ApiBundle') diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php index 6659443b..d37cbbf9 100644 --- a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php @@ -3,6 +3,7 @@ namespace Tests\Wallabag\ApiBundle\Controller; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; +use Wallabag\ApiBundle\Entity\Client; class DeveloperControllerTest extends WallabagCoreTestCase { @@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase $this->assertContains('My app', $alert[0]); } - /** - * @depends testCreateClient - */ public function testCreateToken() { $client = $this->getClient(); - $em = $client->getContainer()->get('doctrine.orm.entity_manager'); - $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app'); + $apiClient = $this->createApiClientForUser('admin'); $client->request('POST', '/oauth/v2/token', [ 'grant_type' => 'password', @@ -83,6 +80,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase public function testRemoveClient() { $client = $this->getClient(); + $adminApiClient = $this->createApiClientForUser('admin'); $em = $client->getContainer()->get('doctrine.orm.entity_manager'); // Try to remove an admin's client with a wrong user @@ -90,12 +88,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase $client->request('GET', '/developer'); $this->assertContains('no_client', $client->getResponse()->getContent()); - // get an ID of a admin's client - $this->logInAs('admin'); - $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); - $this->logInAs('bob'); - $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId()); + $client->request('GET', '/developer/client/delete/'.$adminApiClient->getId()); $this->assertEquals(403, $client->getResponse()->getStatusCode()); // Try to remove the admin's client with the good user @@ -111,7 +105,29 @@ class DeveloperControllerTest extends WallabagCoreTestCase $client->click($link); $this->assertEquals(302, $client->getResponse()->getStatusCode()); - $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); - $this->assertGreaterThan(count($newNbClients), count($nbClients)); + $this->assertNull( + $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()), + 'The client should have been removed' + ); + } + + /** + * @param string $username + * + * @return Client + */ + private function createApiClientForUser($username) + { + $client = $this->getClient(); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + $userManager = $client->getContainer()->get('fos_user.user_manager'); + $user = $userManager->findUserBy(array('username' => $username)); + $apiClient = new Client($user); + $apiClient->setName('My app'); + $apiClient->setAllowedGrantTypes(['password']); + $em->persist($apiClient); + $em->flush(); + + return $apiClient; } } diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index bf7d373a..1ecd03fb 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -3,8 +3,10 @@ namespace Tests\Wallabag\ApiBundle\Controller; use Tests\Wallabag\ApiBundle\WallabagApiTestCase; +use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Helper\ContentProxy; +use Wallabag\UserBundle\Entity\User; class EntryRestControllerTest extends WallabagApiTestCase { @@ -801,22 +803,28 @@ class EntryRestControllerTest extends WallabagApiTestCase public function testDeleteEntriesTagsListAction() { - $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:Entry') - ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + $entry = new Entry($em->getReference(User::class, 1)); + $entry->setUrl('http://0.0.0.0/test-entry'); + $entry->addTag((new Tag())->setLabel('foo-tag')); + $entry->addTag((new Tag())->setLabel('bar-tag')); + $em->persist($entry); + $em->flush(); - $tags = $entry->getTags(); - - $this->assertCount(4, $tags); + $em->clear(); $list = [ [ - 'url' => 'http://0.0.0.0/entry4', - 'tags' => 'new tag 1, new tag 2', + 'url' => 'http://0.0.0.0/test-entry', + 'tags' => 'foo-tag, bar-tag', ], ]; $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); + $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + + $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); + $this->assertCount(0, $entry->getTags()); } public function testPostEntriesListAction() @@ -841,9 +849,14 @@ class EntryRestControllerTest extends WallabagApiTestCase public function testDeleteEntriesListAction() { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1')); + + $em->flush(); + $em->clear(); $list = [ - 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', - 'http://0.0.0.0/entry3', + 'http://0.0.0.0/test-entry1', + 'http://0.0.0.0/test-entry-not-exist', ]; $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); @@ -853,10 +866,10 @@ class EntryRestControllerTest extends WallabagApiTestCase $content = json_decode($this->client->getResponse()->getContent(), true); $this->assertTrue($content[0]['entry']); - $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); + $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']); $this->assertFalse($content[1]['entry']); - $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); + $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); } public function testLimitBulkAction() diff --git a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php index 90b132eb..7f69bd67 100644 --- a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php @@ -22,36 +22,35 @@ class TagRestControllerTest extends WallabagApiTestCase return end($content); } - /** - * @depends testGetUserTags - */ - public function testDeleteUserTag($tag) + public function testDeleteUserTag() { - $tagName = $tag['label']; + $tagLabel = 'tagtest'; + $tag = new Tag(); + $tag->setLabel($tagLabel); + + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + $em->persist($tag); + $em->flush(); + $em->clear(); - $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); + $this->client->request('DELETE', '/api/tags/'.$tag->getId().'.json'); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); $this->assertArrayHasKey('label', $content); - $this->assertEquals($tag['label'], $content['label']); - $this->assertEquals($tag['slug'], $content['slug']); + $this->assertEquals($tag->getLabel(), $content['label']); + $this->assertEquals($tag->getSlug(), $content['slug']); - $entries = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:Entry') - ->findAllByTagId($this->user->getId(), $tag['id']); + $entries = $em->getRepository('WallabagCoreBundle:Entry') + ->findAllByTagId($this->user->getId(), $tag->getId()); $this->assertCount(0, $entries); - $tag = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabel($tagName); + $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); - $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag'); + $this->assertNull($tag, $tagLabel.' was removed because it begun an orphan tag'); } public function dataForDeletingTagByLabel() diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php index a67655c8..8709da70 100644 --- a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php +++ b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php @@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie; abstract class WallabagApiTestCase extends WebTestCase { /** - * @var Client + * @var \Symfony\Bundle\FrameworkBundle\Client */ protected $client = null; @@ -23,7 +23,7 @@ abstract class WallabagApiTestCase extends WebTestCase } /** - * @return Client + * @return \Symfony\Bundle\FrameworkBundle\Client */ protected function createAuthorizedClient() { -- cgit v1.2.3