Created
July 7, 2011 09:33
-
-
Save kix/1069184 to your computer and use it in GitHub Desktop.
Тест-кейс с полуавтоматической подгрузкой фикстур
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 | |
/** | |
* @author kix <kix@kixlive.ru> | |
*/ | |
use Doctrine\Common\DataFixtures\Loader; | |
/** | |
* | |
* Тест-кейс с полуавтоматической подгрузкой фикстур | |
* @author Stepan Anchugov <kix@kixlive.ru> | |
* | |
*/ | |
class App_Test_PHPUnit_FixtureTestCase extends PHPUnit_Framework_TestCase { | |
/** | |
* Entity manager | |
* @var Doctrine\ORM\EntityManager | |
*/ | |
private $_em; | |
/** | |
* @var array<AbstractFixture> | |
*/ | |
public $_fixtures; | |
/** | |
* Fixture loader | |
* @var Doctrine\Common\DataFixtures\Loader | |
*/ | |
private $_loader; | |
function __construct() | |
{ | |
$this->_em = App::getEntityManager(); | |
$this->_loader = new Loader(); | |
} | |
function setUp() | |
{ | |
if (count($this->_fixtures) > 0) { | |
$this->loadFixtures($this->_fixtures); | |
} | |
} | |
function setEm(Doctrine\ORM\EntityManager $em) | |
{ | |
$this->_em = $em; | |
} | |
function assertArrayEquals(array $expected, array $actual) | |
{ | |
$diff = array_diff($expected, $actual); | |
$this->assertEmpty($diff); | |
} | |
function getQueries() | |
{ | |
return $this->_em->getConfiguration()->getSQLLogger()->queries; | |
} | |
function loadFixtures(array $fixtures) | |
{ | |
$loader = new Loader(); | |
foreach ($fixtures as $fixtureName) { | |
$fixtureClass = $fixtureName . '_Fixture'; | |
if (!class_exists($fixtureClass)) { | |
throw new Exception(sprintf('Fixture %s was required in class %s, but could not be found', | |
$fixtureClass, get_class($this))); | |
return false; | |
} | |
$fixture = new $fixtureClass(); | |
if (!($fixture instanceof AbstractFixture)) { | |
throw new Exception(sprintf( | |
'Fixture %s should extend AbstractFixture class. But it does not!', | |
$fixtureClassName)); | |
} | |
$loader->addFixture($fixture); | |
} | |
/* | |
* Kill the magic | |
* $fixtureClassName = get_class($this).'_Fixture'; | |
if (class_exists($fixtureClassName)) { | |
$fixture = new $fixtureClassName(); | |
if ($fixture instanceof AbstractFixture) { | |
$this->_fixture = $fixture; | |
} else { | |
throw new Exception(sprintf( | |
'Fixture %s should extend AbstractFixture class. But it does not!', | |
$fixtureClassName)); | |
} | |
}*/ | |
$purger = new ORMPurger(); | |
$executor = new ORMExecutor($this->_em, $purger); | |
$executor->execute($this->_loader->getFixtures()); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment