|
11 | 11 |
|
12 | 12 | namespace Tests\AppBundle\Controller;
|
13 | 13 |
|
14 |
| -use AppBundle\DataFixtures\FixturesTrait; |
15 | 14 | use AppBundle\Entity\Post;
|
16 | 15 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
17 |
| -use Symfony\Component\HttpFoundation\Response; |
18 | 16 |
|
19 | 17 | /**
|
20 | 18 | * Functional test for the controllers defined inside BlogController.
|
|
28 | 26 | */
|
29 | 27 | class BlogControllerTest extends WebTestCase
|
30 | 28 | {
|
31 |
| - use FixturesTrait; |
32 |
| - |
33 | 29 | public function testIndex()
|
34 | 30 | {
|
35 | 31 | $client = static::createClient();
|
@@ -71,26 +67,21 @@ public function testNewComment()
|
71 | 67 | 'PHP_AUTH_USER' => 'john_user',
|
72 | 68 | 'PHP_AUTH_PW' => 'kitten',
|
73 | 69 | ]);
|
| 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(); |
74 | 75 |
|
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); |
79 | 77 |
|
80 |
| - $crawler = $client->request('GET', '/en/blog/posts/'.$post->getSlug()); |
81 | 78 | $form = $crawler->selectButton('Publish comment')->form([
|
82 |
| - 'comment[content]' => $commentContent, |
| 79 | + 'comment[content]' => 'Hi, Symfony!', |
83 | 80 | ]);
|
84 |
| - $client->submit($form); |
85 |
| - |
86 |
| - $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); |
| 81 | + $crawler = $client->submit($form); |
87 | 82 |
|
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(); |
92 | 84 |
|
93 |
| - $this->assertSame($commentsCount + 1, $post->getComments()->count()); |
94 |
| - $this->assertSame($commentContent, $comment->getContent()); |
| 85 | + $this->assertSame('Hi, Symfony!', $newComment); |
95 | 86 | }
|
96 | 87 | }
|
0 commit comments