DoctrineDataCollectorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests\DataCollector;
  11. use Doctrine\DBAL\Platforms\MySqlPlatform;
  12. use Doctrine\DBAL\Version;
  13. use PHPUnit\Framework\TestCase;
  14. use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class DoctrineDataCollectorTest extends TestCase
  18. {
  19. public function testCollectConnections()
  20. {
  21. $c = $this->createCollector(array());
  22. $c->collect(new Request(), new Response());
  23. $this->assertEquals(array('default' => 'doctrine.dbal.default_connection'), $c->getConnections());
  24. }
  25. public function testCollectManagers()
  26. {
  27. $c = $this->createCollector(array());
  28. $c->collect(new Request(), new Response());
  29. $this->assertEquals(array('default' => 'doctrine.orm.default_entity_manager'), $c->getManagers());
  30. }
  31. public function testCollectQueryCount()
  32. {
  33. $c = $this->createCollector(array());
  34. $c->collect(new Request(), new Response());
  35. $this->assertEquals(0, $c->getQueryCount());
  36. $queries = array(
  37. array('sql' => 'SELECT * FROM table1', 'params' => array(), 'types' => array(), 'executionMS' => 0),
  38. );
  39. $c = $this->createCollector($queries);
  40. $c->collect(new Request(), new Response());
  41. $this->assertEquals(1, $c->getQueryCount());
  42. }
  43. public function testCollectTime()
  44. {
  45. $c = $this->createCollector(array());
  46. $c->collect(new Request(), new Response());
  47. $this->assertEquals(0, $c->getTime());
  48. $queries = array(
  49. array('sql' => 'SELECT * FROM table1', 'params' => array(), 'types' => array(), 'executionMS' => 1),
  50. );
  51. $c = $this->createCollector($queries);
  52. $c->collect(new Request(), new Response());
  53. $this->assertEquals(1, $c->getTime());
  54. $queries = array(
  55. array('sql' => 'SELECT * FROM table1', 'params' => array(), 'types' => array(), 'executionMS' => 1),
  56. array('sql' => 'SELECT * FROM table2', 'params' => array(), 'types' => array(), 'executionMS' => 2),
  57. );
  58. $c = $this->createCollector($queries);
  59. $c->collect(new Request(), new Response());
  60. $this->assertEquals(3, $c->getTime());
  61. }
  62. /**
  63. * @dataProvider paramProvider
  64. */
  65. public function testCollectQueries($param, $types, $expected, $explainable)
  66. {
  67. $queries = array(
  68. array('sql' => 'SELECT * FROM table1 WHERE field1 = ?1', 'params' => array($param), 'types' => $types, 'executionMS' => 1),
  69. );
  70. $c = $this->createCollector($queries);
  71. $c->collect(new Request(), new Response());
  72. $collectedQueries = $c->getQueries();
  73. $this->assertEquals($expected, $collectedQueries['default'][0]['params'][0]);
  74. $this->assertEquals($explainable, $collectedQueries['default'][0]['explainable']);
  75. }
  76. public function testCollectQueryWithNoParams()
  77. {
  78. $queries = array(
  79. array('sql' => 'SELECT * FROM table1', 'params' => array(), 'types' => array(), 'executionMS' => 1),
  80. array('sql' => 'SELECT * FROM table1', 'params' => null, 'types' => null, 'executionMS' => 1),
  81. );
  82. $c = $this->createCollector($queries);
  83. $c->collect(new Request(), new Response());
  84. $collectedQueries = $c->getQueries();
  85. $this->assertEquals(array(), $collectedQueries['default'][0]['params']);
  86. $this->assertTrue($collectedQueries['default'][0]['explainable']);
  87. $this->assertEquals(array(), $collectedQueries['default'][1]['params']);
  88. $this->assertTrue($collectedQueries['default'][1]['explainable']);
  89. }
  90. /**
  91. * @dataProvider paramProvider
  92. */
  93. public function testSerialization($param, $types, $expected, $explainable)
  94. {
  95. $queries = array(
  96. array('sql' => 'SELECT * FROM table1 WHERE field1 = ?1', 'params' => array($param), 'types' => $types, 'executionMS' => 1),
  97. );
  98. $c = $this->createCollector($queries);
  99. $c->collect(new Request(), new Response());
  100. $c = unserialize(serialize($c));
  101. $collectedQueries = $c->getQueries();
  102. $this->assertEquals($expected, $collectedQueries['default'][0]['params'][0]);
  103. $this->assertEquals($explainable, $collectedQueries['default'][0]['explainable']);
  104. }
  105. public function paramProvider()
  106. {
  107. $tests = array(
  108. array('some value', array(), 'some value', true),
  109. array(1, array(), 1, true),
  110. array(true, array(), true, true),
  111. array(null, array(), null, true),
  112. array(new \DateTime('2011-09-11'), array('date'), '2011-09-11', true),
  113. array(fopen(__FILE__, 'r'), array(), 'Resource(stream)', false),
  114. array(new \SplFileInfo(__FILE__), array(), 'Object(SplFileInfo)', false),
  115. );
  116. if (version_compare(Version::VERSION, '2.6', '>=')) {
  117. $tests[] = array('this is not a date', array('date'), 'this is not a date', false);
  118. $tests[] = array(new \stdClass(), array('date'), 'Object(stdClass)', false);
  119. }
  120. return $tests;
  121. }
  122. private function createCollector($queries)
  123. {
  124. $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $connection->expects($this->any())
  128. ->method('getDatabasePlatform')
  129. ->will($this->returnValue(new MySqlPlatform()));
  130. $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
  131. $registry
  132. ->expects($this->any())
  133. ->method('getConnectionNames')
  134. ->will($this->returnValue(array('default' => 'doctrine.dbal.default_connection')));
  135. $registry
  136. ->expects($this->any())
  137. ->method('getManagerNames')
  138. ->will($this->returnValue(array('default' => 'doctrine.orm.default_entity_manager')));
  139. $registry->expects($this->any())
  140. ->method('getConnection')
  141. ->will($this->returnValue($connection));
  142. $logger = $this->getMockBuilder('Doctrine\DBAL\Logging\DebugStack')->getMock();
  143. $logger->queries = $queries;
  144. $collector = new DoctrineDataCollector($registry);
  145. $collector->addLogger('default', $logger);
  146. return $collector;
  147. }
  148. }