BaseTestCaseOM.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /**
  26. * Base test case contains common mock objects
  27. * generation methods for multi object manager
  28. * test cases
  29. *
  30. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  31. * @package Gedmo
  32. * @subpackage BaseTestCase
  33. * @link http://www.gediminasm.org
  34. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  35. */
  36. abstract class BaseTestCaseOM extends \PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * @var EventManager
  40. */
  41. protected $evm;
  42. /**
  43. * Initialized document managers
  44. *
  45. * @var array
  46. */
  47. private $dms = array();
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function setUp()
  52. {
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function tearDown()
  58. {
  59. foreach ($this->dms as $dm) {
  60. if ($dm) {
  61. foreach ($dm->getDocumentDatabases() as $db) {
  62. foreach ($db->listCollections() as $collection) {
  63. $collection->drop();
  64. }
  65. }
  66. $dm->getConnection()->close();
  67. $dm = null;
  68. }
  69. }
  70. }
  71. /**
  72. * DocumentManager mock object together with
  73. * annotation mapping driver and database
  74. *
  75. * @param string $dbName
  76. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  77. * @return DocumentManager
  78. */
  79. protected function getMockDocumentManager($dbName, MappingDriver $mappingDriver = null)
  80. {
  81. if (!class_exists('Mongo')) {
  82. $this->markTestSkipped('Missing Mongo extension.');
  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, MappingDriver $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, MappingDriver $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(MappingDriver $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 (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 TranslatableListener);
  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, MappingDriver $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
  226. ->expects($this->any())
  227. ->method('getMongoCmd')
  228. ->will($this->returnValue('$'))
  229. ;
  230. $config
  231. ->expects($this->any())
  232. ->method('getDefaultCommitOptions')
  233. ->will($this->returnValue(array('safe' => true)))
  234. ;
  235. if (null === $mappingDriver) {
  236. $mappingDriver = $this->getDefaultMongoODMMetadataDriverImplementation();
  237. }
  238. $config->expects($this->any())
  239. ->method('getMetadataDriverImpl')
  240. ->will($this->returnValue($mappingDriver));
  241. return $config;
  242. }
  243. /**
  244. * Get annotation mapping configuration for ORM
  245. *
  246. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  247. * @return Doctrine\ORM\Configuration
  248. */
  249. private function getMockAnnotatedORMConfig(MappingDriver $mappingDriver = null)
  250. {
  251. $config = $this->getMock('Doctrine\ORM\Configuration');
  252. $config->expects($this->once())
  253. ->method('getProxyDir')
  254. ->will($this->returnValue(__DIR__.'/../../temp'));
  255. $config->expects($this->once())
  256. ->method('getProxyNamespace')
  257. ->will($this->returnValue('Proxy'));
  258. $config->expects($this->once())
  259. ->method('getAutoGenerateProxyClasses')
  260. ->will($this->returnValue(true));
  261. $config->expects($this->once())
  262. ->method('getClassMetadataFactoryName')
  263. ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'));
  264. $config
  265. ->expects($this->any())
  266. ->method('getDefaultRepositoryClassName')
  267. ->will($this->returnValue('Doctrine\\ORM\\EntityRepository'))
  268. ;
  269. $config
  270. ->expects($this->any())
  271. ->method('getQuoteStrategy')
  272. ->will($this->returnValue(new DefaultQuoteStrategy()))
  273. ;
  274. $config
  275. ->expects($this->any())
  276. ->method('getNamingStrategy')
  277. ->will($this->returnValue(new DefaultNamingStrategy()))
  278. ;
  279. if (null === $mappingDriver) {
  280. $mappingDriver = $this->getDefaultORMMetadataDriverImplementation();
  281. }
  282. $config->expects($this->any())
  283. ->method('getMetadataDriverImpl')
  284. ->will($this->returnValue($mappingDriver));
  285. return $config;
  286. }
  287. }