Created
November 1, 2012 15:27
-
-
Save Ocramius/3994325 to your computer and use it in GitHub Desktop.
ZF2 and Doctrine Data Fixtures testing environment
This file contains hidden or 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 Marco Pivetta <ocramius@gmail.com> | |
| */ | |
| use Zend\ServiceManager\ServiceManager; | |
| use Zend\Mvc\Service\ServiceManagerConfig; | |
| use DoctrineORMModuleTest\Framework\TestCase; | |
| use ContentTest\Util\ServiceManagerFactory; | |
| use Zend\Loader\StandardAutoloader; | |
| chdir(__DIR__); | |
| $previousDir = '.'; | |
| while (!file_exists('config/application.config.php')) { | |
| $dir = dirname(getcwd()); | |
| if ($previousDir === $dir) { | |
| throw new RuntimeException( | |
| 'Unable to locate "config/application.config.php":' | |
| . ' is the Content module in a sub-directory of your application skeleton?' | |
| ); | |
| } | |
| $previousDir = $dir; | |
| chdir($dir); | |
| } | |
| if (!((@include_once __DIR__ . '/../../../../../vendor/autoload.php') || !(@include_once __DIR__ . '/../../../../autoload.php'))) { | |
| throw new RuntimeException('vendor/autoload.php could not be found. Did you run `php composer.phar install`?'); | |
| } | |
| if (!$config = @include __DIR__ . '/TestConfiguration.php') { | |
| $config = require __DIR__ . '/TestConfiguration.php.dist'; | |
| } | |
| $loader = new StandardAutoloader(); | |
| $loader->registerNamespace('ContentTest', __DIR__ . 'ContentTest'); | |
| $loader->registerNamespace('ContentDataFixture', __DIR__ . 'ContentDataFixture'); | |
| $loader->register(); | |
| $serviceManager = new ServiceManager(new ServiceManagerConfig( | |
| isset($config['service_manager']) ? $config['service_manager'] : array() | |
| )); | |
| $serviceManager->setService('ApplicationConfig', $config); | |
| /** @var $moduleManager \Zend\ModuleManager\ModuleManager */ | |
| $moduleManager = $serviceManager->get('ModuleManager'); | |
| $moduleManager->loadModules(); | |
| ServiceManagerFactory::setApplicationConfig($config); |
This file contains hidden or 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 ContentTest\EntityRepository; | |
| use PHPUnit_Framework_TestCase; | |
| use ContentTest\Util\ServiceManagerFactory; | |
| use Content\EntityRepository\PageRepository; | |
| /** | |
| * Tests for PageRepository | |
| * | |
| * @author Marco Pivetta <ocramius@gmail.com> | |
| */ | |
| class PageRepositoryTest extends PHPUnit_Framework_TestCase | |
| { | |
| /** | |
| * @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor | |
| */ | |
| protected $fixtureExectutor; | |
| /** | |
| * @var PageRepository | |
| */ | |
| protected $repository; | |
| public function setUp() | |
| { | |
| $sm = ServiceManagerFactory::getServiceManager(); | |
| $this->repository = $sm->get('Content\EntityRepository\PageRepository'); | |
| $this->fixtureExectutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor'); | |
| $this->assertInstanceOf('Content\EntityRepository\PageRepository', $this->repository); | |
| } | |
| /** | |
| * @covers \Content\EntityRepository\PageRepository::getRootPages | |
| */ | |
| public function testGetRootPages() | |
| { | |
| $pageWithNoLogs = new PageWithNoLogs(); | |
| $this->fixtureExectutor->execute(array($pageWithNoLogs)); | |
| $this->assertEmpty($this->repository->getRootPages()); | |
| $this->assertCount(1, $this->repository->findAll()); | |
| } | |
| } |
This file contains hidden or 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 ContentDataFixture; | |
| use Doctrine\Common\DataFixtures\AbstractFixture; | |
| use Doctrine\Common\Persistence\ObjectManager; | |
| use Content\Entity\Page; | |
| /** | |
| * @author Marco Pivetta <ocramius@gmail.com> | |
| */ | |
| class PageWithNoLogs extends AbstractFixture | |
| { | |
| /** | |
| * @var Page | |
| */ | |
| protected $page; | |
| /** | |
| * {inheritDoc} | |
| */ | |
| public function load(ObjectManager $manager) | |
| { | |
| $this->page = new Page(); | |
| $manager->persist($this->page); | |
| $manager->flush(); | |
| } | |
| /** | |
| * @return Page | |
| */ | |
| public function getPage() | |
| { | |
| return $this->page; | |
| } | |
| } |
This file contains hidden or 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 Marco Pivetta <ocramius@gmail.com> | |
| */ | |
| namespace ContentTest\Util; | |
| use Zend\ServiceManager\ServiceManager; | |
| use Zend\ServiceManager\ServiceLocatorInterface; | |
| use Zend\Mvc\Service\ServiceManagerConfig; | |
| use Doctrine\Common\DataFixtures\Purger\ORMPurger as FixturePurger; | |
| use Doctrine\Common\DataFixtures\Executor\ORMExecutor as FixtureExecutor; | |
| use Doctrine\ORM\Tools\SchemaTool; | |
| /** | |
| * Base test case to be used when a new service manager instance is required | |
| */ | |
| abstract class ServiceManagerFactory | |
| { | |
| /** | |
| * @var array | |
| */ | |
| private static $config = array(); | |
| /** | |
| * @static | |
| * @param array $config | |
| */ | |
| public static function setApplicationConfig(array $config) | |
| { | |
| static::$config = $config; | |
| } | |
| /** | |
| * @static | |
| * @return array | |
| */ | |
| public static function getApplicationConfig() | |
| { | |
| return static::$config; | |
| } | |
| /** | |
| * @param array|null $config | |
| * @return ServiceManager | |
| */ | |
| public static function getServiceManager(array $config = null) | |
| { | |
| $config = $config ?: static::getApplicationConfig(); | |
| $serviceManager = new ServiceManager(new ServiceManagerConfig( | |
| isset($config['service_manager']) ? $config['service_manager'] : array() | |
| )); | |
| $serviceManager->setService('ApplicationConfig', $config); | |
| /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ | |
| $moduleManager = $serviceManager->get('ModuleManager'); | |
| $moduleManager->loadModules(); | |
| // @todo move to own factory class/add to merged configuration? Create a test module? | |
| $serviceManager->setFactory( | |
| 'Doctrine\Common\DataFixtures\Executor\AbstractExecutor', | |
| function(ServiceLocatorInterface $sl) | |
| { | |
| /* @var $em \Doctrine\ORM\EntityManager */ | |
| $em = $sl->get('Doctrine\ORM\EntityManager'); | |
| $schemaTool = new SchemaTool($em); | |
| $schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata()); | |
| return new FixtureExecutor($em, new FixturePurger($em)); | |
| } | |
| ); | |
| return $serviceManager; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment