Skip to content

Commit

Permalink
Add vararg constructor to InMemoryEntityLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Dec 3, 2018
1 parent d3599a1 commit ff7819a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
@@ -1,5 +1,9 @@
# Wikibase DataModel Services release notes

## Version 3.13.0 (dev)

* Added vararg constructor to `InMemoryEntityLookup`

## Version 3.12.0 (2018-11-06)

* Added compatibility with Wikibase DataModel 9.x
Expand Down
9 changes: 9 additions & 0 deletions src/Lookup/InMemoryEntityLookup.php
Expand Up @@ -30,6 +30,15 @@ class InMemoryEntityLookup implements EntityLookup {
*/
private $exceptions = [];

/**
* @param EntityDocument ...$entities
*/
public function __construct( ...$entities ) {
foreach ( $entities as $entity ) {
$this->addEntity( $entity );
}
}

/**
* @param EntityDocument $entity
*
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/FakeEntityDocument.php
Expand Up @@ -12,16 +12,16 @@
class FakeEntityDocument implements EntityDocument {

/**
* @var EntityId
* @var EntityId|null
*/
private $id;

public function __construct( EntityId $id ) {
public function __construct( EntityId $id = null ) {
$this->id = $id;
}

/**
* @return EntityId
* @return EntityId|null
*/
public function getId() {
return $this->id;
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/Lookup/InMemoryEntityLookupTest.php
Expand Up @@ -2,13 +2,14 @@

namespace Wikibase\DataModel\Services\Tests\Lookup;

use InvalidArgumentException;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument;
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;

/**
* @covers Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup
* @covers \Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testGivenIdInExceptionList_getEntityThrowsException() {
$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );

$lookup->getEntity( new ItemId( 'Q2' ) );
$this->setExpectedException( EntityLookupException::class );
$this->expectException( EntityLookupException::class );
$lookup->getEntity( new ItemId( 'Q1' ) );
}

Expand All @@ -64,8 +65,26 @@ public function testGivenIdInExceptionList_hasEntityThrowsException() {
$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );

$lookup->hasEntity( new ItemId( 'Q2' ) );
$this->setExpectedException( EntityLookupException::class );
$this->expectException( EntityLookupException::class );
$lookup->hasEntity( new ItemId( 'Q1' ) );
}

public function testGivenConstructorVarArgEntities_theyCanBeRetrieved() {
$lookup = new InMemoryEntityLookup(
new FakeEntityDocument( new ItemId( 'Q1' ) ),
new FakeEntityDocument( new ItemId( 'Q2' ) )
);

$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
}

public function testGivenEntityWithoutIdInConstructor_exceptionIsThrown() {
$this->expectException( InvalidArgumentException::class );

new InMemoryEntityLookup(
new FakeEntityDocument()
);
}

}

0 comments on commit ff7819a

Please sign in to comment.