Skip to content

Commit 2d4b409

Browse files
committed
Added some functional tests for the backend
1 parent 27a175e commit 2d4b409

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 <[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

Comments
 (0)