EntityDiffOldTest::provideConflictDetection()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 8.8072
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;
4
5
use PHPUnit\Framework\TestCase;
6
use RuntimeException;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\Property;
10
use Wikibase\DataModel\Services\Diff\EntityDiffer;
11
use Wikibase\DataModel\Services\Diff\EntityPatcher;
12
13
/**
14
 * @covers \Wikibase\DataModel\Services\Diff\EntityDiff
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Daniel Kinzler
18
 * @author Jens Ohlig <[email protected]>
19
 */
20
abstract class EntityDiffOldTest extends TestCase {
21
22
	private static function newEntity( $entityType ) {
23
		switch ( $entityType ) {
24
			case Item::ENTITY_TYPE:
25
				return new Item();
26
			case Property::ENTITY_TYPE:
27
				return Property::newFromType( 'string' );
28
			default:
29
				throw new RuntimeException( "unknown entity type: $entityType" );
30
		}
31
	}
32
33
	protected function generateApplyData( $entityType ) {
34
		$tests = [];
35
36
		// #0: add label
37
		$a = self::newEntity( $entityType );
38
		$a->setLabel( 'en', 'Test' );
39
40
		$b = self::newEntity( $entityType );
41
		$b->setLabel( 'en', 'Test' );
42
		$b->setLabel( 'de', 'Test' );
43
44
		$tests[] = [ $a, $b ];
45
46
		// #1: remove label
47
		$a = self::newEntity( $entityType );
48
		$a->setLabel( 'en', 'Test' );
49
		$a->setLabel( 'de', 'Test' );
50
51
		$b = self::newEntity( $entityType );
52
		$b->setLabel( 'de', 'Test' );
53
54
		$tests[] = [ $a, $b ];
55
56
		// #2: change label
57
		$a = self::newEntity( $entityType );
58
		$a->setLabel( 'en', 'Test' );
59
60
		$b = self::newEntity( $entityType );
61
		$b->setLabel( 'en', 'Test!!!' );
62
63
		// #3: add description ------------------------------
64
		$a = self::newEntity( $entityType );
65
		$a->setDescription( 'en', 'Test' );
66
67
		$b = self::newEntity( $entityType );
68
		$b->setDescription( 'en', 'Test' );
69
		$b->setDescription( 'de', 'Test' );
70
71
		$tests[] = [ $a, $b ];
72
73
		// #4: remove description
74
		$a = self::newEntity( $entityType );
75
		$a->setDescription( 'en', 'Test' );
76
		$a->setDescription( 'de', 'Test' );
77
78
		$b = self::newEntity( $entityType );
79
		$b->setDescription( 'de', 'Test' );
80
81
		$tests[] = [ $a, $b ];
82
83
		// #5: change description
84
		$a = self::newEntity( $entityType );
85
		$a->setDescription( 'en', 'Test' );
86
87
		$b = self::newEntity( $entityType );
88
		$b->setDescription( 'en', 'Test!!!' );
89
90
		$tests[] = [ $a, $b ];
91
92
		// #6: add alias ------------------------------
93
		$a = self::newEntity( $entityType );
94
		$a->setAliases( 'en', [ 'Foo', 'Bar' ] );
95
96
		$b = self::newEntity( $entityType );
97
		$b->setAliases( 'en', [ 'Foo', 'Bar', 'Quux' ] );
98
99
		$tests[] = [ $a, $b ];
100
101
		// #7: add alias language
102
		$a = self::newEntity( $entityType );
103
		$a->setAliases( 'en', [ 'Foo', 'Bar' ] );
104
105
		$b = self::newEntity( $entityType );
106
		$b->setAliases( 'en', [ 'Foo', 'Bar' ] );
107
		$b->setAliases( 'de', [ 'Quux' ] );
108
109
		$tests[] = [ $a, $b ];
110
111
		// #8: remove alias
112
		$a = self::newEntity( $entityType );
113
		$a->setAliases( 'en', [ 'Foo', 'Bar' ] );
114
115
		$b = self::newEntity( $entityType );
116
		$b->setAliases( 'en', [ 'Bar' ] );
117
118
		$tests[] = [ $a, $b ];
119
120
		// #9: remove alias language
121
		$a = self::newEntity( $entityType );
122
		$a->setAliases( 'en', [ 'Foo', 'Bar' ] );
123
124
		$b = self::newEntity( $entityType );
125
126
		$tests[] = [ $a, $b ];
127
		return $tests;
128
	}
129
130
	public function provideConflictDetection() {
131
		$cases = [];
132
133
		// #0: adding a label where there was none before
134
		$base = self::newEntity( Item::ENTITY_TYPE );
135
		$current = unserialize( serialize( $base ) );
136
137
		$new = unserialize( serialize( $base ) );
138
		$new->setLabel( 'en', 'TEST' );
139
140
		$cases[] = [
141
			$base,
142
			$current,
143
			$new,
144
			0 // there should eb no conflicts.
145
		];
146
147
		// #1: adding an alias where there was none before
148
		$base = self::newEntity( Item::ENTITY_TYPE );
149
		$current = $base;
150
151
		$new = unserialize( serialize( $base ) );
152
		$new->setAliases( 'en', [ 'TEST' ] );
153
154
		$cases[] = [
155
			$base,
156
			$current,
157
			$new,
158
			0 // there should eb no conflicts.
159
		];
160
161
		// #2: adding an alias where there already was one before
162
		$base = self::newEntity( Item::ENTITY_TYPE );
163
		$base->setAliases( 'en', [ 'Foo' ] );
164
		$current = $base;
165
166
		$new = unserialize( serialize( $base ) );
167
		$new->setAliases( 'en', [ 'Bar' ] );
168
169
		$cases[] = [
170
			$base,
171
			$current,
172
			$new,
173
			0 // there should be no conflicts.
174
		];
175
176
		// #3: adding an alias where there already was one in another language
177
		$base = self::newEntity( Item::ENTITY_TYPE );
178
		$base->setAliases( 'en', [ 'Foo' ] );
179
		$current = $base;
180
181
		$new = unserialize( serialize( $base ) );
182
		$new->setAliases( 'de', [ 'Bar' ] );
183
184
		$cases[] = [
185
			$base,
186
			$current,
187
			$new,
188
			0 // there should be no conflicts.
189
		];
190
191
		return $cases;
192
	}
193
194
	/**
195
	 * @dataProvider provideConflictDetection
196
	 */
197
	public function testConflictDetection(
198
		EntityDocument $base,
199
		EntityDocument $current,
200
		EntityDocument $new,
201
		$expectedConflicts
202
	) {
203
		$differ = new EntityDiffer();
204
		$patcher = new EntityPatcher();
205
206
		$patch = $differ->diffEntities( $base, $new );
207
208
		$patchedCurrent = unserialize( serialize( $current ) );
209
		$patcher->patchEntity( $patchedCurrent, $patch );
210
211
		$cleanPatch = $differ->diffEntities( $base, $patchedCurrent );
212
213
		$conflicts = $patch->count() - $cleanPatch->count();
214
215
		$this->assertEquals( $expectedConflicts, $conflicts, 'check number of conflicts detected' );
216
	}
217
218
}
219