Skip to content

Commit 824db3c

Browse files
committed
Make StatementList Comparable and Countable
1 parent b5e0008 commit 824db3c

3 files changed

Lines changed: 136 additions & 3 deletions

File tree

src/Claim/ClaimList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Wikibase\DataModel\Snak\Snaks;
1111

1212
/**
13-
* Ordered, non-unique, collection of Claim objects.
13+
* Ordered, non-unique, mutable, collection of Claim objects.
1414
* Provides various filter operations though does not do any indexing by default.
1515
*
1616
* @since 1.0

src/Statement/StatementList.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
use Wikibase\DataModel\Snak\Snaks;
1515

1616
/**
17-
* Ordered, non-unique, collection of Statement objects.
17+
* Ordered, non-unique, mutable, collection of Statement objects.
1818
* Provides various filter operations though does not do any indexing by default.
1919
*
2020
* @since 1.0
2121
*
2222
* @licence GNU GPL v2+
2323
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2424
*/
25-
class StatementList implements \IteratorAggregate {
25+
class StatementList implements \IteratorAggregate, \Comparable, \Countable {
2626

2727
/**
2828
* @var Statement[]
@@ -142,4 +142,45 @@ public function toArray() {
142142
return $this->statements;
143143
}
144144

145+
/**
146+
* @see Countable::count
147+
* @return int
148+
*/
149+
public function count() {
150+
return count( $this->statements );
151+
}
152+
153+
/**
154+
* @see Comparable::equals
155+
*
156+
* @param mixed $statementList
157+
*
158+
* @return boolean
159+
*/
160+
public function equals( $statementList ) {
161+
if ( !( $statementList instanceof self ) ) {
162+
return false;
163+
}
164+
165+
if ( $this->count() !== $statementList->count() ) {
166+
return false;
167+
}
168+
169+
return $this->statementsEqual( $statementList->statements );
170+
}
171+
172+
private function statementsEqual( array $statements ) {
173+
reset( $statements );
174+
175+
foreach ( $this->statements as $statement ) {
176+
if ( !$statement->equals( current( $statements ) ) ) {
177+
return false;
178+
}
179+
180+
next( $statements );
181+
}
182+
183+
return true;
184+
}
185+
145186
}

tests/unit/Statement/StatementListTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,96 @@ public function testGivenNonTraversable_constructorThrowsException() {
248248
new StatementList( null );
249249
}
250250

251+
public function testCountForEmptyList() {
252+
$list = new StatementList();
253+
$this->assertSame( 0, count( $list ) );
254+
$this->assertSame( 0, $list->count() );
255+
}
256+
257+
public function testCountForNonEmptyList() {
258+
$list = new StatementList( array(
259+
$this->getStatementWithSnak( 1, 'foo' ),
260+
$this->getStatementWithSnak( 2, 'bar' ),
261+
) );
262+
263+
$this->assertSame( 2, $list->count() );
264+
}
265+
266+
/**
267+
* @dataProvider statementArrayProvider
268+
*/
269+
public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) {
270+
$firstStatements = new StatementList( $statements );
271+
$secondStatements = new StatementList( $statements );
272+
273+
$this->assertTrue( $firstStatements->equals( $secondStatements ) );
274+
}
275+
276+
public function statementArrayProvider() {
277+
return array(
278+
array(
279+
array(
280+
$this->getStatementWithSnak( 1, 'foo' ),
281+
$this->getStatementWithSnak( 2, 'bar' ),
282+
)
283+
),
284+
285+
array(
286+
array(
287+
$this->getStatementWithSnak( 1, 'foo' ),
288+
)
289+
),
290+
291+
array(
292+
array()
293+
),
294+
);
295+
}
296+
297+
public function testGivenDifferentLists_equalsReturnsFalse() {
298+
$firstStatements = new StatementList( array(
299+
$this->getStatementWithSnak( 1, 'foo' ),
300+
$this->getStatementWithSnak( 2, 'bar' ),
301+
) );
302+
303+
$secondStatements = new StatementList( array(
304+
$this->getStatementWithSnak( 1, 'foo' ),
305+
$this->getStatementWithSnak( 2, 'SPAM' ),
306+
) );
307+
308+
$this->assertFalse( $firstStatements->equals( $secondStatements ) );
309+
}
310+
311+
public function testGivenListsWithDifferentDuplicates_equalsReturnsFalse() {
312+
$firstStatements = new StatementList( array(
313+
$this->getStatementWithSnak( 1, 'foo' ),
314+
$this->getStatementWithSnak( 1, 'foo' ),
315+
$this->getStatementWithSnak( 2, 'bar' ),
316+
) );
317+
318+
$secondStatements = new StatementList( array(
319+
$this->getStatementWithSnak( 1, 'foo' ),
320+
$this->getStatementWithSnak( 2, 'bar' ),
321+
$this->getStatementWithSnak( 2, 'bar' ),
322+
) );
323+
324+
$this->assertFalse( $firstStatements->equals( $secondStatements ) );
325+
}
326+
327+
public function testGivenListsWithDifferentOrder_equalsReturnsFalse() {
328+
$firstStatements = new StatementList( array(
329+
$this->getStatementWithSnak( 1, 'foo' ),
330+
$this->getStatementWithSnak( 2, 'bar' ),
331+
$this->getStatementWithSnak( 3, 'baz' ),
332+
) );
333+
334+
$secondStatements = new StatementList( array(
335+
$this->getStatementWithSnak( 1, 'foo' ),
336+
$this->getStatementWithSnak( 3, 'baz' ),
337+
$this->getStatementWithSnak( 2, 'bar' ),
338+
) );
339+
340+
$this->assertFalse( $firstStatements->equals( $secondStatements ) );
341+
}
342+
251343
}

0 commit comments

Comments
 (0)