Completed
Push — master ( 23c630...ec7273 )
by
unknown
02:52
created

EntityDiffTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
c 1
b 0
f 1
lcom 0
cbo 9
dl 0
loc 154
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewForType() 0 4 1
A newForTypeProvider() 0 6 1
B isEmptyProvider() 0 25 4
A testIsEmpty() 0 4 1
A diffProvider() 0 47 2
A testGetClaimsDiff() 0 13 4
A testGetDescriptionsDiff() 0 6 1
A testGetLabelsDiff() 0 6 1
A testGetAliasesDiff() 0 6 1
A testGetType() 0 4 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 PHPUnit_Framework_TestCase;
10
use Wikibase\DataModel\Services\Diff\EntityDiff;
11
use Wikibase\DataModel\Services\Diff\Internal\StatementListDiffer;
12
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
13
use Wikibase\DataModel\Statement\Statement;
14
use Wikibase\DataModel\Statement\StatementList;
15
16
/**
17
 * @covers Wikibase\DataModel\Services\Diff\EntityDiff
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class EntityDiffTest extends PHPUnit_Framework_TestCase {
23
24
	/**
25
	 * @dataProvider newForTypeProvider
26
	 */
27
	public function testNewForType( $entityType, $expected ) {
28
		$diff = EntityDiff::newForType( $entityType );
29
		$this->assertInstanceOf( $expected, $diff );
30
	}
31
32
	public function newForTypeProvider() {
33
		return array(
34
			array( 'item', 'Wikibase\DataModel\Services\Diff\ItemDiff' ),
35
			array( 'anything', 'Wikibase\DataModel\Services\Diff\EntityDiff' ),
36
		);
37
	}
38
39
	public function isEmptyProvider() {
40
		$argLists = array();
41
42
		$argLists[] = array( array(), true );
43
44
		$fields = array( 'aliases', 'label', 'description', 'claim' );
45
46
		foreach ( $fields as $field ) {
47
			$argLists[] = array( array( $field => new Diff( array() ) ), true );
48
		}
49
50
		$diffOps = array();
51
52
		foreach ( $fields as $field ) {
53
			$diffOps[$field] = new Diff( array() );
54
		}
55
56
		$argLists[] = array( $diffOps, true );
57
58
		foreach ( $fields as $field ) {
59
			$argLists[] = array( array( $field => new Diff( array( new DiffOpAdd( 42 ) ) ) ), false );
60
		}
61
62
		return $argLists;
63
	}
64
65
	/**
66
	 * @dataProvider isEmptyProvider
67
	 * @param Diff[] $diffOps
68
	 * @param bool $isEmpty
69
	 */
70
	public function testIsEmpty( array $diffOps, $isEmpty ) {
71
		$diff = new EntityDiff( $diffOps );
72
		$this->assertEquals( $isEmpty, $diff->isEmpty() );
73
	}
74
75
	public function diffProvider() {
76
		$diffs = array();
77
78
		$diffOps = array(
79
			'label' => new Diff( array(
80
				'en' => new DiffOpAdd( 'foobar' ),
81
				'de' => new DiffOpRemove( 'onoez' ),
82
				'nl' => new DiffOpChange( 'foo', 'bar' ),
83
			), true )
84
		);
85
86
		$diffs[] = new EntityDiff( $diffOps );
87
88
		$diffOps['description'] = new Diff( array(
89
			'en' => new DiffOpAdd( 'foobar' ),
90
			'de' => new DiffOpRemove( 'onoez' ),
91
			'nl' => new DiffOpChange( 'foo', 'bar' ),
92
		), true );
93
94
		$diffs[] = new EntityDiff( $diffOps );
95
96
		$diffOps['aliases'] = new Diff( array(
97
			'en' => new Diff( array( new DiffOpAdd( 'foobar' ), new DiffOpRemove( 'onoez' ) ), false ),
98
			'de' => new Diff( array( new DiffOpRemove( 'foo' ) ), false ),
99
		), true );
100
101
		$diffs[] = new EntityDiff( $diffOps );
102
103
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
104
		$statement->setGuid( 'EntityDiffTest$foo' );
105
106
		$statementListDiffer = new StatementListDiffer();
107
		$diffOps['claim'] = $statementListDiffer->getDiff(
108
			new StatementList( $statement ),
109
			new StatementList()
110
		);
111
112
		$diffs[] = new EntityDiff( $diffOps );
113
114
		$argLists = array();
115
116
		foreach ( $diffs as $diff ) {
117
			$argLists[] = array( $diff );
118
		}
119
120
		return $argLists;
121
	}
122
123
	/**
124
	 * @dataProvider diffProvider
125
	 */
126
	public function testGetClaimsDiff( EntityDiff $entityDiff ) {
127
		$diff = $entityDiff->getClaimsDiff();
128
129
		$this->assertInstanceOf( 'Diff\Diff', $diff );
130
		$this->assertTrue( $diff->isAssociative() );
131
132
		foreach ( $diff as $diffOp ) {
133
			$this->assertTrue( $diffOp instanceof DiffOpAdd || $diffOp instanceof DiffOpRemove );
134
135
			$statement = $diffOp instanceof DiffOpAdd ? $diffOp->getNewValue() : $diffOp->getOldValue();
136
			$this->assertInstanceOf( 'Wikibase\DataModel\Statement\Statement', $statement );
137
		}
138
	}
139
140
	/**
141
	 * @dataProvider diffProvider
142
	 */
143
	public function testGetDescriptionsDiff( EntityDiff $entityDiff ) {
144
		$diff = $entityDiff->getDescriptionsDiff();
145
146
		$this->assertInstanceOf( 'Diff\Diff', $diff );
147
		$this->assertTrue( $diff->isAssociative() );
148
	}
149
150
	/**
151
	 * @dataProvider diffProvider
152
	 */
153
	public function testGetLabelsDiff( EntityDiff $entityDiff ) {
154
		$diff = $entityDiff->getLabelsDiff();
155
156
		$this->assertInstanceOf( 'Diff\Diff', $diff );
157
		$this->assertTrue( $diff->isAssociative() );
158
	}
159
160
	/**
161
	 * @dataProvider diffProvider
162
	 */
163
	public function testGetAliasesDiff( EntityDiff $entityDiff ) {
164
		$diff = $entityDiff->getAliasesDiff();
165
166
		$this->assertInstanceOf( 'Diff\Diff', $diff );
167
		$this->assertTrue( $diff->isAssociative() );
168
	}
169
170
	public function testGetType() {
171
		$diff = new EntityDiff();
172
		$this->assertSame( 'diff/entity', $diff->getType() );
173
	}
174
175
}
176