ItemPatcherTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 16
dl 0
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenEmptyDiff_itemIsReturnedAsIs() 0 10 1
A getPatchedItem() 0 8 1
A testCanPatchEntityType() 0 7 1
A testGivenNonItem_exceptionIsThrown() 0 6 1
A testPatchesLabels() 0 23 1
A testDescriptionsArePatched() 0 21 1
A testStatementsArePatched() 0 21 1
A testSiteLinksArePatched() 0 26 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Diff;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\DiffOp\DiffOpAdd;
7
use Diff\DiffOp\DiffOpChange;
8
use Diff\DiffOp\DiffOpRemove;
9
use InvalidArgumentException;
10
use PHPUnit\Framework\TestCase;
11
use Wikibase\DataModel\Entity\Item;
12
use Wikibase\DataModel\Entity\Property;
13
use Wikibase\DataModel\Services\Diff\ItemDiff;
14
use Wikibase\DataModel\Services\Diff\ItemPatcher;
15
use Wikibase\DataModel\SiteLink;
16
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
17
use Wikibase\DataModel\Statement\Statement;
18
19
/**
20
 * @covers \Wikibase\DataModel\Services\Diff\ItemPatcher
21
 *
22
 * @license GPL-2.0-or-later
23
 * @author Jeroen De Dauw < [email protected] >
24
 */
25
class ItemPatcherTest extends TestCase {
26
27
	public function testGivenEmptyDiff_itemIsReturnedAsIs() {
28
		$item = new Item();
29
		$item->getFingerprint()->setLabel( 'en', 'foo' );
30
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'bar' );
31
32
		$patchedItem = $this->getPatchedItem( $item, new ItemDiff() );
33
34
		$this->assertInstanceOf( Item::class, $patchedItem );
35
		$this->assertTrue( $item->equals( $patchedItem ) );
36
	}
37
38
	private function getPatchedItem( Item $item, ItemDiff $patch ) {
39
		$patchedItem = $item->copy();
40
41
		$patcher = new ItemPatcher();
42
		$patcher->patchEntity( $patchedItem, $patch );
43
44
		return $patchedItem;
45
	}
46
47
	public function testCanPatchEntityType() {
48
		$patcher = new ItemPatcher();
49
		$this->assertTrue( $patcher->canPatchEntityType( 'item' ) );
50
		$this->assertFalse( $patcher->canPatchEntityType( 'property' ) );
51
		$this->assertFalse( $patcher->canPatchEntityType( '' ) );
52
		$this->assertFalse( $patcher->canPatchEntityType( null ) );
53
	}
54
55
	public function testGivenNonItem_exceptionIsThrown() {
56
		$patcher = new ItemPatcher();
57
58
		$this->expectException( InvalidArgumentException::class );
59
		$patcher->patchEntity( Property::newFromType( 'kittens' ), new ItemDiff() );
60
	}
61
62
	public function testPatchesLabels() {
63
		$item = new Item();
64
		$item->getFingerprint()->setLabel( 'en', 'foo' );
65
		$item->getFingerprint()->setLabel( 'de', 'bar' );
66
67
		$patch = new ItemDiff( [
68
			'label' => new Diff( [
69
				'en' => new DiffOpChange( 'foo', 'spam' ),
70
				'nl' => new DiffOpAdd( 'baz' ),
71
			] )
72
		] );
73
74
		$patchedItem = $this->getPatchedItem( $item, $patch );
75
76
		$this->assertSame(
77
			[
78
				'en' => 'spam',
79
				'de' => 'bar',
80
				'nl' => 'baz',
81
			],
82
			$patchedItem->getFingerprint()->getLabels()->toTextArray()
83
		);
84
	}
85
86
	public function testDescriptionsArePatched() {
87
		$property = new Item();
88
		$property->setDescription( 'en', 'foo' );
89
		$property->setDescription( 'de', 'bar' );
90
91
		$patch = new ItemDiff( [
92
			'description' => new Diff( [
93
				'en' => new DiffOpChange( 'foo', 'spam' ),
94
				'nl' => new DiffOpAdd( 'baz' ),
95
			] ),
96
		] );
97
98
		$patcher = new ItemPatcher();
99
		$patcher->patchEntity( $property, $patch );
100
101
		$this->assertSame( [
102
			'en' => 'spam',
103
			'de' => 'bar',
104
			'nl' => 'baz',
105
		], $property->getFingerprint()->getDescriptions()->toTextArray() );
106
	}
107
108
	public function testStatementsArePatched() {
109
		$removedStatement = new Statement( new PropertyNoValueSnak( 1 ), null, null, 's1' );
110
		$addedStatement = new Statement( new PropertyNoValueSnak( 2 ), null, null, 's2' );
111
112
		$item = new Item();
113
		$item->getStatements()->addStatement( $removedStatement );
114
115
		$patch = new ItemDiff( [
116
			'claim' => new Diff( [
117
				's1' => new DiffOpRemove( $removedStatement ),
118
				's2' => new DiffOpAdd( $addedStatement ),
119
			] ),
120
		] );
121
122
		$expected = new Item();
123
		$expected->getStatements()->addStatement( $addedStatement );
124
125
		$patcher = new ItemPatcher();
126
		$patcher->patchEntity( $item, $patch );
127
		$this->assertTrue( $expected->equals( $item ) );
128
	}
129
130
	public function testSiteLinksArePatched() {
131
		$removedSiteLink = new SiteLink( 'rewiki', 'Removed' );
132
		$addedSiteLink = new SiteLink( 'adwiki', 'Added' );
133
134
		$item = new Item();
135
		$item->getSiteLinkList()->addSiteLink( $removedSiteLink );
136
137
		$patch = new ItemDiff( [
138
			'links' => new Diff( [
139
				'rewiki' => new Diff( [
140
					'name' => new DiffOpRemove( 'Removed' ),
141
				] ),
142
				'adwiki' => new Diff( [
143
					'name' => new DiffOpAdd( 'Added' ),
144
					'badges' => new Diff(),
145
				] ),
146
			] ),
147
		] );
148
149
		$expected = new Item();
150
		$expected->getSiteLinkList()->addSiteLink( $addedSiteLink );
151
152
		$patcher = new ItemPatcher();
153
		$patcher->patchEntity( $item, $patch );
154
		$this->assertTrue( $expected->equals( $item ) );
155
	}
156
157
}
158