providePatchAliasGroupList()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 214

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 214
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Diff\Internal;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\DiffOp\DiffOpAdd;
7
use Diff\DiffOp\DiffOpChange;
8
use Diff\DiffOp\DiffOpRemove;
9
use PHPUnit\Framework\TestCase;
10
use Wikibase\DataModel\Services\Diff\Internal\AliasGroupListPatcher;
11
use Wikibase\DataModel\Term\AliasGroup;
12
use Wikibase\DataModel\Term\AliasGroupList;
13
14
/**
15
 * @covers \Wikibase\DataModel\Services\Diff\Internal\AliasGroupListPatcher
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Bene* < [email protected] >
19
 */
20
class AliasGroupListPatcherTest extends TestCase {
21
22
	/**
23
	 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
24
	 */
25
	public function providePatchAliasGroupList() {
26
		return [
27
			'add aliases (associative)' => [
28
				new AliasGroupList(),
29
				new Diff( [
30
					'en' => new Diff( [ new DiffOpAdd( 'foo' ), new DiffOpAdd( 'bar' ) ] )
31
				], true ),
32
				new AliasGroupList( [
33
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
34
				] )
35
			],
36
			'add aliases (non-associative)' => [
37
				new AliasGroupList(),
38
				new Diff( [
39
					'en' => new Diff( [ new DiffOpAdd( 'foo' ), new DiffOpAdd( 'bar' ) ] )
40
				], false ),
41
				new AliasGroupList( [
42
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
43
				] )
44
			],
45
			'add aliases (auto-detected)' => [
46
				new AliasGroupList(),
47
				new Diff( [
48
					'en' => new Diff( [ new DiffOpAdd( 'foo' ), new DiffOpAdd( 'bar' ) ] )
49
				] ),
50
				new AliasGroupList( [
51
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
52
				] )
53
			],
54
			'add aliases (atomic)' => [
55
				new AliasGroupList(),
56
				new Diff( [
57
					'en' => new DiffOpAdd( [ 'foo', 'bar' ] )
58
				] ),
59
				new AliasGroupList( [
60
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
61
				] )
62
			],
63
			'change aliase' => [
64
				new AliasGroupList( [
65
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
66
				] ),
67
				new Diff( [
68
					'en' => new Diff( [ new DiffOpChange( 'bar', 'baz' ) ] )
69
				] ),
70
				new AliasGroupList( [
71
					new AliasGroup( 'en', [ 'foo', 'baz' ] )
72
				] )
73
			],
74
			'change aliases (atomic)' => [
75
				new AliasGroupList( [
76
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
77
				] ),
78
				new Diff( [
79
					'en' => new DiffOpChange( [ 'foo', 'bar' ], [ 'baz' ] )
80
				] ),
81
				new AliasGroupList( [
82
					new AliasGroup( 'en', [ 'baz' ] )
83
				] )
84
			],
85
			'remove all aliases' => [
86
				new AliasGroupList( [
87
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
88
				] ),
89
				new Diff( [
90
					'en' => new Diff( [ new DiffOpRemove( 'foo' ), new DiffOpRemove( 'bar' ) ] )
91
				] ),
92
				new AliasGroupList()
93
			],
94
			'remove all aliases (atomic)' => [
95
				new AliasGroupList( [
96
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
97
				] ),
98
				new Diff( [
99
					'en' => new DiffOpRemove( [ 'foo', 'bar' ] )
100
				] ),
101
				new AliasGroupList()
102
			],
103
			'remove some aliases' => [
104
				new AliasGroupList( [
105
					new AliasGroup( 'en', [ 'foo', 'bar', 'baz' ] )
106
				] ),
107
				new Diff( [
108
					'en' => new Diff( [ new DiffOpRemove( 'foo' ), new DiffOpRemove( 'bar' ) ] )
109
				] ),
110
				new AliasGroupList( [
111
					new AliasGroup( 'en', [ 'baz' ] )
112
				] )
113
			],
114
			'remove some aliases is no-op (atomic)' => [
115
				new AliasGroupList( [
116
					new AliasGroup( 'en', [ 'foo', 'bar', 'baz' ] )
117
				] ),
118
				new Diff( [
119
					'en' => new DiffOpRemove( [ 'foo', 'bar' ] )
120
				] ),
121
				new AliasGroupList( [
122
					new AliasGroup( 'en', [ 'foo', 'bar', 'baz' ] )
123
				] )
124
			],
125
			'add alias to an existing language' => [
126
				new AliasGroupList( [
127
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
128
				] ),
129
				new Diff( [
130
					'en' => new Diff( [ new DiffOpAdd( 'baz' ) ] )
131
				] ),
132
				new AliasGroupList( [
133
					new AliasGroup( 'en', [ 'foo', 'bar', 'baz' ] )
134
				] )
135
			],
136
			'add an existing language is no-op (atomic)' => [
137
				new AliasGroupList( [
138
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
139
				] ),
140
				new Diff( [
141
					'en' => new DiffOpAdd( [ 'baz' ] )
142
				] ),
143
				new AliasGroupList( [
144
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
145
				] )
146
			],
147
			'add two alias groups' => [
148
				new AliasGroupList( [
149
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
150
				] ),
151
				new Diff( [
152
					'de' => new Diff( [ new DiffOpAdd( 'foo' ), new DiffOpAdd( 'baz' ) ] ),
153
					'nl' => new Diff( [ new DiffOpAdd( 'bar' ), new DiffOpAdd( 'baz' ) ] )
154
				] ),
155
				new AliasGroupList( [
156
					new AliasGroup( 'en', [ 'foo', 'bar' ] ),
157
					new AliasGroup( 'de', [ 'foo', 'baz' ] ),
158
					new AliasGroup( 'nl', [ 'bar', 'baz' ] )
159
				] )
160
			],
161
			'add two alias groups (atomic)' => [
162
				new AliasGroupList( [
163
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
164
				] ),
165
				new Diff( [
166
					'de' => new DiffOpAdd( [ 'foo', 'baz' ] ),
167
					'nl' => new DiffOpAdd( [ 'bar', 'baz' ] )
168
				] ),
169
				new AliasGroupList( [
170
					new AliasGroup( 'en', [ 'foo', 'bar' ] ),
171
					new AliasGroup( 'de', [ 'foo', 'baz' ] ),
172
					new AliasGroup( 'nl', [ 'bar', 'baz' ] )
173
				] )
174
			],
175
			'remove a not existing language is no-op' => [
176
				new AliasGroupList( [
177
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
178
				] ),
179
				new Diff( [
180
					'de' => new Diff( [ new DiffOpRemove( 'bar' ) ] )
181
				] ),
182
				new AliasGroupList( [
183
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
184
				] )
185
			],
186
			'remove a not existing language is no-op (atomic)' => [
187
				new AliasGroupList( [
188
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
189
				] ),
190
				new Diff( [
191
					'de' => new DiffOpRemove( [ 'bar' ] )
192
				] ),
193
				new AliasGroupList( [
194
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
195
				] )
196
			],
197
			'change different aliases is no-op' => [
198
				new AliasGroupList( [
199
					new AliasGroup( 'en', [ 'foo' ] )
200
				] ),
201
				new Diff( [
202
					'en' => new Diff( [ new DiffOpChange( 'bar', 'baz' ) ] )
203
				] ),
204
				new AliasGroupList( [
205
					new AliasGroup( 'en', [ 'foo' ] )
206
				] )
207
			],
208
			'change different aliases is no-op (atomic)' => [
209
				new AliasGroupList( [
210
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
211
				] ),
212
				new Diff( [
213
					'en' => new DiffOpChange( [ 'foo', 'baz' ], [ 'baz' ] )
214
				] ),
215
				new AliasGroupList( [
216
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
217
				] )
218
			],
219
			'complex diff' => [
220
				new AliasGroupList( [
221
					new AliasGroup( 'en', [ 'foo', 'bar' ] ),
222
					new AliasGroup( 'de', [ 'foo', 'baz' ] ),
223
					new AliasGroup( 'nl', [ 'bar', 'baz' ] ),
224
				] ),
225
				new Diff( [
226
					'en' => new Diff( [ new DiffOpRemove( 'foo' ), new DiffOpAdd( 'baz' ) ] ),
227
					'de' => new Diff( [ new DiffOpRemove( 'foo' ), new DiffOpRemove( 'baz' ) ] ),
228
					'nl' => new Diff( [ new DiffOpRemove( 'foo' ), new DiffOpRemove( 'bar' ) ] ),
229
					'it' => new Diff( [ new DiffOpAdd( 'bar' ), new DiffOpAdd( 'baz' ) ] )
230
				] ),
231
				new AliasGroupList( [
232
					new AliasGroup( 'en', [ 'bar', 'baz' ] ),
233
					new AliasGroup( 'nl', [ 'baz' ] ),
234
					new AliasGroup( 'it', [ 'bar', 'baz' ] )
235
				] )
236
			],
237
		];
238
	}
239
240
	/**
241
	 * @dataProvider providePatchAliasGroupList
242
	 */
243
	public function testPatchAliasGroupList( AliasGroupList $aliasGroups, Diff $patch, AliasGroupList $expected ) {
244
		$patcher = new AliasGroupListPatcher();
245
		$patcher->patchAliasGroupList( $aliasGroups, $patch );
246
247
		$this->assertSame( $expected->toTextArray(), $aliasGroups->toTextArray() );
248
	}
249
250
}
251