|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Wikibase\DataModel\Tests\Snak; |
| 4 | + |
| 5 | +use DataValues\StringValue; |
| 6 | +use Wikibase\DataModel\Entity\PropertyId; |
| 7 | +use Wikibase\DataModel\Snak\DerivedPropertyValueSnak; |
| 8 | + |
| 9 | +/** |
| 10 | + * @covers Wikibase\DataModel\Snak\DerivedPropertyValueSnak |
| 11 | + * |
| 12 | + * @group Wikibase |
| 13 | + * @group WikibaseDataModel |
| 14 | + * @group WikibaseSnak |
| 15 | + * |
| 16 | + * @licence GNU GPL v2+ |
| 17 | + * @author Adam Shorland |
| 18 | + */ |
| 19 | +class DerivedPropertyValueSnakTest extends SnakObjectTest { |
| 20 | + |
| 21 | + public function constructorProvider() { |
| 22 | + return array( |
| 23 | + 'No extras' => array( |
| 24 | + true, |
| 25 | + new PropertyId( 'P1' ), |
| 26 | + new StringValue( 'a' ), |
| 27 | + array(), |
| 28 | + ), |
| 29 | + '2 extras' => array( |
| 30 | + true, |
| 31 | + new PropertyId( 'P9001' ), |
| 32 | + new StringValue( 'bc' ), |
| 33 | + array( 'foo' => new StringValue( 'foo' ), 'bar' => new StringValue( 'bar' ) ), |
| 34 | + ), |
| 35 | + 'fail - Integer key' => array( |
| 36 | + 'InvalidArgumentException', |
| 37 | + new PropertyId( 'P9001' ), |
| 38 | + new StringValue( 'bc' ), |
| 39 | + array( new StringValue( 'foo' ) ), |
| 40 | + ), |
| 41 | + 'fail - not a value' => array( |
| 42 | + 'InvalidArgumentException', |
| 43 | + new PropertyId( 'P9001' ), |
| 44 | + new StringValue( 'bc' ), |
| 45 | + array( 'foo' => 'bar' ), |
| 46 | + ), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function getClass() { |
| 51 | + return 'Wikibase\DataModel\Snak\DerivedPropertyValueSnak'; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This test is a safeguard to make sure hashes are not changed unintentionally. |
| 56 | + */ |
| 57 | + public function testHashStability() { |
| 58 | + $snak = new DerivedPropertyValueSnak( |
| 59 | + new PropertyId( 'P1' ), |
| 60 | + new StringValue( 'a' ), |
| 61 | + array( 'foo' => new StringValue( 'foo' ) ) |
| 62 | + ); |
| 63 | + $hash = $snak->getHash(); |
| 64 | + |
| 65 | + // @codingStandardsIgnoreStart |
| 66 | + $expected = sha1( 'C:48:"Wikibase\DataModel\Snak\DerivedPropertyValueSnak":53:{a:2:{i:0;i:1;i:1;C:22:"DataValues\StringValue":1:{a}}}' ); |
| 67 | + // @codingStandardsIgnoreEnd |
| 68 | + $this->assertSame( $expected, $hash ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider instanceProvider |
| 73 | + */ |
| 74 | + public function testGetDerivedDataValues( DerivedPropertyValueSnak $snak, $args ) { |
| 75 | + $dataValues = $snak->getDerivedDataValues(); |
| 76 | + $this->assertEquals( $args[2], $dataValues ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dataProvider instanceProvider |
| 81 | + */ |
| 82 | + public function testGetDerivedDataValue( DerivedPropertyValueSnak $snak, $args ) { |
| 83 | + $this->assertEquals( null, $snak->getDerivedDataValue( 'ponies and unicorns' ) ); |
| 84 | + foreach ( $args[2] as $expectedKey => $expectedDataValue ) { |
| 85 | + $dataValue = $snak->getDerivedDataValue( $expectedKey ); |
| 86 | + $this->assertEquals( $expectedDataValue, $dataValue ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments