|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\Tests\Integration\PackagePrivate\Util; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Wikibase\TermStore\MediaWiki\PackagePrivate\Util\ReplicaMasterAwareRecordIdsAcquirer; |
|
7
|
|
|
use Wikimedia\Rdbms\IDatabase; |
|
8
|
|
|
use Wikimedia\Rdbms\DatabaseSqlite; |
|
9
|
|
|
|
|
10
|
|
|
class ReplicaMasterAwareRecordIdsAcquirerTest extends TestCase { |
|
11
|
|
|
|
|
12
|
|
|
const TABLE_DDL_FILE_PATH = __DIR__ . '/ReplicaMasterAwareRecordIdsAcquirerTest_tableDDL.sql'; |
|
13
|
|
|
const TABLE_NAME = 'replica_master_aware_record_ids_acquirer_test'; |
|
14
|
|
|
const ID_COLUMN = 'id'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var IDatabase $dbMaster |
|
18
|
|
|
*/ |
|
19
|
|
|
private $dbMaster; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var IDatabase $dbReplica |
|
23
|
|
|
*/ |
|
24
|
|
|
private $dbReplica; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp() { |
|
27
|
|
|
$this->dbMaster = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
|
28
|
|
|
$this->dbMaster->sourceFile( self::TABLE_DDL_FILE_PATH ); |
|
29
|
|
|
|
|
30
|
|
|
$this->dbReplica = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
|
31
|
|
|
$this->dbReplica->sourceFile( self::TABLE_DDL_FILE_PATH ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testWhenAllRecordsExistInReplica() { |
|
35
|
|
|
$records = $this->getTestRecords(); |
|
36
|
|
|
|
|
37
|
|
|
$this->dbReplica->insert( |
|
38
|
|
|
self::TABLE_NAME, |
|
39
|
|
|
$records |
|
40
|
|
|
); |
|
41
|
|
|
$this->assertSameRecordsInDb( $records, $this->dbReplica ); |
|
42
|
|
|
|
|
43
|
|
|
$idsAcquirer = $this->getTestSubjectInstance(); |
|
44
|
|
|
$acquiredRecordsWithIds = $idsAcquirer->acquireIds( $records ); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertNoRecordsInDb( $records, $this->dbMaster ); |
|
47
|
|
|
$this->assertSameRecordsInDb( $acquiredRecordsWithIds, $this->dbReplica ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testWhenAllRecordsExistInMaster() { |
|
51
|
|
|
$records = $this->getTestRecords(); |
|
52
|
|
|
|
|
53
|
|
|
$this->dbMaster->insert( |
|
54
|
|
|
self::TABLE_NAME, |
|
55
|
|
|
$records |
|
56
|
|
|
); |
|
57
|
|
|
$this->assertSameRecordsInDb( $records, $this->dbMaster ); |
|
58
|
|
|
|
|
59
|
|
|
$idsAcquirer = $this->getTestSubjectInstance(); |
|
60
|
|
|
$acquiredRecordsWithIds = $idsAcquirer->acquireIds( $records ); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertNoRecordsInDb( $records, $this->dbReplica ); |
|
63
|
|
|
$this->assertSameRecordsInDb( $acquiredRecordsWithIds, $this->dbMaster ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testWhenAllRecordsDoNotExistInReplicaOrMaster() { |
|
67
|
|
|
$records = $this->getTestRecords(); |
|
68
|
|
|
|
|
69
|
|
|
$idsAcquirer = $this->getTestSubjectInstance(); |
|
70
|
|
|
$acquiredRecordsWithIds = $idsAcquirer->acquireIds( $records ); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertNoRecordsInDb( $records, $this->dbReplica ); |
|
73
|
|
|
$this->assertSameRecordsInDb( $acquiredRecordsWithIds, $this->dbMaster ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testWhenSomeRecordsDoNotExistInReplicaButExistInMaster() { |
|
77
|
|
|
$records = $this->getTestRecords(); |
|
78
|
|
|
|
|
79
|
|
|
$recordsInReplica = [ $records[0], $records[1] ]; |
|
80
|
|
|
$recordsInMaster = [ $records[2] ]; |
|
81
|
|
|
|
|
82
|
|
|
$this->dbReplica->insert( |
|
83
|
|
|
self::TABLE_NAME, |
|
84
|
|
|
$recordsInReplica |
|
85
|
|
|
); |
|
86
|
|
|
$this->assertSameRecordsInDb( $recordsInReplica, $this->dbReplica ); |
|
87
|
|
|
|
|
88
|
|
|
$this->dbMaster->insert( |
|
89
|
|
|
self::TABLE_NAME, |
|
90
|
|
|
$recordsInMaster |
|
91
|
|
|
); |
|
92
|
|
|
$this->assertSameRecordsInDb( $recordsInMaster, $this->dbMaster ); |
|
93
|
|
|
|
|
94
|
|
|
$idsAcquirer = $this->getTestSubjectInstance(); |
|
95
|
|
|
$acquiredRecordsWithIds = $idsAcquirer->acquireIds( $records ); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertSame( |
|
98
|
|
|
count( $acquiredRecordsWithIds ), |
|
99
|
|
|
count( $records ) |
|
100
|
|
|
); |
|
101
|
|
|
$this->assertSameRecordsInDb( [ $records[3] ], $this->dbMaster ); |
|
102
|
|
|
$this->assertNoRecordsInDb( $recordsInReplica, $this->dbMaster ); |
|
103
|
|
|
$this->assertNoRecordsInDb( $recordsInMaster, $this->dbReplica ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function assertNoRecordsInDb( array $records, IDatabase $db ) { |
|
107
|
|
|
$recordsInDbCount = $db->selectRowCount( |
|
108
|
|
|
self::TABLE_NAME, |
|
109
|
|
|
'*', |
|
110
|
|
|
$this->recordsToSelectConditions( $records, $db ) |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame( 0, $recordsInDbCount ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function assertSameRecordsInDb( array $records, IDatabase $db ) { |
|
117
|
|
|
$recordsInDbCount = $db->selectRowCount( |
|
118
|
|
|
self::TABLE_NAME, |
|
119
|
|
|
'*', |
|
120
|
|
|
$this->recordsToSelectConditions( $records, $db ) |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertCount( $recordsInDbCount, $records ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function recordsToSelectConditions( array $records, IDatabase $db ) { |
|
127
|
|
|
$conditionsPairs = []; |
|
|
|
|
|
|
128
|
|
|
foreach ( $records as $record ) { |
|
129
|
|
|
$conditionPairs[] = $db->makeList( $record, IDatabase::LIST_AND ); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $db->makeList( $conditionPairs, IDatabase::LIST_OR ); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function getTestSubjectInstance() { |
|
136
|
|
|
return new ReplicaMasterAwareRecordIdsAcquirer( |
|
137
|
|
|
$this->dbMaster, |
|
138
|
|
|
$this->dbReplica, |
|
139
|
|
|
self::TABLE_NAME, |
|
140
|
|
|
self::ID_COLUMN |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function getTestRecords() { |
|
145
|
|
|
return [ |
|
146
|
|
|
[ 'column_value' => 'valueA1', 'column_id' => '1' ], |
|
147
|
|
|
[ 'column_value' => 'valueA2', 'column_id' => '2' ], |
|
148
|
|
|
[ 'column_value' => 'valueA3', 'column_id' => '3' ], |
|
149
|
|
|
[ 'column_value' => 'valueA4', 'column_id' => '4' ] |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.