-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Improve queries parameters display in Profiler #34384
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Symfony/Bridge/Doctrine/DataCollector/ObjectParameter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?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\Bridge\Doctrine\DataCollector; | ||
|
||
final class ObjectParameter | ||
{ | ||
private $object; | ||
private $error; | ||
private $stringable; | ||
private $class; | ||
|
||
/** | ||
* @param object $object | ||
*/ | ||
public function __construct($object, ?\Throwable $error) | ||
{ | ||
$this->object = $object; | ||
$this->error = $error; | ||
$this->stringable = \is_callable([$object, '__toString']); | ||
$this->class = \get_class($object); | ||
} | ||
|
||
/** | ||
* @return object | ||
*/ | ||
public function getObject() | ||
{ | ||
return $this->object; | ||
} | ||
|
||
public function getError(): ?\Throwable | ||
{ | ||
return $this->error; | ||
} | ||
|
||
public function isStringable(): bool | ||
{ | ||
return $this->stringable; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->class; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\VarDumper\Cloner\Data; | ||
use Symfony\Component\VarDumper\Dumper\CliDumper; | ||
|
||
class DoctrineDataCollectorTest extends TestCase | ||
{ | ||
|
@@ -74,7 +75,7 @@ public function testCollectTime() | |
/** | ||
* @dataProvider paramProvider | ||
*/ | ||
public function testCollectQueries($param, $types, $expected, $explainable) | ||
public function testCollectQueries($param, $types, $expected, $explainable, bool $runnable = true) | ||
{ | ||
$queries = [ | ||
['sql' => 'SELECT * FROM table1 WHERE field1 = ?1', 'params' => [$param], 'types' => $types, 'executionMS' => 1], | ||
|
@@ -83,8 +84,19 @@ public function testCollectQueries($param, $types, $expected, $explainable) | |
$c->collect(new Request(), new Response()); | ||
|
||
$collectedQueries = $c->getQueries(); | ||
$this->assertEquals($expected, $collectedQueries['default'][0]['params'][0]); | ||
|
||
$collectedParam = $collectedQueries['default'][0]['params'][0]; | ||
if ($collectedParam instanceof Data) { | ||
$dumper = new CliDumper($out = fopen('php://memory', 'r+b')); | ||
$dumper->setColors(false); | ||
$collectedParam->dump($dumper); | ||
$this->assertStringMatchesFormat($expected, print_r(stream_get_contents($out, -1, 0), true)); | ||
} else { | ||
$this->assertEquals($expected, $collectedParam); | ||
} | ||
|
||
$this->assertEquals($explainable, $collectedQueries['default'][0]['explainable']); | ||
$this->assertSame($runnable, $collectedQueries['default'][0]['runnable']); | ||
} | ||
|
||
public function testCollectQueryWithNoParams() | ||
|
@@ -100,9 +112,11 @@ public function testCollectQueryWithNoParams() | |
$this->assertInstanceOf(Data::class, $collectedQueries['default'][0]['params']); | ||
$this->assertEquals([], $collectedQueries['default'][0]['params']->getValue()); | ||
$this->assertTrue($collectedQueries['default'][0]['explainable']); | ||
$this->assertTrue($collectedQueries['default'][0]['runnable']); | ||
$this->assertInstanceOf(Data::class, $collectedQueries['default'][1]['params']); | ||
$this->assertEquals([], $collectedQueries['default'][1]['params']->getValue()); | ||
$this->assertTrue($collectedQueries['default'][1]['explainable']); | ||
$this->assertTrue($collectedQueries['default'][1]['runnable']); | ||
} | ||
|
||
public function testCollectQueryWithNoTypes() | ||
|
@@ -134,7 +148,7 @@ public function testReset() | |
/** | ||
* @dataProvider paramProvider | ||
*/ | ||
public function testSerialization($param, $types, $expected, $explainable) | ||
public function testSerialization($param, $types, $expected, $explainable, bool $runnable = true) | ||
{ | ||
$queries = [ | ||
['sql' => 'SELECT * FROM table1 WHERE field1 = ?1', 'params' => [$param], 'types' => $types, 'executionMS' => 1], | ||
9E88
|
@@ -144,8 +158,19 @@ public function testSerialization($param, $types, $expected, $explainable) | |
$c = unserialize(serialize($c)); | ||
|
||
$collectedQueries = $c->getQueries(); | ||
$this->assertEquals($expected, $collectedQueries['default'][0]['params'][0]); | ||
|
||
$collectedParam = $collectedQueries['default'][0]['params'][0]; | ||
if ($collectedParam instanceof Data) { | ||
$dumper = new CliDumper($out = fopen('php://memory', 'r+b')); | ||
$dumper->setColors(false); | ||
$collectedParam->dump($dumper); | ||
$this->assertStringMatchesFormat($expected, print_r(stream_get_contents($out, -1, 0), true)); | ||
} else { | ||
$this->assertEquals($expected, $collectedParam); | ||
} | ||
|
||
$this->assertEquals($explainable, $collectedQueries['default'][0]['explainable']); | ||
$this->assertSame($runnable, $collectedQueries['default'][0]['runnable']); | ||
} | ||
|
||
public function paramProvider() | ||
|
@@ -156,19 +181,46 @@ public function paramProvider() | |
[true, [], true, true], | ||
[null, [], null, true], | ||
[new \DateTime('2011-09-11'), ['date'], '2011-09-11', true], | ||
[fopen(__FILE__, 'r'), [], '/* Resource(stream) */', false], | ||
[new \stdClass(), [], '/* Object(stdClass) */', false], | ||
[fopen(__FILE__, 'r'), [], '/* Resource(stream) */', false, false], | ||
[ | ||
new \stdClass(), | ||
[], | ||
<<<EOTXT | ||
{#%d | ||
⚠: "Object of class "stdClass" could not be converted to string." | ||
} | ||
EOTXT | ||
, | ||
false, | ||
false, | ||
], | ||
[ | ||
new StringRepresentableClass(), | ||
[], | ||
'/* Object(Symfony\Bridge\Doctrine\Tests\DataCollector\StringRepresentableClass): */"string representation"', | ||
<<<EOTXT | ||
Symfony\Bridge\Doctrine\Tests\DataCollector\StringRepresentableClass {#%d | ||
__toString(): "string representation" | ||
} | ||
EOTXT | ||
, | ||
false, | ||
], | ||
]; | ||
|
||
if (version_compare(Version::VERSION, '2.6', '>=')) { | ||
$tests[] = ['this is not a date', ['date'], 'this is not a date', false]; | ||
$tests[] = [new \stdClass(), ['date'], '/* Object(stdClass) */', false]; | ||
$tests[] = ['this is not a date', ['date'], "⚠ Could not convert PHP value 'this is not a date' of type 'string' to type 'date'. Expected one of the following types: null, DateTime", false, false]; | ||
$tests[] = [ | ||
new \stdClass(), | ||
['date'], | ||
<<<EOTXT | ||
{#%d | ||
⚠: "Could not convert PHP value of type 'stdClass' to type 'date'. Expected one of the following types: null, DateTime" | ||
} | ||
EOTXT | ||
, | ||
false, | ||
false, | ||
]; | ||
} | ||
|
||
return $tests; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.