Completed
Push — master ( dfbbd6...7f838a )
by Daniel
02:52
created

EntityDiffOldTest::testApply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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