1 | <?php |
||
32 | class SnakListTest extends HashArrayTest { |
||
33 | |||
34 | /** |
||
35 | * @see HashArrayTest::getInstanceClass |
||
36 | */ |
||
37 | public function getInstanceClass() { |
||
38 | return 'Wikibase\DataModel\Snak\SnakList'; |
||
39 | } |
||
40 | |||
41 | public function elementInstancesProvider() { |
||
42 | $id42 = new PropertyId( 'P42' ); |
||
43 | |||
44 | $argLists = array(); |
||
45 | |||
46 | $argLists[] = array( array( new PropertyNoValueSnak( $id42 ) ) ); |
||
47 | $argLists[] = array( array( new PropertyNoValueSnak( new PropertyId( 'P9001' ) ) ) ); |
||
48 | $argLists[] = array( array( new PropertyValueSnak( $id42, new StringValue( 'a' ) ) ) ); |
||
49 | |||
50 | return $argLists; |
||
51 | } |
||
52 | |||
53 | public function constructorProvider() { |
||
54 | $id42 = new PropertyId( 'P42' ); |
||
55 | $id9001 = new PropertyId( 'P9001' ); |
||
56 | |||
57 | return array( |
||
58 | array(), |
||
59 | array( array() ), |
||
60 | array( array( |
||
61 | new PropertyNoValueSnak( $id42 ) |
||
62 | ) ), |
||
63 | array( array( |
||
64 | new PropertyNoValueSnak( $id42 ), |
||
65 | new PropertyNoValueSnak( $id9001 ), |
||
66 | ) ), |
||
67 | array( array( |
||
68 | new PropertyNoValueSnak( $id42 ), |
||
69 | new PropertyNoValueSnak( $id9001 ), |
||
70 | new PropertyValueSnak( $id42, new StringValue( 'a' ) ), |
||
71 | ) ), |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @dataProvider invalidConstructorArgumentsProvider |
||
77 | * @expectedException InvalidArgumentException |
||
78 | */ |
||
79 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
||
80 | new SnakList( $input ); |
||
81 | } |
||
82 | |||
83 | public function invalidConstructorArgumentsProvider() { |
||
99 | |||
100 | public function testGivenAssociativeArray_constructorPreservesArrayKeys() { |
||
104 | |||
105 | /** |
||
106 | * @dataProvider instanceProvider |
||
107 | * @param SnakList $array |
||
108 | */ |
||
109 | public function testHasSnak( SnakList $array ) { |
||
110 | /** |
||
111 | * @var Snak $hashable |
||
112 | */ |
||
113 | foreach ( iterator_to_array( $array ) as $hashable ) { |
||
114 | $this->assertTrue( $array->hasSnak( $hashable ) ); |
||
115 | $this->assertTrue( $array->hasSnakHash( $hashable->getHash() ) ); |
||
116 | $array->removeSnak( $hashable ); |
||
117 | $this->assertFalse( $array->hasSnak( $hashable ) ); |
||
118 | $this->assertFalse( $array->hasSnakHash( $hashable->getHash() ) ); |
||
119 | } |
||
120 | |||
121 | $this->assertTrue( true ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @dataProvider instanceProvider |
||
126 | * @param SnakList $array |
||
127 | */ |
||
128 | public function testRemoveSnak( SnakList $array ) { |
||
129 | $elementCount = $array->count(); |
||
130 | |||
131 | /** |
||
132 | * @var Snak $element |
||
133 | */ |
||
134 | foreach ( iterator_to_array( $array ) as $element ) { |
||
135 | $this->assertTrue( $array->hasSnak( $element ) ); |
||
136 | |||
137 | if ( $elementCount % 2 === 0 ) { |
||
138 | $array->removeSnak( $element ); |
||
139 | } |
||
140 | else { |
||
141 | $array->removeSnakHash( $element->getHash() ); |
||
142 | } |
||
143 | |||
144 | $this->assertFalse( $array->hasSnak( $element ) ); |
||
145 | $this->assertEquals( --$elementCount, $array->count() ); |
||
146 | } |
||
147 | |||
148 | $element = new PropertyNoValueSnak( new PropertyId( 'P42' ) ); |
||
149 | |||
150 | $array->removeSnak( $element ); |
||
151 | $array->removeSnakHash( $element->getHash() ); |
||
152 | |||
153 | $this->assertTrue( true ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @dataProvider instanceProvider |
||
158 | * @param SnakList $array |
||
159 | */ |
||
160 | public function testAddSnak( SnakList $array ) { |
||
161 | $elementCount = $array->count(); |
||
162 | |||
163 | $elements = $this->elementInstancesProvider(); |
||
164 | $element = array_shift( $elements ); |
||
165 | $element = $element[0][0]; |
||
166 | |||
167 | if ( !$array->hasSnak( $element ) ) { |
||
168 | ++$elementCount; |
||
169 | } |
||
170 | |||
171 | $this->assertEquals( !$array->hasSnak( $element ), $array->addSnak( $element ) ); |
||
172 | |||
173 | $this->assertEquals( $elementCount, $array->count() ); |
||
174 | |||
175 | $this->assertFalse( $array->addSnak( $element ) ); |
||
176 | |||
177 | $this->assertEquals( $elementCount, $array->count() ); |
||
178 | } |
||
179 | |||
180 | public function orderByPropertyProvider() { |
||
181 | $class = $this->getInstanceClass(); |
||
182 | |||
183 | $id1 = new PropertyId( 'P1' ); |
||
184 | $id2 = new PropertyId( 'P2' ); |
||
185 | $id3 = new PropertyId( 'P3' ); |
||
186 | |||
187 | /** |
||
188 | * List of test data containing snaks to initialize SnakList objects. The first list of |
||
189 | * snaks represents the snak list to be used as test input while the second represents the |
||
190 | * expected result. |
||
191 | * @var array |
||
192 | */ |
||
193 | $rawArguments = array( |
||
194 | 'Default order' => array( |
||
195 | array(), |
||
196 | array(), |
||
197 | ), |
||
198 | 'Unknown id in order' => array( |
||
199 | array(), |
||
200 | array(), |
||
201 | array( 'P1' ) |
||
202 | ), |
||
203 | array( |
||
204 | array( new PropertyNoValueSnak( $id1 ) ), |
||
205 | array( new PropertyNoValueSnak( $id1 ) ), |
||
206 | ), |
||
207 | array( |
||
208 | array( |
||
209 | new PropertyNoValueSnak( $id2 ), |
||
210 | new PropertyNoValueSnak( $id1 ), |
||
211 | ), |
||
212 | array( |
||
213 | new PropertyNoValueSnak( $id2 ), |
||
214 | new PropertyNoValueSnak( $id1 ), |
||
215 | ), |
||
216 | ), |
||
217 | array( |
||
218 | array( |
||
219 | new PropertyNoValueSnak( $id1 ), |
||
220 | new PropertyNoValueSnak( $id2 ), |
||
221 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
222 | ), |
||
223 | array( |
||
224 | new PropertyNoValueSnak( $id1 ), |
||
225 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
226 | new PropertyNoValueSnak( $id2 ), |
||
227 | ), |
||
228 | ), |
||
229 | 'With additional order' => array( |
||
230 | array( |
||
231 | new PropertyNoValueSnak( $id3 ), |
||
232 | new PropertyNoValueSnak( $id2 ), |
||
233 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
234 | ), |
||
235 | array( |
||
236 | new PropertyNoValueSnak( $id2 ), |
||
237 | new PropertyNoValueSnak( $id3 ), |
||
238 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
239 | ), |
||
240 | array( 'P2' ) |
||
241 | ), |
||
242 | array( |
||
243 | array( |
||
244 | new PropertyNoValueSnak( $id3 ), |
||
245 | new PropertyNoValueSnak( $id2 ), |
||
246 | new PropertyNoValueSnak( $id2 ), |
||
247 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
248 | new PropertyNoValueSnak( $id1 ), |
||
249 | ), |
||
250 | array( |
||
251 | new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
||
252 | new PropertyNoValueSnak( $id1 ), |
||
253 | new PropertyNoValueSnak( $id3 ), |
||
254 | new PropertyNoValueSnak( $id2 ), |
||
255 | new PropertyNoValueSnak( $id2 ), |
||
256 | ), |
||
257 | array( 'P1' ) |
||
258 | ), |
||
259 | ); |
||
260 | |||
261 | $arguments = array(); |
||
262 | |||
263 | foreach ( $rawArguments as $key => $rawArgument ) { |
||
264 | $arguments[$key] = array( |
||
265 | new $class( $rawArgument[0] ), |
||
266 | new $class( $rawArgument[1] ), |
||
267 | array_key_exists( 2, $rawArgument ) ? $rawArgument[2] : array() |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | return $arguments; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @dataProvider orderByPropertyProvider |
||
276 | */ |
||
277 | public function testOrderByProperty( SnakList $snakList, SnakList $expected, array $order = array() ) { |
||
294 | |||
295 | } |
||
296 |