BaseTestCaseORM.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace Tool;
  3. use Gedmo\Tool\Logging\DBAL\QueryAnalyzer;
  4. use Doctrine\Common\Annotations\AnnotationReader;
  5. use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\Common\EventManager;
  8. use Doctrine\ORM\Tools\SchemaTool;
  9. use Gedmo\Translatable\TranslatableListener;
  10. use Gedmo\Sluggable\SluggableListener;
  11. use Gedmo\Tree\TreeListener;
  12. use Gedmo\Timestampable\TimestampableListener;
  13. use Gedmo\Loggable\LoggableListener;
  14. /**
  15. * Base test case contains common mock objects
  16. * and functionality among all extensions using
  17. * ORM object manager
  18. *
  19. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  20. * @package Gedmo
  21. * @subpackage BaseTestCase
  22. * @link http://www.gediminasm.org
  23. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  24. */
  25. abstract class BaseTestCaseORM extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var EntityManager
  29. */
  30. protected $em;
  31. /**
  32. * @var QueryAnalyzer
  33. */
  34. protected $queryAnalyzer;
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function setUp()
  39. {
  40. }
  41. /**
  42. * EntityManager mock object together with
  43. * annotation mapping driver and pdo_sqlite
  44. * database in memory
  45. *
  46. * @param EventManager $evm
  47. * @return EntityManager
  48. */
  49. protected function getMockSqliteEntityManager(EventManager $evm = null)
  50. {
  51. $conn = array(
  52. 'driver' => 'pdo_sqlite',
  53. 'memory' => true,
  54. );
  55. $config = $this->getMockAnnotatedConfig();
  56. $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager());
  57. $schema = array_map(function($class) use ($em) {
  58. return $em->getClassMetadata($class);
  59. }, (array)$this->getUsedEntityFixtures());
  60. $schemaTool = new SchemaTool($em);
  61. $schemaTool->dropSchema(array());
  62. $schemaTool->createSchema($schema);
  63. return $this->em = $em;
  64. }
  65. /**
  66. * EntityManager mock object together with
  67. * annotation mapping driver and custom
  68. * connection
  69. *
  70. * @param array $conn
  71. * @param EventManager $evm
  72. * @return EntityManager
  73. */
  74. protected function getMockCustomEntityManager(array $conn, EventManager $evm = null)
  75. {
  76. $config = $this->getMockAnnotatedConfig();
  77. $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager());
  78. $schema = array_map(function($class) use ($em) {
  79. return $em->getClassMetadata($class);
  80. }, (array)$this->getUsedEntityFixtures());
  81. $schemaTool = new SchemaTool($em);
  82. $schemaTool->dropSchema(array());
  83. $schemaTool->createSchema($schema);
  84. return $this->em = $em;
  85. }
  86. /**
  87. * EntityManager mock object with
  88. * annotation mapping driver
  89. *
  90. * @param EventManager $evm
  91. * @return EntityManager
  92. */
  93. protected function getMockMappedEntityManager(EventManager $evm = null)
  94. {
  95. $driver = $this->getMock('Doctrine\DBAL\Driver');
  96. $driver->expects($this->once())
  97. ->method('getDatabasePlatform')
  98. ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
  99. $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
  100. $conn->expects($this->once())
  101. ->method('getEventManager')
  102. ->will($this->returnValue($evm ?: $this->getEventManager()));
  103. $config = $this->getMockAnnotatedConfig();
  104. $this->em = EntityManager::create($conn, $config);
  105. return $this->em;
  106. }
  107. /**
  108. * Starts query statistic log
  109. *
  110. * @throws \RuntimeException
  111. */
  112. protected function startQueryLog()
  113. {
  114. if (!$this->em || !$this->em->getConnection()->getDatabasePlatform()) {
  115. throw new \RuntimeException('EntityManager and database platform must be initialized');
  116. }
  117. $this->queryAnalyzer = new QueryAnalyzer($this->em->getConnection()->getDatabasePlatform());
  118. $this->em
  119. ->getConfiguration()
  120. ->expects($this->any())
  121. ->method('getSQLLogger')
  122. ->will($this->returnValue($this->queryAnalyzer));
  123. }
  124. /**
  125. * Stops query statistic log and outputs
  126. * the data to screen or file
  127. *
  128. * @param boolean $dumpOnlySql
  129. * @param boolean $writeToLog
  130. * @throws \RuntimeException
  131. */
  132. protected function stopQueryLog($dumpOnlySql = false, $writeToLog = false)
  133. {
  134. if ($this->queryAnalyzer) {
  135. $output = $this->queryAnalyzer->getOutput($dumpOnlySql);
  136. if ($writeToLog) {
  137. $fileName = __DIR__.'/../../temp/query_debug_'.time().'.log';
  138. if (($file = fopen($fileName, 'w+')) !== false) {
  139. fwrite($file, $output);
  140. fclose($file);
  141. } else {
  142. throw new \RuntimeException('Unable to write to the log file');
  143. }
  144. } else {
  145. echo $output;
  146. }
  147. }
  148. }
  149. /**
  150. * Creates default mapping driver
  151. *
  152. * @return \Doctrine\ORM\Mapping\Driver\Driver
  153. */
  154. protected function getMetadataDriverImplementation()
  155. {
  156. return new AnnotationDriver($_ENV['annotation_reader']);
  157. }
  158. /**
  159. * Get a list of used fixture classes
  160. *
  161. * @return array
  162. */
  163. abstract protected function getUsedEntityFixtures();
  164. /**
  165. * Build event manager
  166. *
  167. * @return EventManager
  168. */
  169. private function getEventManager()
  170. {
  171. $evm = new EventManager;
  172. $evm->addEventSubscriber(new TreeListener);
  173. $evm->addEventSubscriber(new SluggableListener);
  174. $evm->addEventSubscriber(new LoggableListener);
  175. $evm->addEventSubscriber(new TranslatableListener);
  176. $evm->addEventSubscriber(new TimestampableListener);
  177. return $evm;
  178. }
  179. /**
  180. * Get annotation mapping configuration
  181. *
  182. * @return Doctrine\ORM\Configuration
  183. */
  184. private function getMockAnnotatedConfig()
  185. {
  186. $config = $this->getMock('Doctrine\ORM\Configuration');
  187. $config
  188. ->expects($this->once())
  189. ->method('getProxyDir')
  190. ->will($this->returnValue(__DIR__.'/../../temp'))
  191. ;
  192. $config
  193. ->expects($this->once())
  194. ->method('getProxyNamespace')
  195. ->will($this->returnValue('Proxy'))
  196. ;
  197. $config
  198. ->expects($this->once())
  199. ->method('getAutoGenerateProxyClasses')
  200. ->will($this->returnValue(true))
  201. ;
  202. $config
  203. ->expects($this->once())
  204. ->method('getClassMetadataFactoryName')
  205. ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'))
  206. ;
  207. $mappingDriver = $this->getMetadataDriverImplementation();
  208. $config
  209. ->expects($this->any())
  210. ->method('getMetadataDriverImpl')
  211. ->will($this->returnValue($mappingDriver))
  212. ;
  213. $config
  214. ->expects($this->any())
  215. ->method('getDefaultRepositoryClassName')
  216. ->will($this->returnValue('Doctrine\\ORM\\EntityRepository'))
  217. ;
  218. return $config;
  219. }
  220. }