|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Umbrellio\Postgres\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @codeCoverageIgnore |
|
11
|
|
|
*/ |
|
12
|
|
|
trait IndexAssertions |
|
13
|
|
|
{ |
|
14
|
|
|
abstract public static function assertNotNull($actual, string $message = ''): void; |
|
15
|
|
|
|
|
16
|
|
|
abstract public static function assertSame($expected, $actual, string $message = ''): void; |
|
17
|
|
|
|
|
18
|
|
|
abstract public static function assertNull($actual, string $message = ''): void; |
|
19
|
|
|
|
|
20
|
|
|
abstract public static function assertMatchesRegularExpression( |
|
21
|
|
|
string $pattern, |
|
22
|
|
|
string $string, |
|
23
|
|
|
string $message = '' |
|
24
|
|
|
): void; |
|
25
|
|
|
|
|
26
|
|
|
abstract public static function assertTrue($condition, string $message = ''): void; |
|
27
|
|
|
|
|
28
|
|
|
abstract public static function assertFalse($condition, string $message = ''): void; |
|
29
|
|
|
|
|
30
|
|
|
protected function seeIndex(string $index): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->assertNotNull($this->getIndexListing($index)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function notSeeIndex(string $index): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->assertNull($this->getIndexListing($index)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function assertSameIndex(string $index, string $expectedDef): void |
|
41
|
|
|
{ |
|
42
|
|
|
$definition = $this->getIndexListing($index); |
|
43
|
|
|
|
|
44
|
|
|
$this->seeIndex($index); |
|
45
|
|
|
$this->assertSame($expectedDef, $definition); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function assertRegExpIndex(string $index, string $expectedDef): void |
|
49
|
|
|
{ |
|
50
|
|
|
$definition = $this->getIndexListing($index); |
|
51
|
|
|
|
|
52
|
|
|
$this->seeIndex($index); |
|
53
|
|
|
$this->assertMatchesRegularExpression($expectedDef, $definition ?: ''); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function dontSeeConstraint(string $table, string $index): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->assertFalse($this->existConstraintOnTable($table, $index)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function seeConstraint(string $table, string $index): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->assertTrue($this->existConstraintOnTable($table, $index)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getIndexListing($index): ?string |
|
67
|
|
|
{ |
|
68
|
|
|
$definition = DB::selectOne('SELECT * FROM pg_indexes WHERE indexname = ?', [$index]); |
|
69
|
|
|
|
|
70
|
|
|
return $definition ? $definition->indexdef : null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function existConstraintOnTable(string $table, string $index): bool |
|
74
|
|
|
{ |
|
75
|
|
|
$expression = ' |
|
76
|
|
|
SELECT c.conname |
|
77
|
|
|
FROM pg_constraint c |
|
78
|
|
|
LEFT JOIN pg_class t ON c.conrelid = t.oid |
|
79
|
|
|
LEFT JOIN pg_class t2 ON c.confrelid = t2.oid |
|
80
|
|
|
WHERE t.relname = ? AND c.conname = ?; |
|
81
|
|
|
'; |
|
82
|
|
|
$definition = DB::selectOne($expression, [$table, $index]); |
|
83
|
|
|
return $definition ? true : false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|