|
11 | 11 |
|
12 | 12 | namespace Tests\AppBundle\Controller\Admin;
|
13 | 13 |
|
| 14 | +use AppBundle\Entity\Post; |
14 | 15 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
15 | 16 | use Symfony\Component\HttpFoundation\Response;
|
16 | 17 |
|
@@ -54,40 +55,69 @@ public function getUrlsForRegularUsers()
|
54 | 55 | }
|
55 | 56 |
|
56 | 57 | /**
|
57 |
| - * @dataProvider getUrlsForAdminUsers |
| 58 | + * @return \Symfony\Bundle\FrameworkBundle\Client |
58 | 59 | */
|
59 |
| - public function testAdminUsers($httpMethod, $url, $statusCode) |
| 60 | + private function getAdminClient() |
60 | 61 | {
|
61 |
| - $client = static::createClient([], [ |
| 62 | + return static::createClient([], [ |
62 | 63 | 'PHP_AUTH_USER' => 'jane_admin',
|
63 | 64 | 'PHP_AUTH_PW' => 'kitten',
|
64 | 65 | ]);
|
65 |
| - |
66 |
| - $client->request($httpMethod, $url); |
67 |
| - $this->assertSame($statusCode, $client->getResponse()->getStatusCode()); |
68 |
| - } |
69 |
| - |
70 |
| - public function getUrlsForAdminUsers() |
71 |
| - { |
72 |
| - yield ['GET', '/en/admin/post/', Response::HTTP_OK]; |
73 |
| - yield ['GET', '/en/admin/post/1', Response::HTTP_OK]; |
74 |
| - yield ['GET', '/en/admin/post/1/edit', Response::HTTP_OK]; |
75 |
| - yield ['POST', '/en/admin/post/1/delete', Response::HTTP_FOUND]; |
76 | 66 | }
|
77 | 67 |
|
78 |
| - public function testBackendHomepage() |
| 68 | + public function testAdminBackendHomePage() |
79 | 69 | {
|
80 |
| - $client = static::createClient([], [ |
81 |
| - 'PHP_AUTH_USER' => 'jane_admin', |
82 |
| - 'PHP_AUTH_PW' => 'kitten', |
83 |
| - ]); |
| 70 | + $client = $this->getAdminClient(); |
84 | 71 |
|
85 | 72 | $crawler = $client->request('GET', '/en/admin/post/');
|
| 73 | + $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
86 | 74 |
|
87 | 75 | $this->assertCount(
|
88 | 76 | 30,
|
89 | 77 | $crawler->filter('body#admin_post_index #main tbody tr'),
|
90 | 78 | 'The backend homepage displays all the available posts.'
|
91 | 79 | );
|
92 | 80 | }
|
| 81 | + |
| 82 | + /** |
| 83 | + * This test changes the database contents by deleting a blog post. However, |
| 84 | + * thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes |
| 85 | + * to the database are rolled back when this test completes. This means that |
| 86 | + * all the application tests begin with the same database contents. |
| 87 | + */ |
| 88 | + public function testAdminDeletePost() |
| 89 | + { |
| 90 | + $client = $this->getAdminClient(); |
| 91 | + $crawler = $client->request('GET', '/en/admin/post/1'); |
| 92 | + $client->submit($crawler->filter('#delete-form')->form()); |
| 93 | + |
| 94 | + $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); |
| 95 | + |
| 96 | + $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); |
| 97 | + $this->assertNull($post); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * This test changes the database contents by editing a blog post. However, |
| 102 | + * thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes |
| 103 | + * to the database are rolled back when this test completes. This means that |
| 104 | + * all the application tests begin with the same database contents. |
| 105 | + */ |
| 106 | + public function testAdminEditPost() |
| 107 | + { |
| 108 | + $newBlogPostTitle = 'Blog Post Title '.mt_rand(); |
| 109 | + |
| 110 | + $client = $this->getAdminClient(); |
| 111 | + $crawler = $client->request('GET', '/en/admin/post/1/edit'); |
| 112 | + $form = $crawler->selectButton('Save changes')->form([ |
| 113 | + 'post[title]' => $newBlogPostTitle, |
| 114 | + ]); |
| 115 | + $client->submit($form); |
| 116 | + |
| 117 | + $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); |
| 118 | + |
| 119 | + /** @var Post $post */ |
| 120 | + $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); |
| 121 | + $this->assertSame($newBlogPostTitle, $post->getTitle()); |
| 122 | + } |
93 | 123 | }
|
0 commit comments