Skip to content

Commit 172ce3d

Browse files
javiereguiluzdmaicher
authored andcommitted
Tweaks
1 parent 0fa427c commit 172ce3d

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

app/AppKernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function registerBundles()
3535
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
3636
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
3737

38-
if ($this->getEnvironment() === 'test') {
39-
// this bundle is used to have isolated phpunit tests when interacting with doctrine & the database
40-
// and is therefore only loaded for the 'test' environment
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
4141
$bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
4242
}
4343
}
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
"white-october/pagerfanta-bundle" : "^1.0"
3232
},
3333
"require-dev": {
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"
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"
3939
},
4040
"scripts": {
4141
"symfony-scripts": [

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
</filter>
3131

3232
<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 -->
3335
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" />
3436
</listeners>
3537
</phpunit>

tests/AppBundle/Controller/Admin/BlogControllerTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,44 +80,44 @@ public function testAdminBackendHomePage()
8080
}
8181

8282
/**
83-
* This test will manipulate the database state by deleting a Post.
84-
* All changes to the database are rolled back before the next test case is executed.
85-
* This is done by using https://github.com/dmaicher/doctrine-test-bundle.
86-
*
87-
* Therefore in the following test case we can edit the Post with ID=1 although we deleted it in this test.
83+
* This test changes the database contents by deleting a blog post. However,
84+
* thanks to the DAMADoctrineTestBundle and its PRPUnit 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.
8887
*/
8988
public function testAdminDeletePost()
9089
{
9190
$client = $this->getAdminClient();
92-
9391
$crawler = $client->request('GET', '/en/admin/post/1');
94-
95-
$client->submit($crawler->filter('form')->form());
92+
$client->submit($crawler->filter('#delete-form')->form());
9693

9794
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
9895

9996
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
10097
$this->assertNull($post);
10198
}
10299

100+
/**
101+
* This test changes the database contents by editing a blog post. However,
102+
* thanks to the DAMADoctrineTestBundle and its PRPUnit 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+
*/
103106
public function testAdminEditPost()
104107
{
105-
$client = $this->getAdminClient();
108+
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
106109

110+
$client = $this->getAdminClient();
107111
$crawler = $client->request('GET', '/en/admin/post/1/edit');
108-
109-
$newTitle = 'what a nice new title!';
110-
111-
$form = $crawler->filter('form')->form([
112-
'post[title]' => $newTitle,
112+
$form = $crawler->selectButton('Save changes')->form([
113+
'post[title]' => $newBlogPostTitle,
113114
]);
114-
115115
$client->submit($form);
116116

117117
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
118118

119119
/** @var Post $post */
120120
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
121-
$this->assertSame($newTitle, $post->getTitle());
121+
$this->assertSame($newBlogPostTitle, $post->getTitle());
122122
}
123123
}

0 commit comments

Comments
 (0)