Skip to content

Commit f4103b6

Browse files
committed
More StatementList tests
1 parent 69ab99e commit f4103b6

2 files changed

Lines changed: 63 additions & 36 deletions

File tree

src/Statement/StatementList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct( $statements = array() /*...*/ ) {
5454
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
5555
}
5656

57-
$this->statements[] = $statement;
57+
$this->addStatement( $statement );
5858
}
5959
}
6060

tests/unit/Statement/StatementListTest.php

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,66 @@
1717
*
1818
* @licence GNU GPL v2+
1919
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
20+
* @author Thiemo Mättig
2021
*/
2122
class StatementListTest extends \PHPUnit_Framework_TestCase {
2223

24+
/**
25+
* @param int $propertyId
26+
* @param string|null $guid
27+
* @param int $rank
28+
*
29+
* @return Statement
30+
*/
31+
private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
32+
$statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' )
33+
->disableOriginalConstructor()
34+
->getMock();
35+
36+
$statement->expects( $this->any() )
37+
->method( 'getGuid' )
38+
->will( $this->returnValue( $guid ) );
39+
40+
$statement->expects( $this->any() )
41+
->method( 'getPropertyId' )
42+
->will( $this->returnValue( PropertyId::newFromNumber( $propertyId ) ) );
43+
44+
$statement->expects( $this->any() )
45+
->method( 'getRank' )
46+
->will( $this->returnValue( $rank ) );
47+
48+
return $statement;
49+
}
50+
51+
private function getStatementWithSnak( $propertyId, $stringValue ) {
52+
$snak = $this->newSnak( $propertyId, $stringValue );
53+
$statement = new Statement( $snak );
54+
$statement->setGuid( sha1( $snak->getHash() ) );
55+
return $statement;
56+
}
57+
58+
private function newSnak( $propertyId, $stringValue ) {
59+
return new PropertyValueSnak( $propertyId, new StringValue( $stringValue ) );
60+
}
61+
62+
public function testConstructorAcceptsDuplicatesWithNoGuid() {
63+
$list = new StatementList(
64+
$this->getStatement( 1, null ),
65+
$this->getStatement( 1, null )
66+
);
67+
68+
$this->assertSame( 2, $list->count() );
69+
}
70+
71+
public function testConstructorAcceptsDuplicatesWithSameGuid() {
72+
$list = new StatementList(
73+
$this->getStatement( 1, 'duplicate' ),
74+
$this->getStatement( 1, 'duplicate' )
75+
);
76+
77+
$this->assertSame( 2, $list->count() );
78+
}
79+
2380
public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() {
2481
$list = new StatementList();
2582
$this->assertSame( array(), $list->getPropertyIds() );
@@ -44,37 +101,18 @@ public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates(
44101
);
45102
}
46103

47-
public function testGivenStatementsWithArrayKeys_reindexesArray() {
104+
public function testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() {
48105
$statement = $this->getStatement( 1, 'guid' );
49106
$list = new StatementList( array( 'ignore-me' => $statement ) );
50107

51108
$this->assertSame( array( 0 => $statement ), $list->toArray() );
52109
}
53110

54-
/**
55-
* @param int $propertyId
56-
* @param string $guid
57-
* @param int $rank
58-
*
59-
* @return Statement
60-
*/
61-
private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
62-
$statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' )
63-
->disableOriginalConstructor()->getMock();
64-
65-
$statement->expects( $this->any() )
66-
->method( 'getGuid' )
67-
->will( $this->returnValue( $guid ) );
68-
69-
$statement->expects( $this->any() )
70-
->method( 'getPropertyId' )
71-
->will( $this->returnValue( PropertyId::newFromNumber( $propertyId ) ) );
72-
73-
$statement->expects( $this->any() )
74-
->method( 'getRank' )
75-
->will( $this->returnValue( $rank ) );
111+
public function testGivenSparseArray_toArrayReturnsReindexedArray() {
112+
$statement = $this->getStatement( 1, 'guid' );
113+
$list = new StatementList( array( 1 => $statement ) );
76114

77-
return $statement;
115+
$this->assertSame( array( 0 => $statement ), $list->toArray() );
78116
}
79117

80118
public function testCanIterate() {
@@ -154,17 +192,6 @@ public function testGetAllSnaksReturnsAllSnaks() {
154192
);
155193
}
156194

157-
private function getStatementWithSnak( $propertyId, $stringValue ) {
158-
$snak = $this->newSnak( $propertyId, $stringValue );
159-
$statement = new Statement( $snak );
160-
$statement->setGuid( sha1( $snak->getHash() ) );
161-
return $statement;
162-
}
163-
164-
private function newSnak( $propertyId, $stringValue ) {
165-
return new PropertyValueSnak( $propertyId, new StringValue( $stringValue ) );
166-
}
167-
168195
public function testAddStatementWithOnlyMainSnak() {
169196
$list = new StatementList();
170197

0 commit comments

Comments
 (0)