Last active
December 26, 2015 11:39
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Ocramius/3994325 - a doctrine testing environment