1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Statement; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
7
|
|
|
use Wikibase\DataModel\Statement\Statement; |
8
|
|
|
use Wikibase\DataModel\Statement\StatementByGuidMap; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers Wikibase\DataModel\Statement\StatementByGuidMap |
12
|
|
|
* |
13
|
|
|
* @license GPL-2.0+ |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
* @author Kai Nissen < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class StatementByGuidMapTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testGivenNotPresentGuid_hasStatementWithGuidReturnsFalse() { |
20
|
|
|
$statements = new StatementByGuidMap(); |
21
|
|
|
|
22
|
|
|
$this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGivenPresentGuid_hasStatementWithGuidReturnsTrue() { |
26
|
|
|
|
27
|
|
|
$statements = new StatementByGuidMap( [ |
28
|
|
|
$this->newStatement( 1, 'some guid' ) |
29
|
|
|
] ); |
30
|
|
|
|
31
|
|
|
$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function newStatement( $propertyId, $guid ) { |
35
|
|
|
$statement = new Statement( new PropertyNoValueSnak( $propertyId ) ); |
36
|
|
|
$statement->setGuid( $guid ); |
37
|
|
|
return $statement; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider nonStringProvider |
42
|
|
|
*/ |
43
|
|
|
public function testGivenNonStringGuid_hasStatementWithGuidThrowsException( $nonString ) { |
44
|
|
|
$statements = new StatementByGuidMap(); |
45
|
|
|
|
46
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
47
|
|
|
$statements->hasStatementWithGuid( $nonString ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function nonStringProvider() { |
51
|
|
|
return [ |
52
|
|
|
[ null ], |
53
|
|
|
[ 42 ], |
54
|
|
|
[ 4.2 ], |
55
|
|
|
[ [] ], |
56
|
|
|
[ (object)[] ], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGivenGuidOfPresentStatement_getStatementByGuidReturnsStatement() { |
61
|
|
|
$statement = $this->newStatement( 1, 'some guid' ); |
62
|
|
|
|
63
|
|
|
$statements = new StatementByGuidMap( [ $statement ] ); |
64
|
|
|
|
65
|
|
|
$this->assertEquals( $statement, $statements->getStatementByGuid( 'some guid' ) ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGivenGuidOfNotPresentStatement_getStatementByGuidReturnsNull() { |
69
|
|
|
$statements = new StatementByGuidMap(); |
70
|
|
|
|
71
|
|
|
$this->assertNull( $statements->getStatementByGuid( 'some guid' ) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @dataProvider nonStringProvider |
76
|
|
|
*/ |
77
|
|
|
public function testGivenNonStringGuid_getStatementByGuidThrowsException( $nonString ) { |
78
|
|
|
$statements = new StatementByGuidMap(); |
79
|
|
|
|
80
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
81
|
|
|
$statements->getStatementByGuid( $nonString ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGivenGuidOfPresentStatement_removeStatementWithGuidRemovesTheStatement() { |
85
|
|
|
$statement = $this->newStatement( 1, 'some guid' ); |
86
|
|
|
$statements = new StatementByGuidMap( [ $statement ] ); |
87
|
|
|
|
88
|
|
|
$statements->removeStatementWithGuid( 'some guid' ); |
89
|
|
|
|
90
|
|
|
$this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGivenGuidOfNonPresentStatement_removeStatementWithGuidDoesNoOp() { |
94
|
|
|
$statement = $this->newStatement( 1, 'some guid' ); |
95
|
|
|
$statements = new StatementByGuidMap( [ $statement ] ); |
96
|
|
|
|
97
|
|
|
$statements->removeStatementWithGuid( '-- different guid --' ); |
98
|
|
|
|
99
|
|
|
$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @dataProvider nonStringProvider |
104
|
|
|
*/ |
105
|
|
|
public function testGivenNonStringGuid_removeStatementWithGuidThrowsException( $nonString ) { |
106
|
|
|
$statements = new StatementByGuidMap(); |
107
|
|
|
|
108
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
109
|
|
|
$statements->removeStatementWithGuid( $nonString ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGivenStatementWithNoGuid_constructorThrowsException() { |
113
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
114
|
|
|
|
115
|
|
|
new StatementByGuidMap( [ |
116
|
|
|
$this->newStatement( 1, null ) |
117
|
|
|
] ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testCanConstructWithStatementTraversable() { |
121
|
|
|
$traversable = new ArrayObject( [ |
122
|
|
|
$this->newStatement( 1, 'some guid' ) |
123
|
|
|
] ); |
124
|
|
|
|
125
|
|
|
$statementMap = new StatementByGuidMap( $traversable ); |
126
|
|
|
|
127
|
|
|
$this->assertTrue( $statementMap->hasStatementWithGuid( 'some guid' ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testWhenMapIsEmpty_countReturnsZero() { |
131
|
|
|
$statements = new StatementByGuidMap(); |
132
|
|
|
|
133
|
|
|
$this->assertSame( 0, $statements->count() ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testMapCanBePassedToCount() { |
137
|
|
|
$statements = new StatementByGuidMap( [ |
138
|
|
|
$this->newStatement( 1, 'some guid' ), |
139
|
|
|
$this->newStatement( 2, 'other guid' ) |
140
|
|
|
] ); |
141
|
|
|
|
142
|
|
|
$this->assertSame( 2, count( $statements ) ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testMapCanBeIteratedOver() { |
146
|
|
|
$statement1 = $this->newStatement( 1, 'some guid' ); |
147
|
|
|
$statement2 = $this->newStatement( 2, 'other guid' ); |
148
|
|
|
|
149
|
|
|
$statementMap = new StatementByGuidMap( [ $statement1, $statement2 ] ); |
150
|
|
|
|
151
|
|
|
$iteratedStatements = []; |
152
|
|
|
|
153
|
|
|
foreach ( $statementMap as $guid => $statement ) { |
154
|
|
|
$iteratedStatements[$guid] = $statement; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$expectedStatements = [ |
158
|
|
|
'some guid' => $statement1, |
159
|
|
|
'other guid' => $statement2 |
160
|
|
|
]; |
161
|
|
|
|
162
|
|
|
$this->assertEquals( $expectedStatements, $iteratedStatements ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testGivenNotPresentStatement_addStatementAddsIt() { |
166
|
|
|
$statements = new StatementByGuidMap(); |
167
|
|
|
|
168
|
|
|
$statements->addStatement( $this->newStatement( 1, 'some guid' ) ); |
169
|
|
|
|
170
|
|
|
$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testGivenStatementWithPresentGuid_addStatementReplacesThePresentStatement() { |
174
|
|
|
$statement1 = $this->newStatement( 1, 'some guid' ); |
175
|
|
|
$statement2 = $this->newStatement( 2, 'some guid' ); |
176
|
|
|
|
177
|
|
|
$statements = new StatementByGuidMap( [ $statement1 ] ); |
178
|
|
|
|
179
|
|
|
$statements->addStatement( $statement2 ); |
180
|
|
|
|
181
|
|
|
$this->assertEquals( $statement2, $statements->getStatementByGuid( 'some guid' ) ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testToArray() { |
185
|
|
|
$statement1 = $this->newStatement( 1, 'some guid' ); |
186
|
|
|
$statement2 = $this->newStatement( 2, 'other guid' ); |
187
|
|
|
|
188
|
|
|
$statementMap = new StatementByGuidMap( [ $statement1, $statement2 ] ); |
189
|
|
|
|
190
|
|
|
$expectedStatements = [ |
191
|
|
|
'some guid' => $statement1, |
192
|
|
|
'other guid' => $statement2 |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$this->assertEquals( $expectedStatements, $statementMap->toArray() ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|