|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace AppBundle\Tests\Controller\Admin; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use AppBundle\Entity\Post; |
| 17 | + |
| 18 | +/** |
| 19 | + * Functional test for the controllers defined inside the BlogController used |
| 20 | + * for managing the blog in the backend. |
| 21 | + * See http://symfony.com/doc/current/book/testing.html#functional-tests |
| 22 | + * |
| 23 | + * Whenever you test resources protected by a firewall, consider using the |
| 24 | + * technique explained in: |
| 25 | + * http://symfony.com/doc/current/cookbook/testing/http_authentication.html |
| 26 | + * |
| 27 | + * Execute the application tests using this command (requires PHPUnit to be installed): |
| 28 | + * |
| 29 | + * $ cd your-symfony-project/ |
| 30 | + * $ phpunit -c app |
| 31 | + * |
| 32 | + */ |
| 33 | +class BlogControllerTest extends WebTestCase |
| 34 | +{ |
| 35 | + public function testRegularUsersCannotAccessToTheBackend() |
| 36 | + { |
| 37 | + $client = static::createClient(array(), array( |
| 38 | + 'PHP_AUTH_USER' => 'john_user', |
| 39 | + 'PHP_AUTH_PW' => 'kitten', |
| 40 | + )); |
| 41 | + |
| 42 | + $client->request('GET', '/admin/post/'); |
| 43 | + |
| 44 | + $this->assertEquals(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testAdministratorUsersCanAccessToTheBackend() |
| 48 | + { |
| 49 | + $client = static::createClient(array(), array( |
| 50 | + 'PHP_AUTH_USER' => 'anna_admin', |
| 51 | + 'PHP_AUTH_PW' => 'kitten', |
| 52 | + )); |
| 53 | + |
| 54 | + $client->request('GET', '/admin/post/'); |
| 55 | + |
| 56 | + $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testIndex() |
| 60 | + { |
| 61 | + $client = static::createClient(array(), array( |
| 62 | + 'PHP_AUTH_USER' => 'anna_admin', |
| 63 | + 'PHP_AUTH_PW' => 'kitten', |
| 64 | + )); |
| 65 | + |
| 66 | + $crawler = $client->request('GET', '/admin/post/'); |
| 67 | + |
| 68 | + $this->assertCount( |
| 69 | + Post::NUM_ITEMS, |
| 70 | + $crawler->filter('body#admin_post_index #main tbody tr'), |
| 71 | + 'The backend homepage displays the right number of posts.' |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments