BaseTestCaseOM.php 10 KB

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