|
12 | 12 | namespace AppBundle\Tests\Controller;
|
13 | 13 |
|
14 | 14 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
15 |
| -use AppBundle\Entity\Post; |
16 | 15 |
|
17 | 16 | /**
|
18 |
| - * Functional test for the DefaultController methods. |
19 |
| - * See http://symfony.com/doc/current/book/testing.html#functional-tests |
| 17 | + * Functional test that implements a "smoke test" of all the public and secure |
| 18 | + * URLs of the application. |
| 19 | + * See http://symfony.com/doc/current/best_practices/tests.html#functional-tests. |
20 | 20 | *
|
21 | 21 | * Execute the application tests using this command (requires PHPUnit to be installed):
|
22 |
| - * $ cd your-symfony-project/ |
23 |
| - * $ phpunit -c app |
24 | 22 | *
|
25 |
| - * @author Ryan Weaver <weaverryan@gmail.com> |
26 |
| - * @author Javier Eguiluz <javier.eguiluz@gmail.com> |
| 23 | + * $ cd your-symfony-project/ |
| 24 | + * $ phpunit -c app |
| 25 | + * |
27 | 26 | */
|
28 | 27 | class DefaultControllerTest extends WebTestCase
|
29 | 28 | {
|
30 |
| - public function testIndex() |
| 29 | + /** |
| 30 | + * PHPUnit's data providers allow to execute the same tests repeated times |
| 31 | + * using a different set of data each time. |
| 32 | + * See http://symfony.com/doc/current/cookbook/form/unit_testing.html#testing-against-different-sets-of-data. |
| 33 | + * |
| 34 | + * @dataProvider getPublicUrls |
| 35 | + */ |
| 36 | + public function testPublicUrls($url) |
| 37 | + { |
| 38 | + $client = self::createClient(); |
| 39 | + $client->request('GET', $url); |
| 40 | + |
| 41 | + $this->assertTrue( |
| 42 | + $client->getResponse()->isSuccessful(), |
| 43 | + sprintf('The %s public URL loads correctly.', $url) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The application contains a lot of secure URLs which shouldn't be |
| 49 | + * publicly accessible. This tests ensures that whenever a user tries to |
| 50 | + * access one of those pages, a redirection to the login form is performed. |
| 51 | + * |
| 52 | + * @dataProvider getSecureUrls |
| 53 | + */ |
| 54 | + public function testSecureUrls($url) |
31 | 55 | {
|
32 |
| - $client = static::createClient(); |
33 |
| - $crawler = $client->request('GET', '/blog/'); |
| 56 | + $client = self::createClient(); |
| 57 | + $client->request('GET', $url); |
34 | 58 |
|
35 |
| - $this->assertEquals(Post::NUM_ITEMS, $crawler->filter('article.post')->count(), |
36 |
| - 'The homepage displayes the right number of posts' |
| 59 | + $this->assertTrue($client->getResponse()->isRedirect()); |
| 60 | + |
| 61 | + $this->assertEquals( |
| 62 | + 'http://localhost/login', |
| 63 | + $client->getResponse()->getTargetUrl(), |
| 64 | + sprintf('The %s secure URL redirects to the login form.', $url) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function getPublicUrls() |
| 69 | + { |
| 70 | + return array( |
| 71 | + array('/'), |
| 72 | + array('/blog/'), |
| 73 | + array('/blog/posts/morbi-tempus-commodo-mattis'), |
| 74 | + array('/login'), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function getSecureUrls() |
| 79 | + { |
| 80 | + return array( |
| 81 | + array('/admin/post/'), |
| 82 | + array('/admin/post/new'), |
| 83 | + array('/admin/post/1'), |
| 84 | + array('/admin/post/1/edit'), |
37 | 85 | );
|
38 | 86 | }
|
39 | 87 | }
|
0 commit comments