testGivenEmptyDiff_itemIsReturnedAsIs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\EntityDiff;
14
use Wikibase\DataModel\Services\Diff\PropertyPatcher;
15
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
16
use Wikibase\DataModel\Statement\Statement;
17
18
/**
19
 * @covers \Wikibase\DataModel\Services\Diff\PropertyPatcher
20
 *
21
 * @license GPL-2.0-or-later
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class PropertyPatcherTest extends TestCase {
25
26
	public function testGivenEmptyDiff_itemIsReturnedAsIs() {
27
		$property = Property::newFromType( 'kittens' );
28
		$property->getFingerprint()->setLabel( 'en', 'foo' );
29
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
30
31
		$patchedProperty = $this->getPatchedProperty( $property, new EntityDiff() );
32
33
		$this->assertInstanceOf( Property::class, $patchedProperty );
34
		$this->assertTrue( $property->equals( $patchedProperty ) );
35
	}
36
37
	private function getPatchedProperty( Property $property, EntityDiff $patch ) {
38
		$patchedProperty = $property->copy();
39
40
		$patcher = new PropertyPatcher();
41
		$patcher->patchEntity( $patchedProperty, $patch );
42
43
		return $patchedProperty;
44
	}
45
46
	public function testCanPatchEntityType() {
47
		$patcher = new PropertyPatcher();
48
		$this->assertTrue( $patcher->canPatchEntityType( 'property' ) );
49
		$this->assertFalse( $patcher->canPatchEntityType( 'item' ) );
50
		$this->assertFalse( $patcher->canPatchEntityType( '' ) );
51
		$this->assertFalse( $patcher->canPatchEntityType( null ) );
52
	}
53
54
	public function testGivenNonItem_exceptionIsThrown() {
55
		$patcher = new PropertyPatcher();
56
57
		$this->expectException( InvalidArgumentException::class );
58
		$patcher->patchEntity( new Item(), new EntityDiff() );
59
	}
60
61
	public function testLabelsArePatched() {
62
		$property = Property::newFromType( 'string' );
63
		$property->setLabel( 'en', 'foo' );
64
		$property->setLabel( 'de', 'bar' );
65
66
		$patch = new EntityDiff( [
67
			'label' => new Diff( [
68
				'en' => new DiffOpChange( 'foo', 'spam' ),
69
				'nl' => new DiffOpAdd( 'baz' ),
70
			] ),
71
		] );
72
73
		$patcher = new PropertyPatcher();
74
		$patcher->patchEntity( $property, $patch );
75
76
		$this->assertSame( [
77
			'en' => 'spam',
78
			'de' => 'bar',
79
			'nl' => 'baz',
80
		], $property->getFingerprint()->getLabels()->toTextArray() );
81
	}
82
83
	public function testDescriptionsArePatched() {
84
		$property = Property::newFromType( 'string' );
85
		$property->setDescription( 'en', 'foo' );
86
		$property->setDescription( 'de', 'bar' );
87
88
		$patch = new EntityDiff( [
89
			'description' => new Diff( [
90
				'en' => new DiffOpChange( 'foo', 'spam' ),
91
				'nl' => new DiffOpAdd( 'baz' ),
92
			] ),
93
		] );
94
95
		$patcher = new PropertyPatcher();
96
		$patcher->patchEntity( $property, $patch );
97
98
		$this->assertSame( [
99
			'en' => 'spam',
100
			'de' => 'bar',
101
			'nl' => 'baz',
102
		], $property->getFingerprint()->getDescriptions()->toTextArray() );
103
	}
104
105
	public function testStatementsArePatched() {
106
		$s1337 = new Statement( new PropertyNoValueSnak( 1337 ) );
107
		$s1337->setGuid( 's1337' );
108
109
		$s23 = new Statement( new PropertyNoValueSnak( 23 ) );
110
		$s23->setGuid( 's23' );
111
112
		$s42 = new Statement( new PropertyNoValueSnak( 42 ) );
113
		$s42->setGuid( 's42' );
114
115
		$patch = new EntityDiff( [
116
				'claim' => new Diff( [
117
					's42' => new DiffOpRemove( $s42 ),
118
					's23' => new DiffOpAdd( $s23 ),
119
				] )
120
		] );
121
122
		$property = Property::newFromType( 'kittens' );
123
		$property->getStatements()->addStatement( $s1337 );
124
		$property->getStatements()->addStatement( $s42 );
125
126
		$expectedProperty = Property::newFromType( 'kittens' );
127
		$expectedProperty->getStatements()->addStatement( $s1337 );
128
		$expectedProperty->getStatements()->addStatement( $s23 );
129
130
		$this->assertEquals(
131
			$expectedProperty,
132
			$this->getPatchedProperty( $property, $patch )
133
		);
134
	}
135
136
}
137