BaseTestCaseOM.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace Tool;
  3. // common
  4. use Doctrine\Common\Annotations\AnnotationReader;
  5. use Doctrine\Common\EventManager;
  6. // orm specific
  7. use Doctrine\ORM\Mapping\Driver\Driver as MappingDriverORM;
  8. use Doctrine\ORM\Mapping\Driver\AnnotationDriver as AnnotationDriverORM;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\ORM\Tools\SchemaTool;
  11. // odm specific
  12. use Doctrine\ODM\MongoDB\Mapping\Driver\Driver as MappingDriverODM;
  13. use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver as AnnotationDriverODM;
  14. use Doctrine\ODM\MongoDB\DocumentManager;
  15. use Doctrine\MongoDB\Connection;
  16. // listeners
  17. use Gedmo\Translatable\TranslationListener;
  18. use Gedmo\Sluggable\SluggableListener;
  19. use Gedmo\Tree\TreeListener;
  20. use Gedmo\Timestampable\TimestampableListener;
  21. use Gedmo\Loggable\LoggableListener;
  22. /**
  23. * Base test case contains common mock objects
  24. * generation methods for multi object manager
  25. * test cases
  26. *
  27. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  28. * @package Gedmo
  29. * @subpackage BaseTestCase
  30. * @link http://www.gediminasm.org
  31. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  32. */
  33. abstract class BaseTestCaseOM extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * @var EventManager
  37. */
  38. protected $evm;
  39. /**
  40. * Initialized document managers
  41. *
  42. * @var array
  43. */
  44. private $dms = array();
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function setUp()
  49. {
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function tearDown()
  55. {
  56. foreach ($this->dms as $dm) {
  57. if ($dm) {
  58. foreach ($dm->getDocumentDatabases() as $db) {
  59. foreach ($db->listCollections() as $collection) {
  60. $collection->drop();
  61. }
  62. }
  63. $dm->getConnection()->close();
  64. $dm = null;
  65. }
  66. }
  67. }
  68. /**
  69. * DocumentManager mock object together with
  70. * annotation mapping driver and database
  71. *
  72. * @param string $dbName
  73. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  74. * @return DocumentManager
  75. */
  76. protected function getMockDocumentManager($dbName, MappingDriverODM $mappingDriver = null)
  77. {
  78. if (!class_exists('Mongo')) {
  79. $this->markTestSkipped('Missing Mongo extension.');
  80. }
  81. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  82. $this->markTestSkipped('ODM does not support version 2.2 of doctrine common.');
  83. }
  84. $conn = new Connection;
  85. $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
  86. $dm = null;
  87. try {
  88. $dm = DocumentManager::create($conn, $config, $this->getEventManager());
  89. $dm->getConnection()->connect();
  90. } catch (\MongoException $e) {
  91. $this->markTestSkipped('Doctrine MongoDB ODM failed to connect');
  92. }
  93. return $dm;
  94. }
  95. /**
  96. * DocumentManager mock object with
  97. * annotation mapping driver
  98. *
  99. * @param string $dbName
  100. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  101. * @return DocumentManager
  102. */
  103. protected function getMockMappedDocumentManager($dbName, MappingDriverODM $mappingDriver = null)
  104. {
  105. $conn = $this->getMock('Doctrine\\MongoDB\\Connection');
  106. $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
  107. $dm = DocumentManager::create($conn, $config, $this->getEventManager());
  108. return $dm;
  109. }
  110. /**
  111. * EntityManager mock object together with
  112. * annotation mapping driver and pdo_sqlite
  113. * database in memory
  114. *
  115. * @param array $fixtures
  116. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  117. * @return EntityManager
  118. */
  119. protected function getMockSqliteEntityManager(array $fixtures, MappingDriverORM $mappingDriver = null)
  120. {
  121. $conn = array(
  122. 'driver' => 'pdo_sqlite',
  123. 'memory' => true,
  124. );
  125. $config = $this->getMockAnnotatedORMConfig($mappingDriver);
  126. $em = EntityManager::create($conn, $config, $this->getEventManager());
  127. $schema = array_map(function($class) use ($em) {
  128. return $em->getClassMetadata($class);
  129. }, $fixtures);
  130. $schemaTool = new SchemaTool($em);
  131. $schemaTool->dropSchema(array());
  132. $schemaTool->createSchema($schema);
  133. return $em;
  134. }
  135. /**
  136. * EntityManager mock object with
  137. * annotation mapping driver
  138. *
  139. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  140. * @return EntityManager
  141. */
  142. protected function getMockMappedEntityManager(MappingDriverORM $mappingDriver = null)
  143. {
  144. $driver = $this->getMock('Doctrine\DBAL\Driver');
  145. $driver->expects($this->once())
  146. ->method('getDatabasePlatform')
  147. ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
  148. $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
  149. $conn->expects($this->once())
  150. ->method('getEventManager')
  151. ->will($this->returnValue($this->getEventManager()));
  152. $config = $this->getMockAnnotatedORMConfig($mappingDriver);
  153. $em = EntityManager::create($conn, $config);
  154. return $em;
  155. }
  156. /**
  157. * Creates default mapping driver
  158. *
  159. * @return \Doctrine\ORM\Mapping\Driver\Driver
  160. */
  161. protected function getDefaultORMMetadataDriverImplementation()
  162. {
  163. return new AnnotationDriverORM($_ENV['annotation_reader']);
  164. }
  165. /**
  166. * Creates default mapping driver
  167. *
  168. * @return \Doctrine\ODM\MongoDB\Mapping\Driver\Driver
  169. */
  170. protected function getDefaultMongoODMMetadataDriverImplementation()
  171. {
  172. return new AnnotationDriverODM($_ENV['annotation_reader']);
  173. }
  174. /**
  175. * Build event manager
  176. *
  177. * @return EventManager
  178. */
  179. private function getEventManager()
  180. {
  181. if (is_null($this->evm)) {
  182. $this->evm = new EventManager;
  183. $this->evm->addEventSubscriber(new TreeListener);
  184. $this->evm->addEventSubscriber(new SluggableListener);
  185. $this->evm->addEventSubscriber(new LoggableListener);
  186. $this->evm->addEventSubscriber(new TranslationListener);
  187. $this->evm->addEventSubscriber(new TimestampableListener);
  188. }
  189. return $this->evm;
  190. }
  191. /**
  192. * Get annotation mapping configuration
  193. *
  194. * @param string $dbName
  195. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  196. * @return Doctrine\ORM\Configuration
  197. */
  198. private function getMockAnnotatedODMMongoDBConfig($dbName, MappingDriverODM $mappingDriver = null)
  199. {
  200. $config = $this->getMock('Doctrine\\ODM\\MongoDB\\Configuration');
  201. $config->expects($this->once())
  202. ->method('getProxyDir')
  203. ->will($this->returnValue(__DIR__.'/../../temp'));
  204. $config->expects($this->once())
  205. ->method('getProxyNamespace')
  206. ->will($this->returnValue('Proxy'));
  207. $config->expects($this->once())
  208. ->method('getHydratorDir')
  209. ->will($this->returnValue(__DIR__.'/../../temp'));
  210. $config->expects($this->once())
  211. ->method('getHydratorNamespace')
  212. ->will($this->returnValue('Hydrator'));
  213. $config->expects($this->any())
  214. ->method('getDefaultDB')
  215. ->will($this->returnValue($dbName));
  216. $config->expects($this->once())
  217. ->method('getAutoGenerateProxyClasses')
  218. ->will($this->returnValue(true));
  219. $config->expects($this->once())
  220. ->method('getAutoGenerateHydratorClasses')
  221. ->will($this->returnValue(true));
  222. $config->expects($this->once())
  223. ->method('getClassMetadataFactoryName')
  224. ->will($this->returnValue('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadataFactory'));
  225. $config->expects($this->any())
  226. ->method('getMongoCmd')
  227. ->will($this->returnValue('$'));
  228. if (is_null($mappingDriver)) {
  229. $mappingDriver = $this->getDefaultMongoODMMetadataDriverImplementation();
  230. }
  231. $config->expects($this->any())
  232. ->method('getMetadataDriverImpl')
  233. ->will($this->returnValue($mappingDriver));
  234. return $config;
  235. }
  236. /**
  237. * Get annotation mapping configuration for ORM
  238. *
  239. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  240. * @return Doctrine\ORM\Configuration
  241. */
  242. private function getMockAnnotatedORMConfig(MappingDriverORM $mappingDriver = null)
  243. {
  244. $config = $this->getMock('Doctrine\ORM\Configuration');
  245. $config->expects($this->once())
  246. ->method('getProxyDir')
  247. ->will($this->returnValue(__DIR__.'/../../temp'));
  248. $config->expects($this->once())
  249. ->method('getProxyNamespace')
  250. ->will($this->returnValue('Proxy'));
  251. $config->expects($this->once())
  252. ->method('getAutoGenerateProxyClasses')
  253. ->will($this->returnValue(true));
  254. $config->expects($this->once())
  255. ->method('getClassMetadataFactoryName')
  256. ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'));
  257. if (is_null($mappingDriver)) {
  258. $mappingDriver = $this->getDefaultORMMetadataDriverImplementation();
  259. }
  260. $config->expects($this->any())
  261. ->method('getMetadataDriverImpl')
  262. ->will($this->returnValue($mappingDriver));
  263. return $config;
  264. }
  265. }