Skip to content

Use dama/doctrine-test-bundle for proper test DB isolation #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/Resources/views/admin/blog/_delete_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ include('blog/_delete_post_confirmation.html.twig') }}
<form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post" data-confirmation="true">
<form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post" data-confirmation="true" id="delete-form">
<input type="hidden" name="token" value="{{ csrf_token('delete') }}" />
<input type="submit" value="{{ 'action.delete_post'|trans }}" class="btn btn-lg btn-block btn-danger" />
</form>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 55 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@
</exclude>
</whitelist>
</filter>

<listeners>
<!-- it begins a database transaction before every testcase and rolls it back after
the test finished, so tests can manipulate the database without affecting other tests -->
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" />
</listeners>
</phpunit>
68 changes: 49 additions & 19 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Tests\AppBundle\Controller\Admin;

use AppBundle\Entity\Post;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -54,40 +55,69 @@ 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,
$crawler->filter('body#admin_post_index #main tbody tr'),
'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());
}
}