|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Wikibase\DataModel\Tests\Statement; |
| 4 | + |
| 5 | +use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
| 6 | +use Wikibase\DataModel\Statement\Statement; |
| 7 | +use Wikibase\DataModel\Statement\StatementByGuidMap; |
| 8 | + |
| 9 | +/** |
| 10 | + * @covers Wikibase\DataModel\Statement\StatementByGuidMap |
| 11 | + * |
| 12 | + * @licence GNU GPL v2+ |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + * @author Kai Nissen < kai.nissen@wikimedia.de > |
| 15 | + */ |
| 16 | +class StatementByGuidMapTest extends \PHPUnit_Framework_TestCase { |
| 17 | + |
| 18 | + public function testGivenNotPresentGuid_hasClaimWithGuidReturnsFalse() { |
| 19 | + $statements = new StatementByGuidMap(); |
| 20 | + |
| 21 | + $this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) ); |
| 22 | + } |
| 23 | + |
| 24 | + public function testGivenPresentGuid_hasStatementWithGuidReturnsTrue() { |
| 25 | + |
| 26 | + $statements = new StatementByGuidMap( array( |
| 27 | + $this->newStatement( 1, 'some guid' ) |
| 28 | + ) ); |
| 29 | + |
| 30 | + $this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
| 31 | + } |
| 32 | + |
| 33 | + private function newStatement( $propertyId, $guid ) { |
| 34 | + $statement = new Statement( new PropertyNoValueSnak( $propertyId ) ); |
| 35 | + $statement->setGuid( $guid ); |
| 36 | + return $statement; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider nonStringProvider |
| 41 | + */ |
| 42 | + public function testGivenNonStringGuid_hasClaimWithGuidThrowsException( $nonString ) { |
| 43 | + $statements = new StatementByGuidMap(); |
| 44 | + |
| 45 | + $this->setExpectedException( 'InvalidArgumentException' ); |
| 46 | + $statements->hasStatementWithGuid( $nonString ); |
| 47 | + } |
| 48 | + |
| 49 | + public function nonStringProvider() { |
| 50 | + return array( |
| 51 | + array( null ), |
| 52 | + array( 42 ), |
| 53 | + array( 4.2 ), |
| 54 | + array( array() ), |
| 55 | + array( (object)array() ), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGivenGuidOfPresentStatement_getStatementByGuidReturnsStatement() { |
| 60 | + $statement = $this->newStatement( 1, 'some guid' ); |
| 61 | + |
| 62 | + $statements = new StatementByGuidMap( array( $statement ) ); |
| 63 | + |
| 64 | + $this->assertEquals( $statement, $statements->getStatementByGuid( 'some guid' ) ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGivenGuidOfNotPresentStatement_getStatementByGuidReturnsNull() { |
| 68 | + $statements = new StatementByGuidMap(); |
| 69 | + |
| 70 | + $this->assertNull( $statements->getStatementByGuid( 'some guid' ) ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider nonStringProvider |
| 75 | + */ |
| 76 | + public function testGivenNonStringGuid_getStatementByGuidThrowsException( $nonString ) { |
| 77 | + $statements = new StatementByGuidMap(); |
| 78 | + |
| 79 | + $this->setExpectedException( 'InvalidArgumentException' ); |
| 80 | + $statements->getStatementByGuid( $nonString ); |
| 81 | + } |
| 82 | + |
| 83 | + public function testGivenGuidOfPresentStatement_removeStatementWithGuidRemovesTheStatement() { |
| 84 | + $statement = $this->newStatement( 1, 'some guid' ); |
| 85 | + $statements = new StatementByGuidMap( array( $statement ) ); |
| 86 | + |
| 87 | + $statements->removeStatementWithGuid( 'some guid' ); |
| 88 | + |
| 89 | + $this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) ); |
| 90 | + } |
| 91 | + |
| 92 | + public function testGivenGuidOfNonPresentStatement_removeStatementWithGuidDoesNoOp() { |
| 93 | + $statement = $this->newStatement( 1, 'some guid' ); |
| 94 | + $statements = new StatementByGuidMap( array( $statement ) ); |
| 95 | + |
| 96 | + $statements->removeStatementWithGuid( '-- different guid --' ); |
| 97 | + |
| 98 | + $this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dataProvider nonStringProvider |
| 103 | + */ |
| 104 | + public function testGivenNonStringGuid_removeStatementWithGuidThrowsException( $nonString ) { |
| 105 | + $statements = new StatementByGuidMap(); |
| 106 | + |
| 107 | + $this->setExpectedException( 'InvalidArgumentException' ); |
| 108 | + $statements->removeStatementWithGuid( $nonString ); |
| 109 | + } |
| 110 | + |
| 111 | + public function testGivenStatementWithNoGuid_constructorThrowsException() { |
| 112 | + $this->setExpectedException( 'InvalidArgumentException' ); |
| 113 | + |
| 114 | + new StatementByGuidMap( array( |
| 115 | + $this->newStatement( 1, null ) |
| 116 | + ) ); |
| 117 | + } |
| 118 | + |
| 119 | + public function testCanConstructWithStatementIterable() { |
| 120 | + $statementList = new \ArrayObject( array( |
| 121 | + $this->newStatement( 1, 'some guid' ) |
| 122 | + ) ); |
| 123 | + |
| 124 | + $statementMap = new StatementByGuidMap( $statementList ); |
| 125 | + |
| 126 | + $this->assertTrue( $statementMap->hasStatementWithGuid( 'some guid' ) ); |
| 127 | + } |
| 128 | + |
| 129 | + public function testWhenMapIsEmpty_countReturnsZero() { |
| 130 | + $statements = new StatementByGuidMap(); |
| 131 | + |
| 132 | + $this->assertSame( 0, $statements->count() ); |
| 133 | + } |
| 134 | + |
| 135 | + public function testMapCanBePassedToCount() { |
| 136 | + $statements = new StatementByGuidMap( array( |
| 137 | + $this->newStatement( 1, 'some guid' ), |
| 138 | + $this->newStatement( 2, 'other guid' ) |
| 139 | + ) ); |
| 140 | + |
| 141 | + $this->assertSame( 2, count( $statements ) ); |
| 142 | + } |
| 143 | + |
| 144 | + public function testMapCanBeIteratedOver() { |
| 145 | + $statement1 = $this->newStatement( 1, 'some guid' ); |
| 146 | + $statement2 = $this->newStatement( 2, 'other guid' ); |
| 147 | + |
| 148 | + $statementMap = new StatementByGuidMap( array( $statement1, $statement2 ) ); |
| 149 | + |
| 150 | + $iteratedStatements = array(); |
| 151 | + |
| 152 | + foreach ( $statementMap as $guid => $statement ) { |
| 153 | + $iteratedStatements[$guid] = $statement; |
| 154 | + } |
| 155 | + |
| 156 | + $expectedStatements = array( |
| 157 | + 'some guid' => $statement1, |
| 158 | + 'other guid' => $statement2 |
| 159 | + ); |
| 160 | + |
| 161 | + $this->assertEquals( $expectedStatements, $iteratedStatements ); |
| 162 | + } |
| 163 | + |
| 164 | + public function testGivenNotPresentStatement_addStatementAddsIt() { |
| 165 | + $statements = new StatementByGuidMap(); |
| 166 | + |
| 167 | + $statements->addStatement( $this->newStatement( 1, 'some guid' ) ); |
| 168 | + |
| 169 | + $this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) ); |
| 170 | + } |
| 171 | + |
| 172 | + public function testGivenStatementWithPresentGuid_addStatementReplacesThePresentStatement() { |
| 173 | + $statement1 = $this->newStatement( 1, 'some guid' ); |
| 174 | + $statement2 = $this->newStatement( 2, 'some guid' ); |
| 175 | + |
| 176 | + $statements = new StatementByGuidMap( array( $statement1 ) ); |
| 177 | + |
| 178 | + $statements->addStatement( $statement2 ); |
| 179 | + |
| 180 | + $this->assertEquals( $statement2, $statements->getStatementByGuid( 'some guid' ) ); |
| 181 | + } |
| 182 | + |
| 183 | + public function testToArray() { |
| 184 | + $statement1 = $this->newStatement( 1, 'some guid' ); |
| 185 | + $statement2 = $this->newStatement( 2, 'other guid' ); |
| 186 | + |
| 187 | + $statementMap = new StatementByGuidMap( array( $statement1, $statement2 ) ); |
| 188 | + |
| 189 | + $expectedStatements = array( |
| 190 | + 'some guid' => $statement1, |
| 191 | + 'other guid' => $statement2 |
| 192 | + ); |
| 193 | + |
| 194 | + $this->assertEquals( $expectedStatements, $statementMap->toArray() ); |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments