BaseTestCasePHPCRODM.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Test\Tool;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Doctrine\DBAL\DriverManager;
  5. use Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver;
  6. use Doctrine\ODM\PHPCR\DocumentManager;
  7. use Doctrine\Common\EventManager;
  8. use Doctrine\MongoDB\Connection;
  9. use Jackalope\RepositoryFactoryDoctrineDBAL;
  10. use Jackalope\Session;
  11. use Jackalope\Transport\DoctrineDBAL\RepositorySchema;
  12. /**
  13. * Base test case contains common mock objects
  14. */
  15. abstract class BaseTestCasePHPCRODM extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var DocumentManager
  19. */
  20. protected $dm;
  21. protected function setUp()
  22. {
  23. if (!class_exists('Doctrine\ODM\PHPCR\Query\Query')) {
  24. $this->markTestSkipped('Doctrine PHPCR-ODM is not available');
  25. }
  26. }
  27. protected function tearDown()
  28. {
  29. if ($this->dm) {
  30. $this->dm = null;
  31. }
  32. }
  33. protected function getMockDocumentManager(EventManager $evm = null)
  34. {
  35. $config = new \Doctrine\ODM\PHPCR\Configuration();
  36. $config->setMetadataDriverImpl($this->getMetadataDriverImplementation());
  37. $this->dm = DocumentManager::create($this->getSession(), $config, $evm ?: $this->getEventManager());
  38. return $this->dm;
  39. }
  40. protected function getMetadataDriverImplementation()
  41. {
  42. return new AnnotationDriver($_ENV['annotation_reader']);
  43. }
  44. private function getSession()
  45. {
  46. $connection = DriverManager::getConnection(array(
  47. 'driver' => 'pdo_sqlite',
  48. 'path' => ':memory:',
  49. ));
  50. $factory = new RepositoryFactoryDoctrineDBAL();
  51. $repository = $factory->getRepository(array(
  52. 'jackalope.doctrine_dbal_connection' => $connection,
  53. ));
  54. $schema = new RepositorySchema(array('disable_fks' => true), $connection);
  55. $schema->reset();
  56. $session = $repository->login(new \PHPCR\SimpleCredentials('', ''));
  57. $cnd = <<<CND
  58. <phpcr='http://www.doctrine-project.org/projects/phpcr_odm'>
  59. [phpcr:managed]
  60. mixin
  61. - phpcr:class (STRING)
  62. - phpcr:classparents (STRING) multiple
  63. CND;
  64. $session->getWorkspace()->getNodeTypeManager()->registerNodeTypesCnd($cnd, true);
  65. return $session;
  66. }
  67. private function getEventManager()
  68. {
  69. $evm = new EventManager();
  70. return $evm;
  71. }
  72. }