Skip to content

Commit

Permalink
Merge branch 'master' into InMemoryEntityLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Apr 16, 2019
2 parents 660db6d + 09a0ec5 commit bd7d8e2
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .scrutinizer.yml
@@ -1,4 +1,7 @@
build: true
build:
environment:
php: 7.2

inherit: true

before_commands:
Expand Down
5 changes: 2 additions & 3 deletions .travis.yml
Expand Up @@ -5,8 +5,6 @@ matrix:
include:
- env: DM=~6.3
php: hhvm
- env: DM=~7.5
php: 5.6
- env: DM=~7.5
php: 7
- env: DM=~8.0
Expand All @@ -20,7 +18,8 @@ sudo: false

install: travis_retry composer require "wikibase/data-model=$DM" --prefer-source

script: composer ci
# Hack: Don't run phpmd with HHVM
script: if $(php --version | grep -q HipHop); then composer test; else composer ci; fi

notifications:
irc:
Expand Down
7 changes: 6 additions & 1 deletion RELEASE-NOTES.md
@@ -1,9 +1,14 @@
# Wikibase DataModel Services release notes

## Version 3.13.0 (dev)
## Version 3.14.0 (dev)

* Added vararg constructor to `InMemoryEntityLookup`

## Version 3.13.0 (2019-02-05)

* Added ExceptionIgnoringEntityLookup
* Bumped minimum PHP requirement to 7.x or HHVM

## Version 3.12.0 (2018-11-06)

* Added compatibility with Wikibase DataModel 9.x
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Expand Up @@ -24,13 +24,14 @@
"irc": "irc://irc.freenode.net/wikidata"
},
"require": {
"php": ">=5.6.0",
"php": ">=5.6.99",
"wikibase/data-model": "~9.0|~8.0|~7.0|~6.3",
"data-values/data-values": "~2.0|~1.0",
"diff/diff": "~2.3",
"wikimedia/assert": "~0.2.2"
"wikimedia/assert": "~0.2.2|~0.3.0|~0.4.0"
},
"require-dev": {
"phpmd/phpmd": "~2.3",
"phpunit/phpunit": "~5.7",
"wikibase/wikibase-codesniffer": "^0.5.0"
},
Expand All @@ -41,7 +42,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.12.x-dev"
"dev-master": "3.13.x-dev"
}
},
"scripts": {
Expand All @@ -50,7 +51,8 @@
"phpunit --coverage-php /dev/null"
],
"cs": [
"phpcs -p -s"
"phpcs -p -s",
"phpmd src/,tests/unit/ text phpmd.xml"
],
"ci": [
"@cs",
Expand Down
53 changes: 53 additions & 0 deletions src/Lookup/ExceptionIgnoringEntityLookup.php
@@ -0,0 +1,53 @@
<?php

namespace Wikibase\DataModel\Services\Lookup;

use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\EntityId;

/**
* An {@link EntityLookup} which ignores any exceptions
* which occur while retrieving an entity
* and instead pretends the entity does not exist.
*
* @license GPL-2.0-or-later
*/
class ExceptionIgnoringEntityLookup implements EntityLookup {

/**
* @var EntityLookup
*/
private $lookup;

public function __construct( EntityLookup $lookup ) {
$this->lookup = $lookup;
}

/**
* Attempt to retrieve the entity,
* returning `null` if any errors occur.
*
* @param EntityId $entityId
* @return EntityDocument|null
*/
public function getEntity( EntityId $entityId ) {
try {
return $this->lookup->getEntity( $entityId );
} catch ( EntityLookupException $exception ) {
return null;
}
}

/**
* Returns whether the given entity can potentially be looked up using {@link getEntity()}.
* Note that this does not guarantee {@link getEntity()} will return an {@link EntityDocument} –
* it may still return `null` if an error occurs retrieving the entity.
*
* @param EntityId $entityId
* @return bool
*/
public function hasEntity( EntityId $entityId ) {
return $this->lookup->hasEntity( $entityId );
}

}
88 changes: 88 additions & 0 deletions tests/unit/Lookup/ExceptionIgnoringEntityLookupTest.php
@@ -0,0 +1,88 @@
<?php

namespace Wikibase\DataModel\Services\Tests\Lookup;

use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Lookup\EntityLookup;
use Wikibase\DataModel\Services\Lookup\UnresolvedEntityRedirectException;
use Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup;

/**
* @covers Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup
*
* @license GPL-2.0-or-later
*/
class ExceptionIgnoringEntityLookupTest extends \PHPUnit_Framework_TestCase {

public function testGetEntity_returnsEntity() {
$entity = new Item( new ItemId( 'Q1' ) );
$entityId = $entity->getId();
$innerLookup = $this->createMock( EntityLookup::class );
$innerLookup->expects( $this->once() )
->method( 'getEntity' )
->with( $entityId )
->willReturn( $entity );
$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );

$actual = $outerLookup->getEntity( $entityId );

$this->assertSame( $entity, $actual );
}

public function testGetEntity_returnsNull() {
$entityId = new ItemId( 'Q999999999' );
$innerLookup = $this->createMock( EntityLookup::class );
$innerLookup->expects( $this->once() )
->method( 'getEntity' )
->with( $entityId )
->willReturn( null );
$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );

$actual = $outerLookup->getEntity( $entityId );

$this->assertNull( $actual );
}

public function testGetEntity_catchesUnresolvedEntityRedirectException() {
$entityId = new ItemId( 'Q2' );
$innerLookup = $this->createMock( EntityLookup::class );
$innerLookup->expects( $this->once() )
->method( 'getEntity' )
->with( $entityId )
->willThrowException( new UnresolvedEntityRedirectException(
$entityId,
new ItemId( 'Q1' )
) );
$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );

$actual = $outerLookup->getEntity( $entityId );

$this->assertNull( $actual );
}

/**
* @dataProvider provideBooleans
*/
public function testHasEntity( $expected ) {
$entityId = new ItemId( 'Q1' );
$innerLookup = $this->createMock( EntityLookup::class );
$innerLookup->expects( $this->once() )
->method( 'hasEntity' )
->with( $entityId )
->willReturn( $expected );
$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );

$actual = $outerLookup->hasEntity( $entityId );

$this->assertSame( $expected, $actual );
}

public function provideBooleans() {
return [
[ true ],
[ false ],
];
}

}

0 comments on commit bd7d8e2

Please sign in to comment.