Skip to content

Commit d968017

Browse files
committed
Use dama/doctrine-test-bundle for proper test DB isolation
1 parent 29b6f86 commit d968017

File tree

5 files changed

+110
-24
lines changed

5 files changed

+110
-24
lines changed

app/AppKernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function registerBundles()
3434
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
3535
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
3636
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
37+
38+
if ($this->getEnvironment() === 'test') {
39+
$bundles[] = new \DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
40+
}
3741
}
3842

3943
return $bundles;

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
"white-october/pagerfanta-bundle" : "^1.0"
3232
},
3333
"require-dev": {
34-
"friendsofphp/php-cs-fixer" : "^2.0",
35-
"phpunit/phpunit" : "^4.8 || ^5.0",
36-
"sensio/generator-bundle" : "^3.0",
37-
"symfony/phpunit-bridge" : "^3.0"
34+
"dama/doctrine-test-bundle": "^1.0",
35+
"friendsofphp/php-cs-fixer": "^2.0",
36+
"phpunit/phpunit": "^4.8 || ^5.0",
37+
"sensio/generator-bundle": "^3.0",
38+
"symfony/phpunit-bridge": "^3.0"
3839
},
3940
"scripts": {
4041
"symfony-scripts": [

composer.lock

Lines changed: 55 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
</exclude>
2929
</whitelist>
3030
</filter>
31+
32+
<listeners>
33+
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" file="vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/PHPUnit/PHPUnitStaticDbConnectionListener.php" />
34+
</listeners>
3135
</phpunit>

tests/AppBundle/Controller/Admin/BlogControllerTest.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Tests\AppBundle\Controller\Admin;
1313

14+
use AppBundle\Entity\Post;
1415
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1516
use Symfony\Component\HttpFoundation\Response;
1617

@@ -54,40 +55,62 @@ public function getUrlsForRegularUsers()
5455
}
5556

5657
/**
57-
* @dataProvider getUrlsForAdminUsers
58+
* @return \Symfony\Bundle\FrameworkBundle\Client
5859
*/
59-
public function testAdminUsers($httpMethod, $url, $statusCode)
60+
private function getAdminClient()
6061
{
61-
$client = static::createClient([], [
62+
return static::createClient([], [
6263
'PHP_AUTH_USER' => 'jane_admin',
6364
'PHP_AUTH_PW' => 'kitten',
6465
]);
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];
7666
}
7767

78-
public function testBackendHomepage()
68+
public function testAdminBackendHomePage()
7969
{
80-
$client = static::createClient([], [
81-
'PHP_AUTH_USER' => 'jane_admin',
82-
'PHP_AUTH_PW' => 'kitten',
83-
]);
70+
$client = $this->getAdminClient();
8471

8572
$crawler = $client->request('GET', '/en/admin/post/');
73+
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
8674

8775
$this->assertCount(
8876
30,
8977
$crawler->filter('body#admin_post_index #main tbody tr'),
9078
'The backend homepage displays all the available posts.'
9179
);
9280
}
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+
}
93116
}

0 commit comments

Comments
 (0)