Passed
Push — master ( 44c26f...43a260 )
by Alaa
01:53 queued 11s
created

testCleanTerms_doesNotReuseIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\Tests\Unit\PackagePrivate;
4
5
use PHPUnit\Framework\TestCase;
6
use Wikibase\TermStore\MediaWiki\PackagePrivate\InMemoryTermIdsAcquirer;
7
use Wikimedia\TestingAccessWrapper;
8
9
/**
10
 * @covers \Wikibase\TermStore\MediaWiki\PackagePrivate\InMemoryTermIdsAcquirer
11
 */
12
class InMemoryTermIdsAcquirerTest extends TestCase {
13
14
	public function testAcquiresUniqueIdsForNonExistingTerms() {
15
		$termsIdsAcquirer = new InMemoryTermIdsAcquirer();
16
17
		$ids = $termsIdsAcquirer->acquireTermIds( [
18
			'label' => [
19
				'en' => 'some label',
20
				'de' => 'another label',
21
			],
22
			'alias' => [
23
				'en' => [ 'alias', 'another alias' ],
24
				'de' => 'de alias'
25
			]
26
		] );
27
28
		$this->assertNoDuplicates( $ids );
29
	}
30
31
	public function testReusesIdsOfExistingTerms() {
32
		$termsIdsAcquirer = new InMemoryTermIdsAcquirer();
33
34
		$previouslyAcquiredIds = $termsIdsAcquirer->acquireTermIds( [
35
			'label' => [
36
				'en' => 'some label',
37
				'de' => 'another label',
38
			],
39
			'alias' => [
40
				'en' => [ 'alias', 'another alias' ],
41
				'de' => 'de alias'
42
			]
43
		] );
44
45
		$newlyAcquiredIds = $termsIdsAcquirer->acquireTermIds( [
46
			'label' => [
47
				'en' => 'some label',
48
				'de' => 'another label',
49
			],
50
			'alias' => [
51
				'en' => [ 'alias', 'another alias' ],
52
				'de' => 'de alias'
53
			]
54
		] );
55
56
		$this->assertEquals(
57
			$previouslyAcquiredIds,
58
			$newlyAcquiredIds
59
		);
60
	}
61
62
	public function testAcquiresAndReusesIdsForNewAndExistingTerms() {
63
		$termsIdsAcquirer = new InMemoryTermIdsAcquirer();
64
65
		$previouslyAcquiredIds = $termsIdsAcquirer->acquireTermIds( [
66
			'label' => [
67
				'en' => 'some label',
68
				'de' => 'another label',
69
			]
70
		] );
71
72
		$newlyAcquiredIds = $termsIdsAcquirer->acquireTermIds( [
73
			'label' => [
74
				'en' => 'some label',
75
				'de' => 'another label',
76
			],
77
			'alias' => [
78
				'en' => [ 'alias', 'another alias' ],
79
				'de' => 'de alias'
80
			]
81
		] );
82
83
		$this->assertNoDuplicates( $newlyAcquiredIds );
84
85
		$this->assertEquals(
86
			$previouslyAcquiredIds,
87
			array_intersect( $previouslyAcquiredIds, $newlyAcquiredIds )
88
		);
89
	}
90
91
	private function assertNoDuplicates( $ids ) {
92
		$this->assertEquals(
93
			count( $ids ),
94
			count( array_unique( $ids ) )
95
		);
96
	}
97
98
	public function testCleanTerms_doesNotReuseIds() {
99
		$termIdsAcquirer = new InMemoryTermIdsAcquirer();
100
101
		$ids1 = $termIdsAcquirer->acquireTermIds( [
102
			'label' => [
103
				'en' => 'the label',
104
				'de' => 'die Bezeichnung',
105
			],
106
			'alias' => [
107
				'en' => [ 'alias', 'another' ],
108
			],
109
		] );
110
		$ids2 = $termIdsAcquirer->acquireTermIds( [
111
			'label' => [ 'en' => 'the label' ],
112
			'description' => [ 'en' => 'the description' ],
113
		] );
114
115
		$termIdsAcquirer->cleanTerms( array_merge( $ids1, $ids2 ) );
116
117
		$ids3 = $termIdsAcquirer->acquireTermIds( [
118
			'label' => [ 'en' => 'the label' ],
119
			'description' => [ 'en' => 'the description' ],
120
		] );
121
		$this->assertGreaterThan( max( max( $ids1 ), max( $ids2 ) ), min( $ids3 ) );
122
	}
123
124
	public function testCleanTerms_completelyCleansArray() {
125
		$termIdsAcquirer = new InMemoryTermIdsAcquirer();
126
127
		$ids1 = $termIdsAcquirer->acquireTermIds( [
128
			'label' => [
129
				'en' => 'the label',
130
				'de' => 'die Bezeichnung',
131
			],
132
			'alias' => [
133
				'en' => [ 'alias', 'another' ],
134
			],
135
		] );
136
		$ids2 = $termIdsAcquirer->acquireTermIds( [
137
			'label' => [ 'en' => 'the label' ],
138
			'description' => [ 'en' => 'the description' ],
139
		] );
140
141
		$termIdsAcquirer->cleanTerms( array_merge( $ids1, $ids2 ) );
142
143
		$this->assertEmpty( TestingAccessWrapper::newFromObject( $termIdsAcquirer )->terms );
144
	}
145
146
	public function testCleanTerms_keepsOtherIds() {
147
		$termIdsAcquirer = new InMemoryTermIdsAcquirer();
148
149
		$ids1 = $termIdsAcquirer->acquireTermIds( [
150
			'label' => [
151
				'en' => 'the label',
152
				'de' => 'die Bezeichnung',
153
			],
154
			'alias' => [
155
				'en' => [ 'alias', 'another' ],
156
			],
157
		] );
158
		$ids2 = $termIdsAcquirer->acquireTermIds( [
159
			'label' => [ 'en' => 'the label' ],
160
			'description' => [ 'en' => 'the description' ],
161
		] );
162
163
		// Clean up terms related to the first set of IDs,
164
		// while keeping the second set of IDs intact.
165
		// (Knowing that the intersection of $ids1 and $ids2 should not be cleaned
166
		// is the caller’s responsibility, not the cleaner’s; we do it via array_diff.)
167
		$idsToClean = array_diff( $ids1, $ids2 );
168
		$termIdsAcquirer->cleanTerms( $idsToClean );
169
170
		$ids3 = $termIdsAcquirer->acquireTermIds( [
171
			'label' => [ 'en' => 'the label' ],
172
			'description' => [ 'en' => 'the description' ],
173
		] );
174
		$this->assertSame( $ids2, $ids3 );
175
	}
176
177
}
178