diff --git a/tests/AppBundle/Controller/Admin/BlogControllerTest.php b/tests/AppBundle/Controller/Admin/BlogControllerTest.php index a5093b96e..4f697cba3 100644 --- a/tests/AppBundle/Controller/Admin/BlogControllerTest.php +++ b/tests/AppBundle/Controller/Admin/BlogControllerTest.php @@ -14,7 +14,6 @@ use AppBundle\Entity\Post; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; -use Tests\ControllerTestTrait; use Tests\FixturesTrait; /** @@ -34,7 +33,6 @@ */ class BlogControllerTest extends WebTestCase { - use ControllerTestTrait; use FixturesTrait; /** @@ -42,7 +40,10 @@ class BlogControllerTest extends WebTestCase */ public function testAccessDeniedForRegularUsers($httpMethod, $url) { - $client = $this->getUserClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'john_user', + 'PHP_AUTH_PW' => 'kitten', + ]); $client->request($httpMethod, $url); $this->assertSame(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode()); @@ -58,7 +59,10 @@ public function getUrlsForRegularUsers() public function testAdminBackendHomePage() { - $client = $this->getAdminClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'jane_admin', + 'PHP_AUTH_PW' => 'kitten', + ]); $crawler = $client->request('GET', '/en/admin/post/'); $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); @@ -82,7 +86,10 @@ public function testAdminNewPost() $postSummary = $this->getRandomPostSummary(); $postContent = $this->getPostContent(); - $client = $this->getAdminClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'jane_admin', + 'PHP_AUTH_PW' => 'kitten', + ]); $crawler = $client->request('GET', '/en/admin/post/new'); $form = $crawler->selectButton('Create post')->form([ 'post[title]' => $postTitle, @@ -103,7 +110,10 @@ public function testAdminNewPost() public function testAdminShowPost() { - $client = $this->getAdminClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'jane_admin', + 'PHP_AUTH_PW' => 'kitten', + ]); $client->request('GET', '/en/admin/post/1'); $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); @@ -119,7 +129,10 @@ public function testAdminEditPost() { $newBlogPostTitle = 'Blog Post Title '.mt_rand(); - $client = $this->getAdminClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'jane_admin', + 'PHP_AUTH_PW' => 'kitten', + ]); $crawler = $client->request('GET', '/en/admin/post/1/edit'); $form = $crawler->selectButton('Save changes')->form([ 'post[title]' => $newBlogPostTitle, @@ -141,7 +154,10 @@ public function testAdminEditPost() */ public function testAdminDeletePost() { - $client = $this->getAdminClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'jane_admin', + 'PHP_AUTH_PW' => 'kitten', + ]); $crawler = $client->request('GET', '/en/admin/post/1'); $client->submit($crawler->filter('#delete-form')->form()); diff --git a/tests/AppBundle/Controller/BlogControllerTest.php b/tests/AppBundle/Controller/BlogControllerTest.php index 31c95a9fe..308b2ff87 100644 --- a/tests/AppBundle/Controller/BlogControllerTest.php +++ b/tests/AppBundle/Controller/BlogControllerTest.php @@ -14,7 +14,6 @@ use AppBundle\Entity\Post; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; -use Tests\ControllerTestTrait; use Tests\FixturesTrait; /** @@ -29,12 +28,11 @@ */ class BlogControllerTest extends WebTestCase { - use ControllerTestTrait; use FixturesTrait; public function testIndex() { - $client = $this->getAnonymousClient(); + $client = static::createClient(); $crawler = $client->request('GET', '/en/blog/'); $this->assertCount( @@ -46,7 +44,7 @@ public function testIndex() public function testRss() { - $client = $this->getAnonymousClient(); + $client = static::createClient(); $crawler = $client->request('GET', '/en/blog/rss.xml'); $this->assertSame( @@ -69,7 +67,10 @@ public function testRss() */ public function testNewComment() { - $client = $this->getUserClient(); + $client = static::createClient([], [ + 'PHP_AUTH_USER' => 'john_user', + 'PHP_AUTH_PW' => 'kitten', + ]); /** @var Post $post */ $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); @@ -85,7 +86,8 @@ public function testNewComment() $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); - // The first one is the last inserted (See descending order of comments association). + // The first one is the most recent comment because of the automatic sorting + // defined in the comments association of the Post entity $comment = $post->getComments()->first(); $this->assertSame($commentsCount + 1, $post->getComments()->count()); diff --git a/tests/AppBundle/Controller/DefaultControllerTest.php b/tests/AppBundle/Controller/DefaultControllerTest.php index e63e4b93a..ea597d884 100644 --- a/tests/AppBundle/Controller/DefaultControllerTest.php +++ b/tests/AppBundle/Controller/DefaultControllerTest.php @@ -14,7 +14,6 @@ use AppBundle\Entity\Post; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Response; -use Tests\ControllerTestTrait; /** * Functional test that implements a "smoke test" of all the public and secure @@ -28,8 +27,6 @@ */ class DefaultControllerTest extends WebTestCase { - use ControllerTestTrait; - /** * PHPUnit's data providers allow to execute the same tests repeated times * using a different set of data each time. @@ -39,7 +36,7 @@ class DefaultControllerTest extends WebTestCase */ public function testPublicUrls($url) { - $client = $this->getAnonymousClient(); + $client = static::createClient(); $client->request('GET', $url); $this->assertSame( @@ -58,8 +55,8 @@ public function testPublicUrls($url) */ public function testPublicBlogPost() { - // the service container is always available via the client - $client = $this->getAnonymousClient(); + $client = static::createClient(); + // the service container is always available via the test client $blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); $client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug())); @@ -75,7 +72,7 @@ public function testPublicBlogPost() */ public function testSecureUrls($url) { - $client = $this->getAnonymousClient(); + $client = static::createClient(); $client->request('GET', $url); $response = $client->getResponse(); diff --git a/tests/ControllerTestTrait.php b/tests/ControllerTestTrait.php deleted file mode 100644 index 5039f23ff..000000000 --- a/tests/ControllerTestTrait.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Tests; - -/** - * Helper class to create users clients. - */ -trait ControllerTestTrait -{ - /** - * @return \Symfony\Bundle\FrameworkBundle\Client - */ - private function getAdminClient() - { - return self::createClient([], [ - 'PHP_AUTH_USER' => 'jane_admin', - 'PHP_AUTH_PW' => 'kitten', - ]); - } - - /** - * @return \Symfony\Bundle\FrameworkBundle\Client - */ - private function getUserClient() - { - return self::createClient([], [ - 'PHP_AUTH_USER' => 'john_user', - 'PHP_AUTH_PW' => 'kitten', - ]); - } - - /** - * @return \Symfony\Bundle\FrameworkBundle\Client - */ - private function getAnonymousClient() - { - return self::createClient(); - } -}