1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest; |
6
|
|
|
|
7
|
|
|
use ApplicationTest\Traits\TestWithTransaction; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class TriggersTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
use TestWithTransaction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @dataProvider providerGetUsageCountManyToMany |
16
|
|
|
*/ |
17
|
|
|
public function testGetUsageCountManyToMany(string $table, int $id1, int $id2): void |
18
|
|
|
{ |
19
|
|
|
$initial1 = $this->getUsageCount($table, $id1); |
20
|
|
|
$initial2 = $this->getUsageCount($table, $id2); |
21
|
|
|
|
22
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
23
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
24
|
|
|
|
25
|
|
|
$this->create($table, $id1); |
26
|
|
|
$this->assertUsageCount($table, $id1, $initial1 + 1); |
27
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
28
|
|
|
|
29
|
|
|
$this->create($table, $id2); |
30
|
|
|
$this->assertUsageCount($table, $id1, $initial1 + 1); |
31
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 1); |
32
|
|
|
|
33
|
|
|
$this->delete($table, $id1); |
34
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
35
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 1); |
36
|
|
|
|
37
|
|
|
$this->delete($table, $id2); |
38
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
39
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function providerGetUsageCountManyToMany(): iterable |
43
|
|
|
{ |
44
|
|
|
yield ['antique_name', 12000, 12001]; |
45
|
|
|
yield ['artist', 3000, 3001]; |
46
|
|
|
yield ['domain', 9000, 9001]; |
47
|
|
|
yield ['material', 8000, 8001]; |
48
|
|
|
yield ['period', 7000, 7001]; |
49
|
|
|
yield ['tag', 4000, 4001]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function assertUsageCount(string $table, int $id, int $expected): void |
53
|
|
|
{ |
54
|
|
|
self::assertSame($expected, $this->getUsageCount($table, $id)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function create(string $table, int $id): void |
58
|
|
|
{ |
59
|
|
|
$field = $table . '_id'; |
60
|
|
|
$relation = "card_$table"; |
61
|
|
|
|
62
|
|
|
$affected = $this->getEntityManager()->getConnection()->executeStatement("INSERT INTO $relation (card_id, $field) VALUES (6005, $id)"); |
63
|
|
|
|
64
|
|
|
self::assertSame(1, $affected); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function delete(string $table, int $id): void |
68
|
|
|
{ |
69
|
|
|
$field = $table . '_id'; |
70
|
|
|
$relation = "card_$table"; |
71
|
|
|
|
72
|
|
|
$affected = $this->getEntityManager()->getConnection()->executeStatement("DELETE FROM $relation WHERE card_id = 6005 AND $field = $id"); |
73
|
|
|
|
74
|
|
|
self::assertSame(1, $affected); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getUsageCount(string $table, int $id): mixed |
78
|
|
|
{ |
79
|
|
|
return $this->getEntityManager()->getConnection()->fetchOne("SELECT usage_count FROM `$table` WHERE id = $id"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @dataProvider providerGetUsageCountManyToOne |
84
|
|
|
*/ |
85
|
|
|
public function testGetUsageCountManyToOne(string $table, int $id1, int $id2): void |
86
|
|
|
{ |
87
|
|
|
$initial1 = $this->getUsageCount($table, $id1); |
88
|
|
|
$initial2 = $this->getUsageCount($table, $id2); |
89
|
|
|
|
90
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
91
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
92
|
|
|
|
93
|
|
|
$this->update($table, 6005, $id1); |
94
|
|
|
$this->assertUsageCount($table, $id1, $initial1 + 1); |
95
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
96
|
|
|
|
97
|
|
|
$this->update($table, 6005, $id2); |
98
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
99
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 1); |
100
|
|
|
|
101
|
|
|
$this->update($table, 6012, $id1); |
102
|
|
|
$this->assertUsageCount($table, $id1, $initial1 + 1); |
103
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 1); |
104
|
|
|
|
105
|
|
|
$this->update($table, 6012, $id2); |
106
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
107
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 2); |
108
|
|
|
|
109
|
|
|
$this->update($table, 6005, null); |
110
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
111
|
|
|
$this->assertUsageCount($table, $id2, $initial2 + 1); |
112
|
|
|
|
113
|
|
|
$this->update($table, 6012, null); |
114
|
|
|
$this->assertUsageCount($table, $id1, $initial1); |
115
|
|
|
$this->assertUsageCount($table, $id2, $initial2); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function providerGetUsageCountManyToOne(): iterable |
119
|
|
|
{ |
120
|
|
|
yield ['institution', 5000, 5001, true]; |
121
|
|
|
yield ['document_type', 11000, 11001, true]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function update(string $table, int $cardId, ?int $id): void |
125
|
|
|
{ |
126
|
|
|
$field = $table . '_id'; |
127
|
|
|
|
128
|
|
|
$id ??= 'NULL'; |
129
|
|
|
|
130
|
|
|
$affected = $this->getEntityManager()->getConnection()->executeStatement("UPDATE card SET $field = $id WHERE id = $cardId"); |
131
|
|
|
|
132
|
|
|
self::assertSame(1, $affected); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|