|
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,62 @@ 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 | + public function testAdminDeletePost() |
| 83 | + { |
| 84 | + $client = $this->getAdminClient(); |
| 85 | + |
| 86 | + $crawler = $client->request('GET', '/en/admin/post/1'); |
| 87 | + |
| 88 | + $client->submit($crawler->filter('form')->form()); |
| 89 | + |
| 90 | + $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); |
| 91 | + |
| 92 | + $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); |
| 93 | + $this->assertNull($post); |
| 94 | + } |
| 95 | + |
| 96 | + public function testAdminEditPost() |
| 97 | + { |
| 98 | + $client = $this->getAdminClient(); |
| 99 | + |
| 100 | + $crawler = $client->request('GET', '/en/admin/post/1/edit'); |
| 101 | + |
| 102 | + $newTitle = 'what a nice new title!'; |
| 103 | + |
| 104 | + $form = $crawler->filter('form')->form([ |
| 105 | + 'post[title]' => $newTitle, |
| 106 | + ]); |
| 107 | + |
| 108 | + $client->submit($form); |
| 109 | + |
| 110 | + $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode()); |
| 111 | + |
| 112 | + /** @var Post $post */ |
| 113 | + $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); |
| 114 | + $this->assertSame($newTitle, $post->getTitle()); |
| 115 | + } |
93 | 116 | } |
0 commit comments