Skip to content

Commit 291a62e

Browse files
committed
feature #459 Use dama/doctrine-test-bundle for proper test DB isolation (dmaicher, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Use dama/doctrine-test-bundle for proper test DB isolation This fixes #445 by using https://github.com/dmaicher/doctrine-test-bundle to have proper test isolation. Commits ------- 3a94540 Fixed a typo 172ce3d Tweaks 0fa427c add some comments about the usage of the bundle fc1b7c3 remove file path from listener config d968017 Use dama/doctrine-test-bundle for proper test DB isolation
2 parents e9589c1 + 3a94540 commit 291a62e

File tree

6 files changed

+118
-21
lines changed

6 files changed

+118
-21
lines changed

app/AppKernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ 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 ('test' === $this->getEnvironment()) {
39+
// this bundle makes it easier to work with databases in PHPUnit
40+
// tests, so it's only loaded for the 'test' environment
41+
$bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
42+
}
3743
}
3844

3945
return $bundles;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ include('blog/_delete_post_confirmation.html.twig') }}
2-
<form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post" data-confirmation="true">
2+
<form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post" data-confirmation="true" id="delete-form">
33
<input type="hidden" name="token" value="{{ csrf_token('delete') }}" />
44
<input type="submit" value="{{ 'action.delete_post'|trans }}" class="btn btn-lg btn-block btn-danger" />
55
</form>

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"white-october/pagerfanta-bundle" : "^1.0"
3232
},
3333
"require-dev": {
34+
"dama/doctrine-test-bundle" : "^1.0",
3435
"friendsofphp/php-cs-fixer" : "^2.0",
3536
"phpunit/phpunit" : "^4.8 || ^5.0",
3637
"sensio/generator-bundle" : "^3.0",

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@
2828
</exclude>
2929
</whitelist>
3030
</filter>
31+
32+
<listeners>
33+
<!-- it begins a database transaction before every testcase and rolls it back after
34+
the test finished, so tests can manipulate the database without affecting other tests -->
35+
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" />
36+
</listeners>
3137
</phpunit>

tests/AppBundle/Controller/Admin/BlogControllerTest.php

Lines changed: 49 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,69 @@ 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+
/**
83+
* This test changes the database contents by deleting a blog post. However,
84+
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
85+
* to the database are rolled back when this test completes. This means that
86+
* all the application tests begin with the same database contents.
87+
*/
88+
public function testAdminDeletePost()
89+
{
90+
$client = $this->getAdminClient();
91+
$crawler = $client->request('GET', '/en/admin/post/1');
92+
$client->submit($crawler->filter('#delete-form')->form());
93+
94+
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
95+
96+
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
97+
$this->assertNull($post);
98+
}
99+
100+
/**
101+
* This test changes the database contents by editing a blog post. However,
102+
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
103+
* to the database are rolled back when this test completes. This means that
104+
* all the application tests begin with the same database contents.
105+
*/
106+
public function testAdminEditPost()
107+
{
108+
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
109+
110+
$client = $this->getAdminClient();
111+
$crawler = $client->request('GET', '/en/admin/post/1/edit');
112+
$form = $crawler->selectButton('Save changes')->form([
113+
'post[title]' => $newBlogPostTitle,
114+
]);
115+
$client->submit($form);
116+
117+
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
118+
119+
/** @var Post $post */
120+
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
121+
$this->assertSame($newBlogPostTitle, $post->getTitle());
122+
}
93123
}

0 commit comments

Comments
 (0)