Completed
Push — master ( e429fa...789203 )
by Daniel
25s
created

PropertyPatcherTest::testLabelsArePatched()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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