8000 Integrates #2733 into master branch by aboks · Pull Request #2820 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Integrates #2733 into master branch #2820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(ManagerRegistry $registry, DbalLogger $logger = null
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = array(
'queries' => null !== $this->logger ? $this->logger->queries : array(),
'queries' => null !== $this->logger ? $this->sanitizeQueries($this->logger->queries) : array(),
'connections' => $this->connections,
'managers' => $this->managers,
);
Expand Down Expand Up @@ -84,4 +84,49 @@ public function getName()
{
return 'db';
}

private function sanitizeQueries($queries)
{
foreach ($queries as $i => $query) {
foreach ($query['params'] as $j => $param) {
$queries[$i]['params'][$j] = $this->varToString($param);
}
}

return $queries;
}

private function varToString($var)
{
if (is_object($var)) {
return sprintf('Object(%s)', get_class($var));
}

if (is_array($var)) {
$a = array();
foreach ($var as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
}

return sprintf("Array(%s)", implode(', ', $a));
}

if (is_resource($var)) {
return sprintf('Resource(%s)', get_resource_type($var));
}

if (null === $var) {
return 'null';
}

if (false === $var) {
return 'false';
}

if (true === $var) {
return 'true';
}

return (string) $var;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<code>{{ query.sql }}</code>
</div>
<small>
<strong>Parameters</strong>: {{ query.params|yaml_encode }}<br />
<strong>Parameters</strong>: {{ query.params }}<br />
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Bridge\Doctrine\DataCollector;

use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DoctrineDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollectConnections()
{
$c = $this->createCollector(array());
$c->collect(new Request(), new Response());
$this->assertEquals(array('default' => 'doctrine.dbal.default_connection'), $c->getConnections());
}

public function testCollectManagers()
{
$c = $this->createCollector(array());
$c->collect(new Request(), new Response());
$this->assertEquals(array('default' => 'doctrine.orm.default_entity_manager'), $c->getManagers());
}

public function testCollectQueryCount()
{
$c = $this->createCollector(array());
$c->collect(new Request(), new Response());
$this->assertEquals(0, $c->getQueryCount());

$queries = array(
array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 0)
);
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$this->assertEquals(1, $c->getQueryCount());
}

public function testCollectTime()
{
$c = $this->createCollector(array());
$c->collect(new Request(), new Response());
$this->assertEquals(0, $c->getTime());

$queries = array(
array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 1)
);
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$this->assertEquals(1, $c->getTime());

$queries = array(
array('sql' => "SELECT * FROM table1", 'params' => array(), 'types' => array(), 'executionMS' => 1),
array('sql' => CF6C "SELECT * FROM table2", 'params' => array(), 'types' => array(), 'executionMS' => 2)
);
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$this->assertEquals(3, $c->getTime());
}

/**
* @dataProvider paramProvider
*/
public function testCollectQueries($param, $expected)
{
$queries = array(
array('sql' => "SELECT * FROM table1 WHERE field1 = ?1", 'params' => array($param), 'types' => array(), 'executionMS' => 1)
);
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());

$collected_queries = $c->getQueries();
$this->assertEquals($expected, $collected_queries[0]['params'][0]);
}

/**
* @dataProvider paramProvider
*/
public function testSerialization($param, $expected)
{
$queries = array(
array('sql' => "SELECT * FROM table1 WHERE field1 = ?1", 'params' => array($param), 'types' => array(), 'executionMS' => 1)
);
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));

$collected_queries = $c->getQueries();
$this->assertEquals($expected, $collected_queries[0]['params'][0]);
}

public function paramProvider()
{
return array(
array('some value', 'some value'),
array(1, '1'),
array(true, 'true'),
array(null, 'null'),
array(new \stdClass(), 'Object(stdClass)'),
array(fopen(__FILE__, 'r'), 'Resource(stream)'),
array(new \SplFileInfo(__FILE__), 'Object(SplFileInfo)'),
);
}

private function createCollector($queries)
{
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$registry
->expects($this->any())
->method('getConnectionNames')
->will($this->returnValue(array('default' => 'doctrine.dbal.default_connection')));
$registry
->expects($this->any())
->method('getManagerNames')
->will($this->returnValue(array('default' => 'doctrine.orm.default_entity_manager')));

$logger = $this->getMock('Symfony\Bridge\Doctrine\Logger\DbalLogger');
$logger->queries = $queries;

return new DoctrineDataCollector($registry, $logger);
}
}
0