1 | <?php |
||
24 | class ReferenceTest extends \PHPUnit_Framework_TestCase { |
||
25 | |||
26 | public function snakListProvider() { |
||
27 | $snakLists = array(); |
||
28 | |||
29 | $snakLists[] = new SnakList(); |
||
30 | |||
31 | $snakLists[] = new SnakList( |
||
32 | array( new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ) ) |
||
33 | ); |
||
34 | |||
35 | $snakLists[] = new SnakList( array( |
||
36 | new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ), |
||
37 | new PropertySomeValueSnak( new PropertyId( 'P2' ) ), |
||
38 | new PropertyNoValueSnak( new PropertyId( 'P3' ) ) |
||
39 | ) ); |
||
40 | |||
41 | $argLists = array(); |
||
42 | |||
43 | foreach ( $snakLists as $snakList ) { |
||
44 | $argLists[] = array( $snakList ); |
||
45 | } |
||
46 | |||
47 | return $argLists; |
||
48 | } |
||
49 | |||
50 | public function instanceProvider() { |
||
72 | |||
73 | /** |
||
74 | * @dataProvider snakListProvider |
||
75 | */ |
||
76 | public function testConstructor( SnakList $snaks ) { |
||
77 | $omnomnomReference = new Reference( $snaks ); |
||
78 | |||
79 | $this->assertInstanceOf( 'Wikibase\DataModel\Reference', $omnomnomReference ); |
||
80 | |||
81 | $this->assertEquals( $snaks, $omnomnomReference->getSnaks() ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @dataProvider instanceProvider |
||
86 | */ |
||
87 | public function testGetHashReturnsString( Reference $reference ) { |
||
90 | |||
91 | /** |
||
92 | * @dataProvider instanceProvider |
||
93 | */ |
||
94 | public function testGetHashIsStable( Reference $reference ) { |
||
97 | |||
98 | /** |
||
99 | * @dataProvider instanceProvider |
||
100 | */ |
||
101 | public function testGetHashIsTheSameForInstanceWithSameValue( Reference $reference ) { |
||
105 | |||
106 | /** |
||
107 | * @dataProvider instanceProvider |
||
108 | */ |
||
109 | public function testGetSnaks( Reference $reference ) { |
||
110 | $snaks = $reference->getSnaks(); |
||
111 | |||
112 | $this->assertInstanceOf( 'Wikibase\DataModel\Snak\SnakList', $snaks ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Provides the same reference with its snak list in an unordered and in the ordered state as it |
||
117 | * would result from issuing SnakList::orderByProperty(). |
||
118 | * @return array |
||
119 | */ |
||
120 | public function unorderedReferenceProvider() { |
||
121 | $ids = array( |
||
122 | new PropertyId( 'P1' ), |
||
123 | new PropertyId( 'P2' ), |
||
124 | new PropertyId( 'P3' ), |
||
125 | new PropertyId( 'P4' ), |
||
126 | ); |
||
127 | |||
128 | $snakListArgs = array( |
||
129 | array( |
||
130 | new SnakList( array( |
||
131 | new PropertyValueSnak( $ids[0], new StringValue( 'a' ) ), |
||
132 | new PropertyNoValueSnak( $ids[1] ), |
||
133 | new PropertySomeValueSnak( $ids[0] ), |
||
134 | ) ), |
||
135 | new SnakList( array( |
||
136 | new PropertyValueSnak( $ids[0], new StringValue( 'a' ) ), |
||
137 | new PropertySomeValueSnak( $ids[0] ), |
||
138 | new PropertyNoValueSnak( $ids[1] ), |
||
139 | ) ) |
||
140 | ), |
||
141 | array( |
||
142 | new SnakList( array( |
||
143 | new PropertyNoValueSnak( $ids[1] ), |
||
144 | new PropertyNoValueSnak( $ids[0] ), |
||
145 | new PropertySomeValueSnak( $ids[1] ), |
||
146 | new PropertyNoValueSnak( $ids[2] ), |
||
147 | new PropertySomeValueSnak( $ids[0] ), |
||
148 | new PropertyNoValueSnak( $ids[3] ), |
||
149 | ) ), |
||
150 | new SnakList( array( |
||
151 | new PropertyNoValueSnak( $ids[1] ), |
||
152 | new PropertySomeValueSnak( $ids[1] ), |
||
153 | new PropertyNoValueSnak( $ids[0] ), |
||
154 | new PropertySomeValueSnak( $ids[0] ), |
||
155 | new PropertyNoValueSnak( $ids[2] ), |
||
156 | new PropertyNoValueSnak( $ids[3] ), |
||
157 | ) ), |
||
158 | ), |
||
159 | ); |
||
160 | |||
161 | $args = array(); |
||
162 | |||
163 | foreach ( $snakListArgs as $snakLists ) { |
||
164 | $args[] = array( |
||
165 | new Reference( $snakLists[0] ), |
||
166 | new Reference( $snakLists[1] ), |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | return $args; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @dataProvider unorderedReferenceProvider |
||
175 | */ |
||
176 | public function testUnorderedReference( Reference $unorderedReference, Reference $orderedReference ) { |
||
179 | |||
180 | public function testReferenceEqualsItself() { |
||
184 | |||
185 | public function testReferenceDoesNotEqualReferenceWithDifferentSnakProperty() { |
||
186 | $reference0 = new Reference( array( new PropertyNoValueSnak( 42 ) ) ); |
||
187 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1337 ) ) ); |
||
188 | $this->assertFalse( $reference0->equals( $reference1 ) ); |
||
189 | } |
||
190 | |||
191 | public function testReferenceDoesNotEqualReferenceWithMoreSnaks() { |
||
192 | $reference0 = new Reference( array( new PropertyNoValueSnak( 42 ) ) ); |
||
193 | |||
194 | $reference1 = new Reference( array( |
||
195 | new PropertyNoValueSnak( 42 ), |
||
196 | new PropertySomeValueSnak( 42 ), |
||
197 | ) ); |
||
198 | |||
199 | $this->assertFalse( $reference0->equals( $reference1 ) ); |
||
200 | } |
||
201 | |||
202 | public function testReferenceEqualsReferenceWithDifferentSnakOrder() { |
||
203 | $reference0 = new Reference( array( |
||
204 | new PropertyNoValueSnak( 1337 ), |
||
205 | new PropertyNoValueSnak( 42 ), |
||
206 | ) ); |
||
207 | |||
208 | $reference1 = new Reference( array( |
||
209 | new PropertyNoValueSnak( 42 ), |
||
210 | new PropertyNoValueSnak( 1337 ), |
||
211 | ) ); |
||
212 | |||
213 | $this->assertTrue( $reference0->equals( $reference1 ) ); |
||
214 | } |
||
215 | |||
216 | public function testReferencesWithDifferentSnakOrderHaveTheSameHash() { |
||
217 | $reference0 = new Reference( array( |
||
218 | new PropertySomeValueSnak( 1337 ), |
||
219 | new PropertyNoValueSnak( 1337 ), |
||
220 | new PropertyNoValueSnak( 42 ), |
||
221 | ) ); |
||
222 | |||
223 | $reference1 = new Reference( array( |
||
224 | new PropertyNoValueSnak( 1337 ), |
||
225 | new PropertyNoValueSnak( 42 ), |
||
226 | new PropertySomeValueSnak( 1337 ), |
||
227 | ) ); |
||
228 | |||
229 | $this->assertSame( $reference0->getHash(), $reference1->getHash() ); |
||
230 | } |
||
231 | |||
232 | public function testCanConstructWithSnakArray() { |
||
233 | $snaks = array( |
||
234 | new PropertyNoValueSnak( 1 ), |
||
235 | new PropertyNoValueSnak( 3 ), |
||
236 | new PropertyNoValueSnak( 2 ), |
||
237 | ); |
||
238 | |||
239 | $reference = new Reference( $snaks ); |
||
240 | |||
241 | $this->assertEquals( |
||
242 | new SnakList( $snaks ), |
||
243 | $reference->getSnaks() |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @dataProvider invalidConstructorArgumentsProvider |
||
249 | * @expectedException InvalidArgumentException |
||
250 | */ |
||
251 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $snaks ) { |
||
254 | |||
255 | public function invalidConstructorArgumentsProvider() { |
||
256 | $id1 = new PropertyId( 'P1' ); |
||
257 | |||
258 | return array( |
||
259 | array( null ), |
||
260 | array( false ), |
||
261 | array( 1 ), |
||
262 | array( 0.1 ), |
||
263 | array( 'string' ), |
||
271 | |||
272 | public function testIsEmpty() { |
||
287 | |||
288 | } |
||
289 |