DoctrineDataCollectorTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Tests\Bridge\Doctrine\DataCollector;
  11. use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class DoctrineDataCollectorTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testCollectConnections()
  17. {
  18. $c = $this->createCollector(array());
  19. $c->collect(new Request(), new Response());
  20. $this->assertEquals(array('default' => 'doctrine.dbal.default_connection'), $c->getConnections());
  21. }
  22. public function testCollectManagers()
  23. {
  24. $c = $this->createCollector(array());
  25. $c->collect(new Request(), new Response());
  26. $this->assertEquals(array('default' => 'doctrine.orm.default_entity_manager'), $c->getManagers());
  27. }
  28. public function testCollectQueryCount()
  29. {
  30. $c = $this->createCollector(array());
  31. $c->collect(new Request(), new Response());
  32. $this->assertEquals(0, $c->getQueryCount());
  33. $queries = array(
  34. array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 0)
  35. );
  36. $c = $this->createCollector($queries);
  37. $c->collect(new Request(), new Response());
  38. $this->assertEquals(1, $c->getQueryCount());
  39. }
  40. public function testCollectTime()
  41. {
  42. $c = $this->createCollector(array());
  43. $c->collect(new Request(), new Response());
  44. $this->assertEquals(0, $c->getTime());
  45. $queries = array(
  46. array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 1)
  47. );
  48. $c = $this->createCollector($queries);
  49. $c->collect(new Request(), new Response());
  50. $this->assertEquals(1, $c->getTime());
  51. $queries = array(
  52. array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 1),
  53. array('sql' => "SELECT * FROM table2", 'params' => array(), 'types' => array(), 'executionMS' => 2)
  54. );
  55. $c = $this->createCollector($queries);
  56. $c->collect(new Request(), new Response());
  57. $this->assertEquals(3, $c->getTime());
  58. }
  59. /**
  60. * @dataProvider paramProvider
  61. */
  62. public function testCollectQueries($param, $expected)
  63. {
  64. $queries = array(
  65. array('sql' => "SELECT * FROM table1 WHERE field1 = ?1", 'params' => array($param), 'types' => array(), 'executionMS' => 1)
  66. );
  67. $c = $this->createCollector($queries);
  68. $c->collect(new Request(), new Response());
  69. $collected_queries = $c->getQueries();
  70. $this->assertEquals($expected, $collected_queries[0]['params'][0]);
  71. }
  72. /**
  73. * @dataProvider paramProvider
  74. */
  75. public function testSerialization($param, $expected)
  76. {
  77. $queries = array(
  78. array('sql' => "SELECT * FROM table1 WHERE field1 = ?1", 'params' => array($param), 'types' => array(), 'executionMS' => 1)
  79. );
  80. $c = $this->createCollector($queries);
  81. $c->collect(new Request(), new Response());
  82. $c = unserialize(serialize($c));
  83. $collected_queries = $c->getQueries();
  84. $this->assertEquals($expected, $collected_queries[0]['params'][0]);
  85. }
  86. public function paramProvider()
  87. {
  88. return array(
  89. array('some value', 'some value'),
  90. array(1, '1'),
  91. array(true, 'true'),
  92. array(null, 'null'),
  93. array(new \stdClass(), 'Object(stdClass)'),
  94. array(fopen(__FILE__, 'r'), 'Resource(stream)'),
  95. array(new \SplFileInfo(__FILE__), 'Object(SplFileInfo)'),
  96. );
  97. }
  98. private function createCollector($queries)
  99. {
  100. $registry = $this->getMock('Symfony\Bridge\Doctrine\RegistryInterface');
  101. $registry
  102. ->expects($this->any())
  103. ->method('getConnectionNames')
  104. ->will($this->returnValue(array('default' => 'doctrine.dbal.default_connection')));
  105. $registry
  106. ->expects($this->any())
  107. ->method('getEntityManagerNames')
  108. ->will($this->returnValue(array('default' => 'doctrine.orm.default_entity_manager')));
  109. $logger = $this->getMock('Symfony\Bridge\Doctrine\Logger\DbalLogger');
  110. $logger->queries = $queries;
  111. return new DoctrineDataCollector($registry, $logger);
  112. }
  113. }