1 | <?php |
||
29 | class ByPropertyIdArrayTest extends \PHPUnit_Framework_TestCase { |
||
30 | |||
31 | public function testArrayObjectNotConstructedFromObject() { |
||
32 | $statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); |
||
33 | $statement1->setGuid( '1' ); |
||
34 | $statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); |
||
35 | $statement2->setGuid( '2' ); |
||
36 | |||
37 | $object = new ArrayObject(); |
||
38 | $object->append( $statement1 ); |
||
39 | |||
40 | $byPropertyIdArray = new ByPropertyIdArray( $object ); |
||
|
|||
41 | // According to the documentation append() "cannot be called when the ArrayObject was |
||
42 | // constructed from an object." This test makes sure it was not constructed from an object. |
||
43 | $byPropertyIdArray->append( $statement2 ); |
||
44 | |||
45 | $this->assertCount( 2, $byPropertyIdArray ); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns an accessible ReflectionMethod of ByPropertyIdArray. |
||
50 | * |
||
51 | * @param string $methodName |
||
52 | * @return ReflectionMethod |
||
53 | */ |
||
54 | protected static function getMethod( $methodName ) { |
||
55 | $class = new ReflectionClass( 'Wikibase\DataModel\ByPropertyIdArray' ); |
||
56 | $method = $class->getMethod( $methodName ); |
||
57 | $method->setAccessible( true ); |
||
58 | return $method; |
||
59 | } |
||
60 | |||
61 | public function listProvider() { |
||
62 | $lists = array(); |
||
63 | |||
64 | $snaks = array( |
||
65 | new PropertyNoValueSnak( new PropertyId( 'P42' ) ), |
||
66 | new PropertySomeValueSnak( new PropertyId( 'P42' ) ), |
||
67 | new PropertySomeValueSnak( new PropertyId( 'P10' ) ), |
||
68 | new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ), |
||
69 | new PropertySomeValueSnak( new PropertyId( 'P1' ) ), |
||
70 | ); |
||
71 | |||
72 | $lists[] = $snaks; |
||
73 | |||
74 | $lists[] = array_map( |
||
75 | function( Snak $snak ) { |
||
76 | return new Statement( $snak ); |
||
77 | }, |
||
78 | $snaks |
||
79 | ); |
||
80 | |||
81 | $argLists = array(); |
||
82 | |||
83 | foreach ( $lists as $list ) { |
||
84 | $argLists[] = array( $list ); |
||
85 | } |
||
86 | |||
87 | return $argLists; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Statement[] |
||
92 | */ |
||
93 | protected function statementsProvider() { |
||
94 | $snaks = array( |
||
95 | new PropertyNoValueSnak( new PropertyId( 'P1' ) ), |
||
96 | new PropertySomeValueSnak( new PropertyId( 'P1' ) ), |
||
97 | new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ), |
||
98 | new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ), |
||
99 | new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ), |
||
100 | new PropertySomeValueSnak( new PropertyId( 'P3' ) ), |
||
101 | ); |
||
102 | |||
103 | return array_map( |
||
104 | function( Snak $snak ) { |
||
105 | return new Statement( $snak ); |
||
106 | }, |
||
107 | $snaks |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @dataProvider listProvider |
||
113 | * @param PropertyIdProvider[] $objects |
||
114 | */ |
||
115 | public function testGetIds( array $objects ) { |
||
116 | $indexedArray = new ByPropertyIdArray( $objects ); |
||
117 | |||
118 | $expected = array(); |
||
119 | |||
120 | foreach ( $objects as $object ) { |
||
121 | $expected[] = $object->getPropertyId(); |
||
122 | } |
||
123 | |||
124 | $expected = array_unique( $expected ); |
||
125 | |||
126 | $indexedArray->buildIndex(); |
||
127 | |||
128 | $this->assertEquals( |
||
129 | array_values( $expected ), |
||
130 | array_values( $indexedArray->getPropertyIds() ) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @dataProvider listProvider |
||
136 | * @param PropertyIdProvider[] $objects |
||
137 | */ |
||
138 | public function testGetById( array $objects ) { |
||
139 | $indexedArray = new ByPropertyIdArray( $objects ); |
||
140 | |||
141 | $ids = array(); |
||
142 | |||
143 | foreach ( $objects as $object ) { |
||
144 | $ids[] = $object->getPropertyId(); |
||
145 | } |
||
146 | |||
147 | $ids = array_unique( $ids ); |
||
148 | |||
149 | $indexedArray->buildIndex(); |
||
150 | |||
151 | $allObtainedObjects = array(); |
||
152 | |||
153 | foreach ( $ids as $id ) { |
||
154 | foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) { |
||
155 | $allObtainedObjects[] = $obtainedObject; |
||
156 | $this->assertEquals( $id, $obtainedObject->getPropertyId() ); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $this->assertEquals( |
||
161 | array_values( $objects ), |
||
162 | array_values( $allObtainedObjects ) |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @dataProvider listProvider |
||
168 | * @param PropertyIdProvider[] $objects |
||
169 | */ |
||
170 | public function testRemoveObject( array $objects ) { |
||
192 | |||
193 | public function testGetByNotSetIdThrowsException() { |
||
194 | $indexedArray = new ByPropertyIdArray(); |
||
195 | $indexedArray->buildIndex(); |
||
201 | |||
202 | public function testNotBuildExceptionIsThrownForByPropertyId() { |
||
208 | |||
209 | public function testNotBuildExceptionIsThrownForGetPropertyIds() { |
||
215 | |||
216 | /** |
||
217 | * @dataProvider listProvider |
||
218 | */ |
||
219 | public function testGetFlatArrayIndexOfObject( array $objects ) { |
||
234 | |||
235 | /** |
||
236 | * @dataProvider listProvider |
||
237 | */ |
||
238 | public function testToFlatArray( array $objects ) { |
||
244 | |||
245 | public function moveProvider() { |
||
299 | |||
300 | /** |
||
301 | * @dataProvider moveProvider |
||
302 | */ |
||
303 | public function testMoveObjectToIndex( |
||
323 | |||
324 | public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() { |
||
333 | |||
334 | public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() { |
||
343 | |||
344 | public function addProvider() { |
||
374 | |||
375 | /** |
||
376 | * @dataProvider addProvider |
||
377 | */ |
||
378 | public function testAddObjectAtIndex( |
||
391 | |||
392 | } |
||
393 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.