diff --git a/app/AppKernel.php b/app/AppKernel.php
index 1129c08d5..e68667c2a 100644
--- a/app/AppKernel.php
+++ b/app/AppKernel.php
@@ -34,6 +34,12 @@ public function registerBundles()
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
+
+ if ('test' === $this->getEnvironment()) {
+ // this bundle makes it easier to work with databases in PHPUnit
+ // tests, so it's only loaded for the 'test' environment
+ $bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
+ }
}
return $bundles;
diff --git a/app/Resources/views/admin/blog/_delete_form.html.twig b/app/Resources/views/admin/blog/_delete_form.html.twig
index 361548984..da76d1281 100644
--- a/app/Resources/views/admin/blog/_delete_form.html.twig
+++ b/app/Resources/views/admin/blog/_delete_form.html.twig
@@ -1,5 +1,5 @@
{{ include('blog/_delete_post_confirmation.html.twig') }}
-
diff --git a/composer.json b/composer.json
index 7f1eb7fb0..bb022f08f 100644
--- a/composer.json
+++ b/composer.json
@@ -31,6 +31,7 @@
"white-october/pagerfanta-bundle" : "^1.0"
},
"require-dev": {
+ "dama/doctrine-test-bundle" : "^1.0",
"friendsofphp/php-cs-fixer" : "^2.0",
"phpunit/phpunit" : "^4.8 || ^5.0",
"sensio/generator-bundle" : "^3.0",
diff --git a/composer.lock b/composer.lock
index 60a83d143..77f3be08e 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "content-hash": "7c54f4ce91a0b78303dc8be7edb768b8",
+ "content-hash": "a201a8d92585c751f757f88752195add",
"packages": [
{
"name": "doctrine/annotations",
@@ -2347,6 +2347,60 @@
}
],
"packages-dev": [
+ {
+ "name": "dama/doctrine-test-bundle",
+ "version": "v1.0.9",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/dmaicher/doctrine-test-bundle.git",
+ "reference": "516bb505e8f7fbd5b7bc0ae69e738dfcf7e9ffd4"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/dmaicher/doctrine-test-bundle/zipball/516bb505e8f7fbd5b7bc0ae69e738dfcf7e9ffd4",
+ "reference": "516bb505e8f7fbd5b7bc0ae69e738dfcf7e9ffd4",
+ "shasum": ""
+ },
+ "require": {
+ "doctrine/dbal": "~2.5",
+ "doctrine/doctrine-bundle": "~1.4",
+ "php": ">=5.5.0",
+ "phpunit/phpunit": "~4.1|~5.0",
+ "symfony/symfony": "~2.3|~3.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.1.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "David Maicher",
+ "email": "mail@dmaicher.de"
+ }
+ ],
+ "description": "Symfony 2/3 bundle to isolate doctrine database tests and improve test performance",
+ "keywords": [
+ "Symfony 3",
+ "doctrine",
+ "isolation",
+ "performance",
+ "symfony",
+ "symfony 2",
+ "tests"
+ ],
+ "time": "2017-01-04T19:24:03+00:00"
+ },
{
"name": "friendsofphp/php-cs-fixer",
"version": "v2.1.0",
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 65e90821d..04d542ede 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -28,4 +28,10 @@
+
+
+
+
+
diff --git a/tests/AppBundle/Controller/Admin/BlogControllerTest.php b/tests/AppBundle/Controller/Admin/BlogControllerTest.php
index a83182915..0b601401e 100644
--- a/tests/AppBundle/Controller/Admin/BlogControllerTest.php
+++ b/tests/AppBundle/Controller/Admin/BlogControllerTest.php
@@ -11,6 +11,7 @@
namespace Tests\AppBundle\Controller\Admin;
+use AppBundle\Entity\Post;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
@@ -54,35 +55,22 @@ public function getUrlsForRegularUsers()
}
/**
- * @dataProvider getUrlsForAdminUsers
+ * @return \Symfony\Bundle\FrameworkBundle\Client
*/
- public function testAdminUsers($httpMethod, $url, $statusCode)
+ private function getAdminClient()
{
- $client = static::createClient([], [
+ return static::createClient([], [
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);
-
- $client->request($httpMethod, $url);
- $this->assertSame($statusCode, $client->getResponse()->getStatusCode());
- }
-
- public function getUrlsForAdminUsers()
- {
- yield ['GET', '/en/admin/post/', Response::HTTP_OK];
- yield ['GET', '/en/admin/post/1', Response::HTTP_OK];
- yield ['GET', '/en/admin/post/1/edit', Response::HTTP_OK];
- yield ['POST', '/en/admin/post/1/delete', Response::HTTP_FOUND];
}
- public function testBackendHomepage()
+ public function testAdminBackendHomePage()
{
- $client = static::createClient([], [
- 'PHP_AUTH_USER' => 'jane_admin',
- 'PHP_AUTH_PW' => 'kitten',
- ]);
+ $client = $this->getAdminClient();
$crawler = $client->request('GET', '/en/admin/post/');
+ $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertCount(
30,
@@ -90,4 +78,46 @@ public function testBackendHomepage()
'The backend homepage displays all the available posts.'
);
}
+
+ /**
+ * This test changes the database contents by deleting a blog post. However,
+ * thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
+ * to the database are rolled back when this test completes. This means that
+ * all the application tests begin with the same database contents.
+ */
+ public function testAdminDeletePost()
+ {
+ $client = $this->getAdminClient();
+ $crawler = $client->request('GET', '/en/admin/post/1');
+ $client->submit($crawler->filter('#delete-form')->form());
+
+ $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
+
+ $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
+ $this->assertNull($post);
+ }
+
+ /**
+ * This test changes the database contents by editing a blog post. However,
+ * thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
+ * to the database are rolled back when this test completes. This means that
+ * all the application tests begin with the same database contents.
+ */
+ public function testAdminEditPost()
+ {
+ $newBlogPostTitle = 'Blog Post Title '.mt_rand();
+
+ $client = $this->getAdminClient();
+ $crawler = $client->request('GET', '/en/admin/post/1/edit');
+ $form = $crawler->selectButton('Save changes')->form([
+ 'post[title]' => $newBlogPostTitle,
+ ]);
+ $client->submit($form);
+
+ $this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
+
+ /** @var Post $post */
+ $post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
+ $this->assertSame($newBlogPostTitle, $post->getTitle());
+ }
}