Test Failed
Pull Request — master (#11)
by Alaa
02:35
created

assertTermsArrayExistInDb()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\Tests\Unit\PackagePrivate;
4
5
use PHPUnit\Framework\TestCase;
6
use Wikibase\TermStore\MediaWiki\TermStoreSchemaUpdater;
7
use Wikibase\TermStore\MediaWiki\PackagePrivate\DatabaseTermIdsAcquirer;
8
use Wikibase\TermStore\MediaWiki\PackagePrivate\InMemoryTypeIdsAcquirer;
9
use Wikimedia\Rdbms\IDatabase;
10
use Wikimedia\Rdbms\DatabaseSqlite;
11
12
/**
13
 * @group TermStoreWithMediaWikiCore
14
 */
15
class DatabaseTermIdsAcquirerTest extends TestCase {
16
17
	/**
18
	 * @var IDatabase $db
19
	 */
20
	private $db;
21
22
	public function setUp() {
23
		$this->db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
24
		$this->db->sourceFile( TermStoreSchemaUpdater::getSqlFileAbsolutePath() );
25
	}
26
27
	public function testAcquireTermIdsReturnsArrayOfIdsForAllTerms() {
28
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
29
		$alreadyAcquiredTypeIds = $typeIdsAcquirer->acquireTypeIds(
0 ignored issues
show
Unused Code introduced by
$alreadyAcquiredTypeIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
			[ 'label', 'description', 'alias' ]
31
		);
32
33
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
34
			$this->db,
35
			$this->db,
36
			$typeIdsAcquirer
37
		);
38
39
		$termsArray = [
40
			'label' => [
41
				'en' => 'same',
42
				'de' => 'same',
43
			],
44
			'description' => [ 'en' => 'same' ],
45
			'alias' => [
46
				'en' => [ 'same', 'same', 'another', 'yet another' ]
47
			]
48
		];
49
50
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
51
52
		$this->assertInternalType( 'array', $acquiredTermIds );
53
		$this->assertCount( 7, $acquiredTermIds );
54
	}
55
56
	public function testAcquireTermIdsStoresTermsInDatabase() {
57
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
58
		$alreadyAcquiredTypeIds = $typeIdsAcquirer->acquireTypeIds(
59
			[ 'label', 'description', 'alias' ]
60
		);
61
62
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
63
			$this->db,
64
			$this->db,
65
			$typeIdsAcquirer
66
		);
67
68
		$termsArray = [
69
			'label' => [
70
				'en' => 'same',
71
				'de' => 'same',
72
			],
73
			'description' => [ 'en' => 'same' ],
74
			'alias' => [
75
				'en' => [ 'same', 'same', 'another', 'yet another' ]
76
			]
77
		];
78
79
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
0 ignored issues
show
Unused Code introduced by
$acquiredTermIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
81
		$this->assertTermSArrayExistInDb( $termsArray, $alreadyAcquiredTypeIds );
82
	}
83
84
	public function testAcquireTermIdsStoresOnlyUniqueTexts() {
85
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
86
87
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
88
			$this->db,
89
			$this->db,
90
			$typeIdsAcquirer
91
		);
92
93
		$termsArray = [
94
			'label' => [
95
				'en' => 'same',
96
				'de' => 'same',
97
			],
98
			'description' => [ 'en' => 'same' ],
99
			'alias' => [
100
				'en' => [ 'same', 'same', 'another', 'yet another' ]
101
			]
102
		];
103
104
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
0 ignored issues
show
Unused Code introduced by
$acquiredTermIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
106
		$this->assertEquals(
107
			3,
108
			$this->db->selectRowCount( 'wbt_text', '*' )
109
		);
110
	}
111
112
	public function testAcquireTermIdsStoresOnlyUniqueTextInLang() {
113
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
114
115
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
116
			$this->db,
117
			$this->db,
118
			$typeIdsAcquirer
119
		);
120
121
		$termsArray = [
122
			'label' => [
123
				'en' => 'same',
124
				'de' => 'same',
125
			],
126
			'description' => [ 'en' => 'same' ],
127
			'alias' => [
128
				'en' => [ 'same', 'same', 'another', 'yet another' ]
129
			]
130
		];
131
132
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
0 ignored issues
show
Unused Code introduced by
$acquiredTermIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
134
		$this->assertEquals(
135
			4,
136
			$this->db->selectRowCount( 'wbt_text_in_lang', '*' )
137
		);
138
	}
139
140
	public function testAcquireTermIdsStoresOnlyUniqueTermInLang() {
141
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
142
143
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
144
			$this->db,
145
			$this->db,
146
			$typeIdsAcquirer
147
		);
148
149
		$termsArray = [
150
			'label' => [
151
				'en' => 'same',
152
				'de' => 'same',
153
			],
154
			'description' => [ 'en' => 'same' ],
155
			'alias' => [
156
				'en' => [ 'same', 'same', 'another', 'yet another' ]
157
			]
158
		];
159
160
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
0 ignored issues
show
Unused Code introduced by
$acquiredTermIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
162
		$this->assertEquals(
163
			6,
164
			$this->db->selectRowCount( 'wbt_term_in_lang', '*' )
165
		);
166
	}
167
168
	public function testAcquireTermIdsReusesExistingTerms() {
169
		$termsArray = [
170
			'label' => [
171
				'en' => 'same',
172
				'de' => 'same',
173
			],
174
			'description' => [ 'en' => 'same' ],
175
			'alias' => [
176
				'en' => [ 'same', 'same', 'another', 'yet another' ]
177
			]
178
		];
179
180
		// We will populate DB with two terms that both have
181
		// text "same". One is of type "label" in language "en",
182
		// and the other is of type "alias" in language "en.
183
		//
184
		// TermIdsAcquirer should then reuse those terms for the given
185
		// termsArray above, meaning thoese pre-inserted terms will
186
		// appear (their ids) in the returned array from
187
		// TermIdsAcquirer::acquireTermIds( $termsArray )
188
		$typeIdsAcquirer = new InMemoryTypeIdsAcquirer();
189
		$alreadyAcquiredTypeIds = $typeIdsAcquirer->acquireTypeIds(
190
			[ 'label', 'description', 'alias' ]
191
		);
192
193
		$this->db->insert( 'wbt_text', [ 'wbx_text' => 'same' ] );
194
		$sameTextId = $this->db->insertId();
195
196
		$this->db->insert(
197
			'wbt_text_in_lang',
198
			[ 'wbxl_text_id' => $sameTextId, 'wbxl_language' => 'en' ]
199
		);
200
		$enSameTextInLangId = $this->db->insertId();
201
202
		$this->db->insert(
203
			'wbt_term_in_lang',
204
			[ 'wbtl_text_in_lang_id' => $enSameTextInLangId,
205
			  'wbtl_type_id' => $alreadyAcquiredTypeIds['label'] ]
206
		);
207
		$labelEnSameTermInLangId = (string)$this->db->insertId();
208
209
		$this->db->insert(
210
			'wbt_term_in_lang',
211
			[ 'wbtl_text_in_lang_id' => $enSameTextInLangId,
212
			  'wbtl_type_id' => $alreadyAcquiredTypeIds['alias'] ]
213
		);
214
		$aliasEnSameTermInLangId = (string)$this->db->insertId();
215
216
		$dbTermIdsAcquirer = new DatabaseTermIdsAcquirer(
217
			$this->db,
218
			$this->db,
219
			$typeIdsAcquirer
220
		);
221
222
		$acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray );
223
224
		$this->assertCount( 7, $acquiredTermIds );
225
226
		// We will assert that the returned ids of acquired terms contains
227
		// one occurence of the term id for en label "same" that already existed in db,
228
		// and two occurences of the term id for en alias "same" that already existed
229
		// in db.
230
		$this->assertCount(
231
			1,
232
			array_filter(
233
				$acquiredTermIds,
234
				function ( $id ) use ( $labelEnSameTermInLangId ) {
235
					return $id === $labelEnSameTermInLangId;
236
				}
237
			)
238
		);
239
		$this->assertCount(
240
			2,
241
			array_filter(
242
				$acquiredTermIds,
243
				function ( $id ) use ( $aliasEnSameTermInLangId ) {
244
					return $id === $aliasEnSameTermInLangId;
245
				}
246
			)
247
		);
248
	}
249
250
	private function assertTermsArrayExistInDb( $termsArray, $typeIds ) {
251
		foreach ( $termsArray as $type => $textsPerLang ) {
252
			foreach ( $textsPerLang as $lang => $texts ) {
253
				foreach ( (array)$texts as $text ) {
254
					$textId = $this->db->selectField(
255
						'wbt_text',
256
						'wbx_id',
257
						[ 'wbx_text' => $text ]
258
					);
259
260
					$this->assertNotEmpty(
261
						$textId,
262
						sprintf( 'Expected record for text "%s" was not found in wbt_text', $text )
263
					);
264
265
					$textInLangId = $this->db->selectField(
266
						'wbt_text_in_lang',
267
						'wbxl_id',
268
						[ 'wbxl_language' => $lang, 'wbxl_text_id' => $textId ]
269
					);
270
271
					$this->assertNotEmpty(
272
						$textInLangId,
273
						sprintf( 'Expected record for text "%s" in language "%s"'
274
								 . ' was not found in wbt_text_in_lang table',
275
								 $text, $lang )
276
					);
277
278
					$this->assertNotEmpty(
279
						$this->db->selectField(
280
							'wbt_term_in_lang',
281
							'wbtl_id',
282
							[ 'wbtl_type_id' => $typeIds[$type], 'wbtl_text_in_lang_id' => $textInLangId ]
283
						),
284
						sprintf( 'Expected record for text "%s" in language "%s"'
285
								 .' of type "%s" was not found in wbt_term_in_lang table',
286
								 $text, $lang, $type )
287
					);
288
				}
289
			}
290
		}
291
	}
292
}
293