Skip to content

Instantly share code, notes, and snippets.

@intellix
Last active December 26, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intellix/7145160 to your computer and use it in GitHub Desktop.
Save intellix/7145160 to your computer and use it in GitHub Desktop.
Testing a Doctrine ZF2 Application with rollbacking of data inserted into your database during the test. Listener solution found through stackoverflow: http://stackoverflow.com/questions/12456281/how-to-clean-a-test-database-with-doctrine And apparently since creating this, I've been provided this by ocramius for a Doctrine testing environment: h…
<?php
namespace Application\Service;
use Application\Entity;
class AccountService extends PHPUnit_Framework_TestCase
{
protected $entityManager;
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function create(Entity\Account $account)
{
$account->setCreation(new \DateTime('now'));
$this->entityManager->persist($account);
$this->entityManager->flush();
return $account;
}
}
<?php
namespace ApplicationTest\Service;
use PHPUnit_Framework_TestCase;
use ApplicationTest\Bootstrap;
use Application\Service;
use Application\Entity;
class AccountServiceTest extends PHPUnit_Framework_TestCase
{
protected $sAccount;
public function setUp()
{
$this->sAccount = new Service\Account;
}
public function testAccountCreation()
{
$account = new Entity\Account;
$account->setEmail('woop@woop.com')
->setPassword('123');
$account = $this->sAccount->create($account);
$this->assertEquals($account->getCreation(), new \DateTime('now'));
}
}
<?php
namespace ApplicationTest;
use PHPUnit_Framework_TestListener;
use ApplicationTest\Bootstrap;
class TestDbCleanupListener implements PHPUnit_Framework_TestListener
{
private $_em;
private function _getEntityManager()
{
if (!$this->_em) {
$this->_em = Bootstrap::getServiceManager()->get('doctrine.entitymanager.orm_default');
}
return $this->_em;
}
/**
* called when test is started - starts transaction
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::startTest()
*/
public function startTest(PHPUnit_Framework_Test $test)
{
$this->_getEntityManager()->beginTransaction();
}
/**
* called when test is ended - rolls back the transaction
* @param PHUnit_Framework_Test $test
* @param float $length the length of time for the test
*/
public function endTest(PHPUnit_Framework_Test $test, $length)
{
$this->_getEntityManager()->rollback();
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addError()
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addFailure()
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addError()
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addSkippedTest()
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::startTestSuite()
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::endTestSuite()
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php" colors="true">
<testsuites>
<testsuite name="Whole application">
<directory>./ApplicationTest</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="ApplicationTest\TestDbCleanupListener" file="./ApplicationTest/PHPUnitTestListener.php"></listener>
</listeners>
</phpunit>
@intellix
Copy link
Author

https://gist.github.com/Ocramius/3994325 - a doctrine testing environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment