Passed
Push — rankClass ( 1b9cf4...7f76c7 )
by no
04:15 queued 16s
created

FingerprintTest::testSetLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Term;
4
5
use OutOfBoundsException;
6
use Wikibase\DataModel\Term\AliasGroup;
7
use Wikibase\DataModel\Term\AliasGroupList;
8
use Wikibase\DataModel\Term\Fingerprint;
9
use Wikibase\DataModel\Term\Term;
10
use Wikibase\DataModel\Term\TermList;
11
12
/**
13
 * @covers \Wikibase\DataModel\Term\Fingerprint
14
 * @uses \Wikibase\DataModel\Term\AliasGroup
15
 * @uses \Wikibase\DataModel\Term\AliasGroupList
16
 * @uses \Wikibase\DataModel\Term\Fingerprint
17
 * @uses \Wikibase\DataModel\Term\Term
18
 * @uses \Wikibase\DataModel\Term\TermList
19
 *
20
 * @license GPL-2.0+
21
 * @author Jeroen De Dauw < [email protected] >
22
 * @author Thiemo Kreuz
23
 */
24
class FingerprintTest extends \PHPUnit_Framework_TestCase {
25
26
	/**
27
	 * @var TermList
28
	 */
29
	private $labels;
30
31
	/**
32
	 * @var TermList
33
	 */
34
	private $descriptions;
35
36
	/**
37
	 * @var AliasGroupList
38
	 */
39
	private $aliasGroups;
40
41
	/**
42
	 * @var Fingerprint
43
	 */
44
	private $fingerprint;
45
46
	protected function setUp() {
47
		$this->labels = $this->createMock( TermList::class );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Wikib...l\Term\TermList::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Wikibase\DataModel\Term\TermList> of property $labels.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
		$this->descriptions = $this->createMock( TermList::class );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Wikib...l\Term\TermList::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Wikibase\DataModel\Term\TermList> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
		$this->aliasGroups = $this->createMock( AliasGroupList::class );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Wikib...\AliasGroupList::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Wikibase\DataModel\Term\AliasGroupList> of property $aliasGroups.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
		$this->fingerprint = new Fingerprint(
52
			new TermList( [
53
				new Term( 'en', 'enlabel' ),
54
				new Term( 'de', 'delabel' ),
55
			] ),
56
			new TermList( [
57
				new Term( 'en', 'endescription' ),
58
				new Term( 'de', 'dedescription' ),
59
			] ),
60
			new AliasGroupList( [
61
				new AliasGroup( 'en', [ 'enalias' ] ),
62
				new AliasGroup( 'de', [ 'dealias' ] ),
63
			] )
64
		);
65
	}
66
67
	public function testEmptyConstructor() {
68
		$fingerprint = new Fingerprint();
69
70
		$this->assertTrue( $fingerprint->getLabels()->isEmpty() );
71
		$this->assertTrue( $fingerprint->getDescriptions()->isEmpty() );
72
		$this->assertTrue( $fingerprint->getAliasGroups()->isEmpty() );
73
	}
74
75
	public function testConstructorSetsValues() {
76
		$fingerprint = new Fingerprint( $this->labels, $this->descriptions, $this->aliasGroups );
77
78
		$this->assertEquals( $this->labels, $fingerprint->getLabels() );
79
		$this->assertEquals( $this->descriptions, $fingerprint->getDescriptions() );
80
		$this->assertEquals( $this->aliasGroups, $fingerprint->getAliasGroups() );
81
	}
82
83
	public function testGetLabel() {
84
		$term = new Term( 'en', 'enlabel' );
85
		$this->assertEquals( $term, $this->fingerprint->getLabel( 'en' ) );
86
	}
87
88
	public function testSetLabel() {
89
		$term = new Term( 'en', 'changed' );
90
		$this->fingerprint->setLabel( 'en', 'changed' );
91
		$this->assertEquals( $term, $this->fingerprint->getLabel( 'en' ) );
92
	}
93
94
	public function testRemoveLabel() {
95
		$labels = new TermList( [
96
			new Term( 'de', 'delabel' ),
97
		] );
98
		$this->fingerprint->removeLabel( 'en' );
99
		$this->assertEquals( $labels, $this->fingerprint->getLabels() );
100
	}
101
102
	/**
103
	 * @expectedException OutOfBoundsException
104
	 */
105
	public function testRemoveLabelMakesGetterThrowException() {
106
		$this->fingerprint->removeLabel( 'en' );
107
		$this->fingerprint->getLabel( 'en' );
108
	}
109
110
	public function testGetDescription() {
111
		$term = new Term( 'en', 'endescription' );
112
		$this->assertEquals( $term, $this->fingerprint->getDescription( 'en' ) );
113
	}
114
115
	public function testSetDescription() {
116
		$description = new Term( 'en', 'changed' );
117
		$this->fingerprint->setDescription( 'en', 'changed' );
118
		$this->assertEquals( $description, $this->fingerprint->getDescription( 'en' ) );
119
	}
120
121
	public function testRemoveDescription() {
122
		$descriptions = new TermList( [
123
			new Term( 'de', 'dedescription' ),
124
		] );
125
		$this->fingerprint->removeDescription( 'en' );
126
		$this->assertEquals( $descriptions, $this->fingerprint->getDescriptions() );
127
	}
128
129
	/**
130
	 * @expectedException OutOfBoundsException
131
	 */
132
	public function testRemoveDescriptionMakesGetterThrowException() {
133
		$this->fingerprint->removeDescription( 'en' );
134
		$this->fingerprint->getDescription( 'en' );
135
	}
136
137
	public function testGetAliasGroup() {
138
		$aliasGroup = new AliasGroup( 'en', [ 'enalias' ] );
139
		$this->assertEquals( $aliasGroup, $this->fingerprint->getAliasGroup( 'en' ) );
140
	}
141
142
	public function testSetAliasGroup() {
143
		$aliasGroup = new AliasGroup( 'en', [ 'changed' ] );
144
		$this->fingerprint->setAliasGroup( 'en', [ 'changed' ] );
145
		$this->assertEquals( $aliasGroup, $this->fingerprint->getAliasGroup( 'en' ) );
146
	}
147
148
	public function testRemoveAliasGroup() {
149
		$aliasGroups = new AliasGroupList( [
150
			new AliasGroup( 'de', [ 'dealias' ] ),
151
		] );
152
		$this->fingerprint->removeAliasGroup( 'en' );
153
		$this->assertEquals( $aliasGroups, $this->fingerprint->getAliasGroups() );
154
	}
155
156
	/**
157
	 * @expectedException OutOfBoundsException
158
	 */
159
	public function testRemoveAliasGroupMakesGetterThrowException() {
160
		$this->fingerprint->removeAliasGroup( 'en' );
161
		$this->fingerprint->getAliasGroup( 'en' );
162
	}
163
164
	/**
165
	 * @dataProvider fingerprintProvider
166
	 */
167
	public function testFingerprintsEqualThemselves( Fingerprint $fingerprint ) {
168
		$this->assertTrue( $fingerprint->equals( $fingerprint ) );
169
		$this->assertTrue( $fingerprint->equals( clone $fingerprint ) );
170
	}
171
172
	public function fingerprintProvider() {
173
		return [
174
			[
175
				new Fingerprint()
176
			],
177
			[
178
				new Fingerprint(
179
					new TermList( [ new Term( 'en', 'foo' ) ] )
180
				)
181
			],
182
			[
183
				new Fingerprint(
184
					new TermList(),
185
					new TermList( [ new Term( 'en', 'foo' ) ] )
186
				)
187
			],
188
			[
189
				new Fingerprint(
190
					new TermList(),
191
					new TermList(),
192
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
193
				)
194
			],
195
			[
196
				new Fingerprint(
197
					new TermList( [ new Term( 'nl', 'bar' ), new Term( 'fr', 'le' ) ] ),
198
					new TermList( [ new Term( 'de', 'baz' ) ] ),
199
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
200
				)
201
			],
202
		];
203
	}
204
205
	/**
206
	 * @dataProvider differentFingerprintsProvider
207
	 */
208
	public function testDifferentFingerprintsDoNotEqual( Fingerprint $one, Fingerprint $two ) {
209
		$this->assertFalse( $one->equals( $two ) );
210
	}
211
212
	public function differentFingerprintsProvider() {
213
		return [
214
			[
215
				new Fingerprint(),
216
				new Fingerprint(
217
					new TermList( [ new Term( 'en', 'foo' ) ] )
218
				)
219
			],
220
			[
221
				new Fingerprint(
222
					new TermList( [ new Term( 'en', 'foo' ), new Term( 'de', 'bar' ) ] )
223
				),
224
				new Fingerprint(
225
					new TermList( [ new Term( 'en', 'foo' ) ] )
226
				)
227
			],
228
			[
229
				new Fingerprint(),
230
				new Fingerprint(
231
					new TermList(),
232
					new TermList( [ new Term( 'en', 'foo' ) ] )
233
				)
234
			],
235
			[
236
				new Fingerprint(),
237
				new Fingerprint(
238
					new TermList(),
239
					new TermList(),
240
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
241
				)
242
			],
243
			[
244
				new Fingerprint(
245
					new TermList( [ new Term( 'nl', 'bar' ), new Term( 'fr', 'le' ) ] ),
246
					new TermList( [ new Term( 'de', 'HAX' ) ] ),
247
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
248
				),
249
				new Fingerprint(
250
					new TermList( [ new Term( 'nl', 'bar' ), new Term( 'fr', 'le' ) ] ),
251
					new TermList( [ new Term( 'de', 'baz' ) ] ),
252
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
253
				)
254
			],
255
		];
256
	}
257
258
	public function testEmptyFingerprintIsEmpty() {
259
		$fingerprint = new Fingerprint();
260
		$this->assertTrue( $fingerprint->isEmpty() );
261
	}
262
263
	/**
264
	 * @dataProvider nonEmptyFingerprintProvider
265
	 */
266
	public function testNonEmptyFingerprintIsNotEmpty( Fingerprint $nonEmptyFingerprint ) {
267
		$this->assertFalse( $nonEmptyFingerprint->isEmpty() );
268
	}
269
270
	public function nonEmptyFingerprintProvider() {
271
		return [
272
			[
273
				new Fingerprint(
274
					new TermList( [ new Term( 'en', 'foo' ) ] )
275
				)
276
			],
277
278
			[
279
				new Fingerprint(
280
					new TermList(),
281
					new TermList( [ new Term( 'en', 'foo' ) ] )
282
				)
283
			],
284
285
			[
286
				new Fingerprint(
287
					new TermList(),
288
					new TermList(),
289
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
290
				)
291
			],
292
293
			[
294
				new Fingerprint(
295
					new TermList( [ new Term( 'nl', 'bar' ), new Term( 'fr', 'le' ) ] ),
296
					new TermList( [ new Term( 'de', 'baz' ) ] ),
297
					new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] )
298
				)
299
			],
300
		];
301
	}
302
303
	public function testSetLabels() {
304
		$fingerprint = new Fingerprint();
305
		$fingerprint->setLabel( 'en', 'foo' );
306
307
		$labels = new TermList( [
308
			new Term( 'de', 'bar' )
309
		] );
310
311
		$fingerprint->setLabels( $labels );
312
313
		$this->assertEquals( $labels, $fingerprint->getLabels() );
314
	}
315
316
	public function testSetDescriptions() {
317
		$fingerprint = new Fingerprint();
318
		$fingerprint->setDescription( 'en', 'foo' );
319
320
		$descriptions = new TermList( [
321
			new Term( 'de', 'bar' )
322
		] );
323
324
		$fingerprint->setDescriptions( $descriptions );
325
326
		$this->assertEquals( $descriptions, $fingerprint->getDescriptions() );
327
	}
328
329
	public function testSetAliasGroups() {
330
		$fingerprint = new Fingerprint();
331
		$fingerprint->setAliasGroup( 'en', [ 'foo' ] );
332
333
		$groups = new AliasGroupList( [
334
			new AliasGroup( 'de', [ 'bar' ] )
335
		] );
336
337
		$fingerprint->setAliasGroups( $groups );
338
339
		$this->assertEquals( $groups, $fingerprint->getAliasGroups() );
340
	}
341
342
	public function testEmptyFingerprintDoesNotHaveLabel() {
343
		$fingerprint = new Fingerprint();
344
		$this->assertFalse( $fingerprint->hasLabel( 'en' ) );
345
	}
346
347
	public function testEmptyFingerprintDoesNotHaveDescription() {
348
		$fingerprint = new Fingerprint();
349
		$this->assertFalse( $fingerprint->hasDescription( 'en' ) );
350
	}
351
352
	public function testEmptyFingerprintDoesNotHaveAliasGroup() {
353
		$fingerprint = new Fingerprint();
354
		$this->assertFalse( $fingerprint->hasAliasGroup( 'en' ) );
355
	}
356
357
	public function testHasLabelReturnsTrueOnlyWhenLabelExists() {
358
		$fingerprint = new Fingerprint();
359
		$fingerprint->setLabel( 'en', 'foo' );
360
361
		$this->assertTrue( $fingerprint->hasLabel( 'en' ) );
362
		$this->assertFalse( $fingerprint->hasLabel( 'de' ) );
363
	}
364
365
	public function testHasDescriptionReturnsTrueOnlyWhenDescriptionExists() {
366
		$fingerprint = new Fingerprint();
367
		$fingerprint->setDescription( 'en', 'foo' );
368
369
		$this->assertTrue( $fingerprint->hasDescription( 'en' ) );
370
		$this->assertFalse( $fingerprint->hasDescription( 'de' ) );
371
	}
372
373
	public function testHasAliasGroupReturnsTrueOnlyWhenAliasGroupExists() {
374
		$fingerprint = new Fingerprint();
375
		$fingerprint->setAliasGroup( 'en', [ 'foo' ] );
376
377
		$this->assertTrue( $fingerprint->hasAliasGroup( 'en' ) );
378
		$this->assertFalse( $fingerprint->hasAliasGroup( 'de' ) );
379
	}
380
381
}
382