These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Wikibase\DataModel\Tests\Claim; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | use ReflectionClass; |
||
| 7 | use Wikibase\DataModel\Claim\Claims; |
||
| 8 | use Wikibase\DataModel\Entity\PropertyId; |
||
| 9 | use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
||
| 10 | use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
||
| 11 | use Wikibase\DataModel\Snak\Snak; |
||
| 12 | use Wikibase\DataModel\Statement\Statement; |
||
| 13 | use Wikibase\DataModel\Statement\StatementList; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @covers Wikibase\DataModel\Claim\Claims |
||
| 17 | * |
||
| 18 | * @since 0.1 |
||
| 19 | * |
||
| 20 | * @group Wikibase |
||
| 21 | * @group WikibaseDataModel |
||
| 22 | * @group WikibaseClaim |
||
| 23 | * |
||
| 24 | * @licence GNU GPL v2+ |
||
| 25 | * @author Daniel Kinzler |
||
| 26 | */ |
||
| 27 | class ClaimsTest extends \PHPUnit_Framework_TestCase { |
||
| 28 | |||
| 29 | private $guidCounter = 0; |
||
| 30 | |||
| 31 | private function makeStatement( Snak $mainSnak, $guid = null ) { |
||
| 32 | if ( $guid === null ) { |
||
| 33 | $this->guidCounter++; |
||
| 34 | $guid = 'TEST$statement-' . $this->guidCounter; |
||
| 35 | } |
||
| 36 | |||
| 37 | $claim = new Statement( $mainSnak ); |
||
| 38 | $claim->setGuid( $guid ); |
||
| 39 | |||
| 40 | return $claim; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testArrayObjectNotConstructedFromObject() { |
||
| 44 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( 1 ) ); |
||
| 45 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( 2 ) ); |
||
| 46 | |||
| 47 | $statementList = new StatementList(); |
||
| 48 | $statementList->addStatement( $statement1 ); |
||
| 49 | |||
| 50 | $claims = new Claims( $statementList ); |
||
| 51 | // According to the documentation append() "cannot be called when the ArrayObject was |
||
| 52 | // constructed from an object." This test makes sure it was not constructed from an object. |
||
| 53 | $claims->append( $statement2 ); |
||
| 54 | |||
| 55 | $this->assertSame( 2, $claims->count() ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @dataProvider constructorErrorProvider |
||
| 60 | */ |
||
| 61 | public function testConstructorError() { |
||
| 62 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
| 63 | |||
| 64 | $class = new ReflectionClass( 'Wikibase\DataModel\Claim\Claims' ); |
||
| 65 | $class->newInstanceArgs( func_get_args() ); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function constructorErrorProvider() { |
||
| 69 | return array( |
||
| 70 | array( 17 ), |
||
| 71 | array( array( 'foo' ) ), |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testHasClaimWithGuid() { |
||
| 76 | $claims = new Claims(); |
||
| 77 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 78 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 79 | |||
| 80 | $this->assertFalse( $claims->hasClaimWithGuid( $statement1->getGuid() ) ); |
||
| 81 | $this->assertFalse( $claims->hasClaimWithGuid( $statement2->getGuid() ) ); |
||
| 82 | |||
| 83 | $claims->addClaim( $statement1 ); |
||
| 84 | $this->assertTrue( $claims->hasClaimWithGuid( $statement1->getGuid() ) ); |
||
| 85 | $this->assertFalse( $claims->hasClaimWithGuid( $statement2->getGuid() ) ); |
||
| 86 | |||
| 87 | $claims->addClaim( $statement2 ); |
||
| 88 | $this->assertTrue( $claims->hasClaimWithGuid( $statement1->getGuid() ) ); |
||
| 89 | $this->assertTrue( $claims->hasClaimWithGuid( $statement2->getGuid() ) ); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testRemoveClaimWithGuid() { |
||
| 93 | $claims = new Claims(); |
||
| 94 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 95 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 96 | |||
| 97 | $claims->addClaim( $statement1 ); |
||
| 98 | $claims->addClaim( $statement2 ); |
||
| 99 | $this->assertSame( 2, $claims->count() ); |
||
| 100 | |||
| 101 | $claims->removeClaimWithGuid( $statement1->getGuid() ); |
||
| 102 | $this->assertSame( 1, $claims->count() ); |
||
| 103 | |||
| 104 | $claims->removeClaimWithGuid( $statement2->getGuid() ); |
||
| 105 | $this->assertSame( 0, $claims->count() ); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function testOffsetUnset() { |
||
| 109 | $claims = new Claims(); |
||
| 110 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 111 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 112 | |||
| 113 | $claims->addClaim( $statement1 ); |
||
| 114 | $claims->addClaim( $statement2 ); |
||
| 115 | $this->assertSame( 2, $claims->count() ); |
||
| 116 | |||
| 117 | $claims->offsetUnset( $statement1->getGuid() ); |
||
| 118 | $this->assertSame( 1, $claims->count() ); |
||
| 119 | |||
| 120 | $claims->offsetUnset( $statement2->getGuid() ); |
||
| 121 | $this->assertSame( 0, $claims->count() ); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function testGetClaimWithGuid() { |
||
| 125 | $claims = new Claims(); |
||
| 126 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 127 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 128 | |||
| 129 | $claims->addClaim( $statement1 ); |
||
| 130 | $this->assertSame( $statement1, $claims->getClaimWithGuid( $statement1->getGuid() ) ); |
||
| 131 | $this->assertNull( $claims->getClaimWithGuid( $statement2->getGuid() ) ); |
||
| 132 | |||
| 133 | $claims->addClaim( $statement2 ); |
||
| 134 | $this->assertSame( $statement1, $claims->getClaimWithGuid( $statement1->getGuid() ) ); |
||
| 135 | $this->assertSame( $statement2, $claims->getClaimWithGuid( $statement2->getGuid() ) ); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testOffsetGet() { |
||
| 139 | $claims = new Claims(); |
||
| 140 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 141 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 142 | |||
| 143 | $claims->addClaim( $statement1 ); |
||
| 144 | $this->assertSame( $statement1, $claims->offsetGet( $statement1->getGuid() ) ); |
||
| 145 | |||
| 146 | $claims->addClaim( $statement2 ); |
||
| 147 | $this->assertSame( $statement1, $claims->offsetGet( $statement1->getGuid() ) ); |
||
| 148 | $this->assertSame( $statement2, $claims->offsetGet( $statement2->getGuid() ) ); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testAddClaim() { |
||
| 152 | $claims = new Claims(); |
||
| 153 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 154 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 155 | |||
| 156 | $claims->addClaim( $statement1 ); |
||
| 157 | $claims->addClaim( $statement2 ); |
||
| 158 | |||
| 159 | $this->assertSame( 2, $claims->count() ); |
||
| 160 | $this->assertEquals( $statement1, $claims[$statement1->getGuid()] ); |
||
| 161 | $this->assertEquals( $statement2, $claims[$statement2->getGuid()] ); |
||
| 162 | |||
| 163 | $claims->addClaim( $statement1 ); |
||
| 164 | $claims->addClaim( $statement2 ); |
||
| 165 | |||
| 166 | $this->assertSame( 2, $claims->count() ); |
||
| 167 | |||
| 168 | $this->assertNotNull( $claims->getClaimWithGuid( $statement1->getGuid() ) ); |
||
| 169 | $this->assertNotNull( $claims->getClaimWithGuid( $statement2->getGuid() ) ); |
||
| 170 | |||
| 171 | $this->assertSame( array( |
||
| 172 | 'TEST$STATEMENT-1' => $statement1, |
||
| 173 | 'TEST$STATEMENT-2' => $statement2, |
||
| 174 | ), $claims->getArrayCopy() ); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @expectedException InvalidArgumentException |
||
| 179 | */ |
||
| 180 | public function testAddClaimWithIndexFails() { |
||
| 181 | $claims = new Claims(); |
||
| 182 | $statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
| 183 | $claims->addClaim( $statement, 0 ); |
||
|
0 ignored issues
–
show
|
|||
| 184 | } |
||
| 185 | |||
| 186 | public function testAppend() { |
||
| 187 | $claims = new Claims(); |
||
| 188 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 189 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 190 | |||
| 191 | $claims->append( $statement1 ); |
||
| 192 | $claims->append( $statement2 ); |
||
| 193 | |||
| 194 | $this->assertSame( 2, $claims->count() ); |
||
| 195 | $this->assertEquals( $statement1, $claims[$statement1->getGuid()] ); |
||
| 196 | $this->assertEquals( $statement2, $claims[$statement2->getGuid()] ); |
||
| 197 | |||
| 198 | $claims->append( $statement1 ); |
||
| 199 | $claims->append( $statement2 ); |
||
| 200 | |||
| 201 | $this->assertSame( 2, $claims->count() ); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function testAppendOperator() { |
||
| 205 | $claims = new Claims(); |
||
| 206 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 207 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 208 | |||
| 209 | $claims[] = $statement1; |
||
| 210 | $claims[] = $statement2; |
||
| 211 | |||
| 212 | $this->assertSame( 2, $claims->count() ); |
||
| 213 | $this->assertEquals( $statement1, $claims[$statement1->getGuid()] ); |
||
| 214 | $this->assertEquals( $statement2, $claims[$statement2->getGuid()] ); |
||
| 215 | |||
| 216 | $claims[] = $statement1; |
||
| 217 | $claims[] = $statement2; |
||
| 218 | |||
| 219 | $this->assertSame( 2, $claims->count() ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @expectedException InvalidArgumentException |
||
| 224 | */ |
||
| 225 | public function testAppendWithNonClaimFails() { |
||
| 226 | $claims = new Claims(); |
||
| 227 | $claims->append( 'bad' ); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function testOffsetSet() { |
||
| 231 | $claims = new Claims(); |
||
| 232 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 233 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 234 | |||
| 235 | $claims->offsetSet( $statement1->getGuid(), $statement1 ); |
||
| 236 | $claims->offsetSet( $statement2->getGuid(), $statement2 ); |
||
| 237 | |||
| 238 | $this->assertSame( 2, $claims->count() ); |
||
| 239 | $this->assertEquals( $statement1, $claims[$statement1->getGuid()] ); |
||
| 240 | $this->assertEquals( $statement2, $claims[$statement2->getGuid()] ); |
||
| 241 | |||
| 242 | $claims->offsetSet( $statement1->getGuid(), $statement1 ); |
||
| 243 | $claims->offsetSet( $statement2->getGuid(), $statement2 ); |
||
| 244 | |||
| 245 | $this->assertSame( 2, $claims->count() ); |
||
| 246 | |||
| 247 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
| 248 | $claims->offsetSet( 'spam', $statement1 ); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function testOffsetSetOperator() { |
||
| 252 | $claims = new Claims(); |
||
| 253 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 254 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 255 | |||
| 256 | $claims[$statement1->getGuid()] = $statement1; |
||
| 257 | $claims[$statement2->getGuid()] = $statement2; |
||
| 258 | |||
| 259 | $this->assertSame( 2, $claims->count() ); |
||
| 260 | $this->assertEquals( $statement1, $claims[$statement1->getGuid()] ); |
||
| 261 | $this->assertEquals( $statement2, $claims[$statement2->getGuid()] ); |
||
| 262 | |||
| 263 | $claims[$statement1->getGuid()] = $statement1; |
||
| 264 | $claims[$statement2->getGuid()] = $statement2; |
||
| 265 | |||
| 266 | $this->assertSame( 2, $claims->count() ); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function testGuidNormalization() { |
||
| 270 | $claims = new Claims(); |
||
| 271 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P15' ) ) ); |
||
| 272 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P16' ) ) ); |
||
| 273 | |||
| 274 | $claim1LowerGuid = strtolower( $statement1->getGuid() ); |
||
| 275 | $claim2UpperGuid = strtoupper( $statement2->getGuid() ); |
||
| 276 | |||
| 277 | $claims->addClaim( $statement1 ); |
||
| 278 | $claims->addClaim( $statement2 ); |
||
| 279 | $this->assertSame( 2, $claims->count() ); |
||
| 280 | |||
| 281 | $this->assertEquals( $statement1, $claims->getClaimWithGuid( $claim1LowerGuid ) ); |
||
| 282 | $this->assertEquals( $statement2, $claims->getClaimWithGuid( $claim2UpperGuid ) ); |
||
| 283 | |||
| 284 | $this->assertEquals( $statement1, $claims->offsetGet( $claim1LowerGuid ) ); |
||
| 285 | $this->assertEquals( $statement2, $claims->offsetGet( $claim2UpperGuid ) ); |
||
| 286 | |||
| 287 | $this->assertEquals( $statement1, $claims[$claim1LowerGuid] ); |
||
| 288 | $this->assertEquals( $statement2, $claims[$claim2UpperGuid] ); |
||
| 289 | |||
| 290 | $claims = new Claims(); |
||
| 291 | $claims->offsetSet( strtoupper( $claim1LowerGuid ), $statement1 ); |
||
| 292 | $claims->offsetSet( strtolower( $claim2UpperGuid ), $statement2 ); |
||
| 293 | $this->assertSame( 2, $claims->count() ); |
||
| 294 | |||
| 295 | $this->assertEquals( $statement1, $claims->getClaimWithGuid( $claim1LowerGuid ) ); |
||
| 296 | $this->assertEquals( $statement2, $claims->getClaimWithGuid( $claim2UpperGuid ) ); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Attempts to add Claims with no GUID set will fail. |
||
| 301 | */ |
||
| 302 | public function testNoGuidFailure() { |
||
| 303 | $statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
| 304 | $list = new Claims(); |
||
| 305 | |||
| 306 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
| 307 | $list->addClaim( $statement ); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function testDuplicateClaims() { |
||
| 311 | $statement1 = $this->makeStatement( new PropertyNoValueSnak( 42 ) ); |
||
| 312 | $statement2 = $this->makeStatement( new PropertyNoValueSnak( 42 ) ); |
||
| 313 | |||
| 314 | $list = new Claims(); |
||
| 315 | $list->addClaim( $statement1 ); |
||
| 316 | $list->addClaim( $statement2 ); |
||
| 317 | |||
| 318 | $this->assertEquals( 2, $list->count(), 'Adding two duplicates to an empty list should result in a count of two' ); |
||
| 319 | |||
| 320 | $this->assertEquals( $statement1, $list->getClaimWithGuid( $statement1->getGuid() ) ); |
||
| 321 | $this->assertEquals( $statement2, $list->getClaimWithGuid( $statement2->getGuid() ) ); |
||
| 322 | |||
| 323 | $list->removeClaimWithGuid( $statement2->getGuid() ); |
||
| 324 | |||
| 325 | $this->assertNotNull( $list->getClaimWithGuid( $statement1->getGuid() ) ); |
||
| 326 | $this->assertNull( $list->getClaimWithGuid( $statement2->getGuid() ) ); |
||
| 327 | } |
||
| 328 | |||
| 329 | public function testIterator() { |
||
| 330 | $expected = array( |
||
| 331 | 'GUID1' => $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P42' ) ), 'guid1' ), |
||
| 332 | 'GUID2' => $this->makeStatement( new PropertySomeValueSnak( new PropertyId( 'P42' ) ), 'guid2' ), |
||
| 333 | 'GUID3' => $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P23' ) ), 'guid3' ), |
||
| 334 | 'GUID4' => $this->makeStatement( new PropertyNoValueSnak( new PropertyId( 'P9000' ) ), 'guid4' ), |
||
| 335 | ); |
||
| 336 | |||
| 337 | $claims = new Claims( $expected ); |
||
| 338 | $actual = iterator_to_array( $claims->getIterator() ); |
||
| 339 | |||
| 340 | $this->assertSame( $expected, $actual ); |
||
| 341 | } |
||
| 342 | |||
| 343 | } |
||
| 344 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.