BaseTestCaseOM.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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\TranslatableListener;
  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. $conn = new Connection;
  82. $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
  83. $dm = null;
  84. try {
  85. $dm = DocumentManager::create($conn, $config, $this->getEventManager());
  86. $dm->getConnection()->connect();
  87. } catch (\MongoException $e) {
  88. $this->markTestSkipped('Doctrine MongoDB ODM failed to connect');
  89. }
  90. return $dm;
  91. }
  92. /**
  93. * DocumentManager mock object with
  94. * annotation mapping driver
  95. *
  96. * @param string $dbName
  97. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  98. * @return DocumentManager
  99. */
  100. protected function getMockMappedDocumentManager($dbName, MappingDriverODM $mappingDriver = null)
  101. {
  102. $conn = $this->getMock('Doctrine\\MongoDB\\Connection');
  103. $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
  104. $dm = DocumentManager::create($conn, $config, $this->getEventManager());
  105. return $dm;
  106. }
  107. /**
  108. * EntityManager mock object together with
  109. * annotation mapping driver and pdo_sqlite
  110. * database in memory
  111. *
  112. * @param array $fixtures
  113. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  114. * @return EntityManager
  115. */
  116. protected function getMockSqliteEntityManager(array $fixtures, MappingDriverORM $mappingDriver = null)
  117. {
  118. $conn = array(
  119. 'driver' => 'pdo_sqlite',
  120. 'memory' => true,
  121. );
  122. $config = $this->getMockAnnotatedORMConfig($mappingDriver);
  123. $em = EntityManager::create($conn, $config, $this->getEventManager());
  124. $schema = array_map(function($class) use ($em) {
  125. return $em->getClassMetadata($class);
  126. }, $fixtures);
  127. $schemaTool = new SchemaTool($em);
  128. $schemaTool->dropSchema(array());
  129. $schemaTool->createSchema($schema);
  130. return $em;
  131. }
  132. /**
  133. * EntityManager mock object with
  134. * annotation mapping driver
  135. *
  136. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  137. * @return EntityManager
  138. */
  139. protected function getMockMappedEntityManager(MappingDriverORM $mappingDriver = null)
  140. {
  141. $driver = $this->getMock('Doctrine\DBAL\Driver');
  142. $driver->expects($this->once())
  143. ->method('getDatabasePlatform')
  144. ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
  145. $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
  146. $conn->expects($this->once())
  147. ->method('getEventManager')
  148. ->will($this->returnValue($this->getEventManager()));
  149. $config = $this->getMockAnnotatedORMConfig($mappingDriver);
  150. $em = EntityManager::create($conn, $config);
  151. return $em;
  152. }
  153. /**
  154. * Creates default mapping driver
  155. *
  156. * @return \Doctrine\ORM\Mapping\Driver\Driver
  157. */
  158. protected function getDefaultORMMetadataDriverImplementation()
  159. {
  160. return new AnnotationDriverORM($_ENV['annotation_reader']);
  161. }
  162. /**
  163. * Creates default mapping driver
  164. *
  165. * @return \Doctrine\ODM\MongoDB\Mapping\Driver\Driver
  166. */
  167. protected function getDefaultMongoODMMetadataDriverImplementation()
  168. {
  169. return new AnnotationDriverODM($_ENV['annotation_reader']);
  170. }
  171. /**
  172. * Build event manager
  173. *
  174. * @return EventManager
  175. */
  176. private function getEventManager()
  177. {
  178. if (is_null($this->evm)) {
  179. $this->evm = new EventManager;
  180. $this->evm->addEventSubscriber(new TreeListener);
  181. $this->evm->addEventSubscriber(new SluggableListener);
  182. $this->evm->addEventSubscriber(new LoggableListener);
  183. $this->evm->addEventSubscriber(new TranslatableListener);
  184. $this->evm->addEventSubscriber(new TimestampableListener);
  185. }
  186. return $this->evm;
  187. }
  188. /**
  189. * Get annotation mapping configuration
  190. *
  191. * @param string $dbName
  192. * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
  193. * @return Doctrine\ORM\Configuration
  194. */
  195. private function getMockAnnotatedODMMongoDBConfig($dbName, MappingDriverODM $mappingDriver = null)
  196. {
  197. $config = $this->getMock('Doctrine\\ODM\\MongoDB\\Configuration');
  198. $config->expects($this->once())
  199. ->method('getProxyDir')
  200. ->will($this->returnValue(__DIR__.'/../../temp'));
  201. $config->expects($this->once())
  202. ->method('getProxyNamespace')
  203. ->will($this->returnValue('Proxy'));
  204. $config->expects($this->once())
  205. ->method('getHydratorDir')
  206. ->will($this->returnValue(__DIR__.'/../../temp'));
  207. $config->expects($this->once())
  208. ->method('getHydratorNamespace')
  209. ->will($this->returnValue('Hydrator'));
  210. $config->expects($this->any())
  211. ->method('getDefaultDB')
  212. ->will($this->returnValue($dbName));
  213. $config->expects($this->once())
  214. ->method('getAutoGenerateProxyClasses')
  215. ->will($this->returnValue(true));
  216. $config->expects($this->once())
  217. ->method('getAutoGenerateHydratorClasses')
  218. ->will($this->returnValue(true));
  219. $config->expects($this->once())
  220. ->method('getClassMetadataFactoryName')
  221. ->will($this->returnValue('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadataFactory'));
  222. $config->expects($this->any())
  223. ->method('getMongoCmd')
  224. ->will($this->returnValue('$'));
  225. if (is_null($mappingDriver)) {
  226. $mappingDriver = $this->getDefaultMongoODMMetadataDriverImplementation();
  227. }
  228. $config->expects($this->any())
  229. ->method('getMetadataDriverImpl')
  230. ->will($this->returnValue($mappingDriver));
  231. return $config;
  232. }
  233. /**
  234. * Get annotation mapping configuration for ORM
  235. *
  236. * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
  237. * @return Doctrine\ORM\Configuration
  238. */
  239. private function getMockAnnotatedORMConfig(MappingDriverORM $mappingDriver = null)
  240. {
  241. $config = $this->getMock('Doctrine\ORM\Configuration');
  242. $config->expects($this->once())
  243. ->method('getProxyDir')
  244. ->will($this->returnValue(__DIR__.'/../../temp'));
  245. $config->expects($this->once())
  246. ->method('getProxyNamespace')
  247. ->will($this->returnValue('Proxy'));
  248. $config->expects($this->once())
  249. ->method('getAutoGenerateProxyClasses')
  250. ->will($this->returnValue(true));
  251. $config->expects($this->once())
  252. ->method('getClassMetadataFactoryName')
  253. ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'));
  254. if (is_null($mappingDriver)) {
  255. $mappingDriver = $this->getDefaultORMMetadataDriverImplementation();
  256. }
  257. $config->expects($this->any())
  258. ->method('getMetadataDriverImpl')
  259. ->will($this->returnValue($mappingDriver));
  260. return $config;
  261. }
  262. }