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