Skip to content

Commit

Permalink
minor #503 Removed ControllerTestTrait (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

Removed ControllerTestTrait

In #481, @yceruto introduced some nice features. I like them all ... but one of them is not compatible with the way Symfony promotes to do things. The `ControllerTestTrait` is a PHP trait with some helper methods to reduce the boilerplate code in tests. Even if the solution is technically correct, we don't use it in the official Symfony docs. This can create confusion for new developers studying this app and reading the docs.

I propose to revert this feature in this PR ... and later we can discuss about ways to improve this without using traits. Thanks!

Commits
-------

17f9a10 Removed ControllerTestTrait
  • Loading branch information
javiereguiluz committed Mar 15, 2017
2 parents 8d42076 + 17f9a10 commit afc881f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 69 deletions.
32 changes: 24 additions & 8 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Expand Up @@ -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;

/**
Expand All @@ -34,15 +33,17 @@
*/
class BlogControllerTest extends WebTestCase
{
use ControllerTestTrait;
use FixturesTrait;

/**
* @dataProvider getUrlsForRegularUsers
*/
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());
Expand All @@ -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());
Expand All @@ -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,
Expand All @@ -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());
Expand All @@ -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,
Expand All @@ -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());

Expand Down
14 changes: 8 additions & 6 deletions tests/AppBundle/Controller/BlogControllerTest.php
Expand Up @@ -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;

/**
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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());
Expand Down
11 changes: 4 additions & 7 deletions tests/AppBundle/Controller/DefaultControllerTest.php
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -39,7 +36,7 @@ class DefaultControllerTest extends WebTestCase
*/
public function testPublicUrls($url)
{
$client = $this->getAnonymousClient();
$client = static::createClient();
$client->request('GET', $url);

$this->assertSame(
Expand All @@ -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()));

Expand All @@ -75,7 +72,7 @@ public function testPublicBlogPost()
*/
public function testSecureUrls($url)
{
$client = $this->getAnonymousClient();
$client = static::createClient();
$client->request('GET', $url);

$response = $client->getResponse();
Expand Down
48 changes: 0 additions & 48 deletions tests/ControllerTestTrait.php

This file was deleted.

0 comments on commit afc881f

Please sign in to comment.