-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathQueryCollectorTest.php
88 lines (72 loc) · 3.01 KB
/
QueryCollectorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace Barryvdh\Debugbar\Tests\DataCollector;
use Barryvdh\Debugbar\Tests\TestCase;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
class QueryCollectorTest extends TestCase
{
use RefreshDatabase;
public function testItReplacesQuestionMarksBindingsCorrectly()
{
$this->loadLaravelMigrations();
debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
$collector->addQuery(new QueryExecuted(
"SELECT ('[1, 2, 3]'::jsonb ?? ?) as a, ('[4, 5, 6]'::jsonb ??| ?) as b, 'hello world ? example ??' as c",
[3, '{4}'],
0,
$this->app['db']->connection()
));
tap($collector->collect(), function (array $collection) {
$this->assertEquals(1, $collection['nb_statements']);
tap(Arr::first($collection['statements']), function (array $statement) {
$this->assertEquals([3, '{4}'], $statement['bindings']);
$this->assertEquals(<<<SQL
SELECT ('[1, 2, 3]'::jsonb ? 3) as a, ('[4, 5, 6]'::jsonb ?| '{4}') as b, 'hello world ? example ??' as c
SQL
, $statement['sql']);
});
});
}
public function testDollarBindingsArePresentedCorrectly()
{
debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
$collector->addQuery(new QueryExecuted(
"SELECT a FROM b WHERE c = ? AND d = ? AND e = ?",
['$10', '$2y$10_DUMMY_BCRYPT_HASH', '$_$$_$$$_$2_$3'],
0,
$this->app['db']->connection()
));
tap(Arr::first($collector->collect()['statements']), function (array $statement) {
$this->assertEquals(
"SELECT a FROM b WHERE c = '$10' AND d = '$2y$10_DUMMY_BCRYPT_HASH' AND e = '\$_$\$_$$\$_$2_$3'",
$statement['sql']
);
});
}
public function testFindingCorrectPathForView()
{
debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
view('query')
->with('db', $this->app['db']->connection())
->with('collector', $collector)
->render();
tap(Arr::first($collector->collect()['statements']), function (array $statement) {
$this->assertEquals(
"SELECT a FROM b WHERE c = '$10' AND d = '$2y$10_DUMMY_BCRYPT_HASH' AND e = '\$_$\$_$$\$_$2_$3'",
$statement['sql']
);
$this->assertTrue(@file_exists($statement['backtrace'][1]->file));
$this->assertEquals(
realpath(__DIR__ . '/../resources/views/query.blade.php'),
realpath($statement['backtrace'][1]->file)
);
});
}
}