Skip to content

Commit afc881f

Browse files
committed
minor #503 Removed ControllerTestTrait (javiereguiluz)
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
2 parents 8d42076 + 17f9a10 commit afc881f

File tree

4 files changed

+36
-69
lines changed

4 files changed

+36
-69
lines changed

tests/AppBundle/Controller/Admin/BlogControllerTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use AppBundle\Entity\Post;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Tests\ControllerTestTrait;
1817
use Tests\FixturesTrait;
1918

2019
/**
@@ -34,15 +33,17 @@
3433
*/
3534
class BlogControllerTest extends WebTestCase
3635
{
37-
use ControllerTestTrait;
3836
use FixturesTrait;
3937

4038
/**
4139
* @dataProvider getUrlsForRegularUsers
4240
*/
4341
public function testAccessDeniedForRegularUsers($httpMethod, $url)
4442
{
45-
$client = $this->getUserClient();
43+
$client = static::createClient([], [
44+
'PHP_AUTH_USER' => 'john_user',
45+
'PHP_AUTH_PW' => 'kitten',
46+
]);
4647

4748
$client->request($httpMethod, $url);
4849
$this->assertSame(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode());
@@ -58,7 +59,10 @@ public function getUrlsForRegularUsers()
5859

5960
public function testAdminBackendHomePage()
6061
{
61-
$client = $this->getAdminClient();
62+
$client = static::createClient([], [
63+
'PHP_AUTH_USER' => 'jane_admin',
64+
'PHP_AUTH_PW' => 'kitten',
65+
]);
6266

6367
$crawler = $client->request('GET', '/en/admin/post/');
6468
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
@@ -82,7 +86,10 @@ public function testAdminNewPost()
8286
$postSummary = $this->getRandomPostSummary();
8387
$postContent = $this->getPostContent();
8488

85-
$client = $this->getAdminClient();
89+
$client = static::createClient([], [
90+
'PHP_AUTH_USER' => 'jane_admin',
91+
'PHP_AUTH_PW' => 'kitten',
92+
]);
8693
$crawler = $client->request('GET', '/en/admin/post/new');
8794
$form = $crawler->selectButton('Create post')->form([
8895
'post[title]' => $postTitle,
@@ -103,7 +110,10 @@ public function testAdminNewPost()
103110

104111
public function testAdminShowPost()
105112
{
106-
$client = $this->getAdminClient();
113+
$client = static::createClient([], [
114+
'PHP_AUTH_USER' => 'jane_admin',
115+
'PHP_AUTH_PW' => 'kitten',
116+
]);
107117
$client->request('GET', '/en/admin/post/1');
108118

109119
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
@@ -119,7 +129,10 @@ public function testAdminEditPost()
119129
{
120130
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
121131

122-
$client = $this->getAdminClient();
132+
$client = static::createClient([], [
133+
'PHP_AUTH_USER' => 'jane_admin',
134+
'PHP_AUTH_PW' => 'kitten',
135+
]);
123136
$crawler = $client->request('GET', '/en/admin/post/1/edit');
124137
$form = $crawler->selectButton('Save changes')->form([
125138
'post[title]' => $newBlogPostTitle,
@@ -141,7 +154,10 @@ public function testAdminEditPost()
141154
*/
142155
public function testAdminDeletePost()
143156
{
144-
$client = $this->getAdminClient();
157+
$client = static::createClient([], [
158+
'PHP_AUTH_USER' => 'jane_admin',
159+
'PHP_AUTH_PW' => 'kitten',
160+
]);
145161
$crawler = $client->request('GET', '/en/admin/post/1');
146162
$client->submit($crawler->filter('#delete-form')->form());
147163

tests/AppBundle/Controller/BlogControllerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use AppBundle\Entity\Post;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Tests\ControllerTestTrait;
1817
use Tests\FixturesTrait;
1918

2019
/**
@@ -29,12 +28,11 @@
2928
*/
3029
class BlogControllerTest extends WebTestCase
3130
{
32-
use ControllerTestTrait;
3331
use FixturesTrait;
3432

3533
public function testIndex()
3634
{
37-
$client = $this->getAnonymousClient();
35+
$client = static::createClient();
3836
$crawler = $client->request('GET', '/en/blog/');
3937

4038
$this->assertCount(
@@ -46,7 +44,7 @@ public function testIndex()
4644

4745
public function testRss()
4846
{
49-
$client = $this->getAnonymousClient();
47+
$client = static::createClient();
5048
$crawler = $client->request('GET', '/en/blog/rss.xml');
5149

5250
$this->assertSame(
@@ -69,7 +67,10 @@ public function testRss()
6967
*/
7068
public function testNewComment()
7169
{
72-
$client = $this->getUserClient();
70+
$client = static::createClient([], [
71+
'PHP_AUTH_USER' => 'john_user',
72+
'PHP_AUTH_PW' => 'kitten',
73+
]);
7374

7475
/** @var Post $post */
7576
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
@@ -85,7 +86,8 @@ public function testNewComment()
8586
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
8687

8788
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
88-
// The first one is the last inserted (See descending order of comments association).
89+
// The first one is the most recent comment because of the automatic sorting
90+
// defined in the comments association of the Post entity
8991
$comment = $post->getComments()->first();
9092

9193
$this->assertSame($commentsCount + 1, $post->getComments()->count());

tests/AppBundle/Controller/DefaultControllerTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use AppBundle\Entity\Post;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Tests\ControllerTestTrait;
1817

1918
/**
2019
* Functional test that implements a "smoke test" of all the public and secure
@@ -28,8 +27,6 @@
2827
*/
2928
class DefaultControllerTest extends WebTestCase
3029
{
31-
use ControllerTestTrait;
32-
3330
/**
3431
* PHPUnit's data providers allow to execute the same tests repeated times
3532
* using a different set of data each time.
@@ -39,7 +36,7 @@ class DefaultControllerTest extends WebTestCase
3936
*/
4037
public function testPublicUrls($url)
4138
{
42-
$client = $this->getAnonymousClient();
39+
$client = static::createClient();
4340
$client->request('GET', $url);
4441

4542
$this->assertSame(
@@ -58,8 +55,8 @@ public function testPublicUrls($url)
5855
*/
5956
public function testPublicBlogPost()
6057
{
61-
// the service container is always available via the client
62-
$client = $this->getAnonymousClient();
58+
$client = static::createClient();
59+
// the service container is always available via the test client
6360
$blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
6461
$client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug()));
6562

@@ -75,7 +72,7 @@ public function testPublicBlogPost()
7572
*/
7673
public function testSecureUrls($url)
7774
{
78-
$client = $this->getAnonymousClient();
75+
$client = static::createClient();
7976
$client->request('GET', $url);
8077

8178
$response = $client->getResponse();

tests/ControllerTestTrait.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)