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