Skip to content

Commit f0b76fd

Browse files
committed
feature symfony#23 Added some functional tests for the backend (javiereguiluz)
This PR was squashed before being merged into the master branch (closes symfony#23). Discussion ---------- Added some functional tests for the backend This allows to explain how to test resources protected by a firewall and the trick to change the firewall configuration just for the test environment. Commits ------- 2d4b409 Added some functional tests for the backend
2 parents 1bc2e9a + 2d4b409 commit f0b76fd

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

app/config/config_test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ web_profiler:
1414

1515
swiftmailer:
1616
disable_delivery: true
17+
18+
# It's recommended to use a separate database for tests. This allows to have a
19+
# fixed and known set of data fixtures, it simplifies the code of tests and it
20+
# makes them more robust.
21+
# In this case we just need to define a different path for the application database.
22+
doctrine:
23+
dbal:
24+
path: "%kernel.root_dir%/data/blog_test.sqlite"
25+
26+
# this configuration simplifies testing URLs protected by the security mechanism
27+
# See http://symfony.com/doc/current/cookbook/testing/http_authentication.html
28+
security:
29+
firewalls:
30+
secured_area:
31+
http_basic: ~

app/data/blog_test.sqlite

58 KB
Binary file not shown.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy