PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/knplabs/knp-components/tests/Test/Tool/BaseTestCaseORM.php

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