Skip to content

Commit 67fa51c

Browse files
committed
minor #555 Simplify comment creating functional test (voronkovich)
This PR was squashed before being merged into the master branch (closes #555). Discussion ---------- Simplify comment creating functional test Current test is not truly "functional" because it knows too much about internal structure of an application. @dmaicher, this test pass only when I disable the `PHPUnitStaticDbConnectionListener`. Could you please look at the code? Commits ------- 959f910 Simplify comment creating functional test
2 parents 1f76214 + 959f910 commit 67fa51c

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/AppBundle/Controller/BlogControllerTest.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
namespace Tests\AppBundle\Controller;
1313

14-
use AppBundle\DataFixtures\FixturesTrait;
1514
use AppBundle\Entity\Post;
1615
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17-
use Symfony\Component\HttpFoundation\Response;
1816

1917
/**
2018
* Functional test for the controllers defined inside BlogController.
@@ -28,8 +26,6 @@
2826
*/
2927
class BlogControllerTest extends WebTestCase
3028
{
31-
use FixturesTrait;
32-
3329
public function testIndex()
3430
{
3531
$client = static::createClient();
@@ -71,26 +67,21 @@ public function testNewComment()
7167
'PHP_AUTH_USER' => 'john_user',
7268
'PHP_AUTH_PW' => 'kitten',
7369
]);
70+
$client->followRedirects();
71+
72+
// Find first blog post
73+
$crawler = $client->request('GET', '/en/blog/');
74+
$postLink = $crawler->filter('article.post > h2 a')->link();
7475

75-
/** @var Post $post */
76-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
77-
$commentContent = $this->getRandomCommentContent();
78-
$commentsCount = $post->getComments()->count();
76+
$crawler = $client->click($postLink);
7977

80-
$crawler = $client->request('GET', '/en/blog/posts/'.$post->getSlug());
8178
$form = $crawler->selectButton('Publish comment')->form([
82-
'comment[content]' => $commentContent,
79+
'comment[content]' => 'Hi, Symfony!',
8380
]);
84-
$client->submit($form);
85-
86-
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
81+
$crawler = $client->submit($form);
8782

88-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
89-
// The first one is the most recent comment because of the automatic sorting
90-
// defined in the comments association of the Post entity
91-
$comment = $post->getComments()->first();
83+
$newComment = $crawler->filter('.post-comment')->first()->filter('div > p')->text();
9284

93-
$this->assertSame($commentsCount + 1, $post->getComments()->count());
94-
$this->assertSame($commentContent, $comment->getContent());
85+
$this->assertSame('Hi, Symfony!', $newComment);
9586
}
9687
}

0 commit comments

Comments
 (0)