-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabaseQueryComparator.php
113 lines (96 loc) · 3.73 KB
/
DatabaseQueryComparator.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Comparators;
use Doctrine\SqlFormatter\NullHighlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use LastDragon_ru\LaraASP\Testing\Database\QueryLog\Query;
use Override;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Comparator\ObjectComparator;
use function array_column;
use function array_flip;
use function array_unique;
use function array_values;
use function mb_strlen;
use function natsort;
use function preg_match_all;
use function str_replace;
use function uksort;
use const PREG_SET_ORDER;
/**
* Compares two {@link Query}.
*
* We are performing following normalization before comparison to be more precise:
*
* * Renumber `laravel_reserved_*` (it will always start from `0` and will not contain gaps)
* * Format the query by [`doctrine/sql-formatter`](https://github.com/doctrine/sql-formatter) package
*/
class DatabaseQueryComparator extends ObjectComparator {
#[Override]
public function accepts(mixed $expected, mixed $actual): bool {
return $expected instanceof Query
&& $actual instanceof Query;
}
/**
* @param array<array-key, mixed> $processed
*/
#[Override]
public function assertEquals(
mixed $expected,
mixed $actual,
float $delta = 0.0,
bool $canonicalize = false,
bool $ignoreCase = false,
array &$processed = [],
): void {
// If classes different we just call parent to fail
if (!($actual instanceof Query) || !($expected instanceof Query) || $actual::class !== $expected::class) {
parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase, $processed);
}
// Normalize queries and compare
$normalizedExpected = $this->normalize($expected);
$normalizedActual = $this->normalize($actual);
try {
parent::assertEquals(
$normalizedExpected,
$normalizedActual,
$delta,
$canonicalize,
$ignoreCase,
$processed,
);
} catch (ComparisonFailure $exception) {
throw new ComparisonFailure(
expected : $normalizedExpected,
actual : $normalizedActual,
expectedAsString: $exception->getExpectedAsString(),
actualAsString : $exception->getActualAsString(),
message : 'Failed asserting that two database queries are equal.',
);
}
}
protected function normalize(Query $query): Query {
// Prepare
$class = $query::class;
$sql = $query->getQuery();
$bindings = $query->getBindings();
// Laravel's aliases have a global counter and are dependent on tests
// execution order -> we need to normalize them before comparison.
if (preg_match_all('/(?<group>laravel_reserved_[\d]+)/', $sql, $matches, PREG_SET_ORDER) > 0) {
$matches = array_unique(array_column($matches, 'group'));
natsort($matches);
$matches = array_values($matches);
$matches = array_flip($matches);
uksort($matches, static function (string|int $a, string|int $b): int {
return mb_strlen("{$b}") <=> mb_strlen("{$a}");
});
foreach ($matches as $match => $index) {
$sql = str_replace($match, "__tmp_alias_{$index}", $sql);
}
$sql = str_replace('__tmp_alias_', 'laravel_reserved_', $sql);
}
// Format
$sql = (new SqlFormatter(new NullHighlighter()))->format($sql, ' ');
// Return
return new $class($sql, $bindings);
}
}