ExceptionIgnoringEntityLookupTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetEntity_returnsEntity() 0 14 1
A testGetEntity_returnsNull() 0 13 1
A testGetEntity_catchesUnresolvedEntityRedirectException() 0 16 1
A testHasEntity() 0 13 1
A provideBooleans() 0 6 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Lookup;
4
5
use PHPUnit\Framework\TestCase;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Services\Lookup\EntityLookup;
9
use Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup;
10
use Wikibase\DataModel\Services\Lookup\UnresolvedEntityRedirectException;
11
12
/**
13
 * @covers \Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup
14
 *
15
 * @license GPL-2.0-or-later
16
 */
17
class ExceptionIgnoringEntityLookupTest extends TestCase {
18
19
	public function testGetEntity_returnsEntity() {
20
		$entity = new Item( new ItemId( 'Q1' ) );
21
		$entityId = $entity->getId();
22
		$innerLookup = $this->createMock( EntityLookup::class );
23
		$innerLookup->expects( $this->once() )
24
			->method( 'getEntity' )
25
			->with( $entityId )
26
			->willReturn( $entity );
27
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
0 ignored issues
show
Documentation introduced by Lucas Werkmeister
$innerLookup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\Lookup\EntityLookup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
29
		$actual = $outerLookup->getEntity( $entityId );
0 ignored issues
show
Bug introduced by Lucas Werkmeister
It seems like $entityId defined by $entity->getId() on line 21 can be null; however, Wikibase\DataModel\Servi...tityLookup::getEntity() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
31
		$this->assertSame( $entity, $actual );
32
	}
33
34
	public function testGetEntity_returnsNull() {
35
		$entityId = new ItemId( 'Q999999999' );
36
		$innerLookup = $this->createMock( EntityLookup::class );
37
		$innerLookup->expects( $this->once() )
38
			->method( 'getEntity' )
39
			->with( $entityId )
40
			->willReturn( null );
41
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
0 ignored issues
show
Documentation introduced by Lucas Werkmeister
$innerLookup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\Lookup\EntityLookup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
43
		$actual = $outerLookup->getEntity( $entityId );
44
45
		$this->assertNull( $actual );
46
	}
47
48
	public function testGetEntity_catchesUnresolvedEntityRedirectException() {
49
		$entityId = new ItemId( 'Q2' );
50
		$innerLookup = $this->createMock( EntityLookup::class );
51
		$innerLookup->expects( $this->once() )
52
			->method( 'getEntity' )
53
			->with( $entityId )
54
			->willThrowException( new UnresolvedEntityRedirectException(
55
				$entityId,
56
				new ItemId( 'Q1' )
57
			) );
58
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
0 ignored issues
show
Documentation introduced by Lucas Werkmeister
$innerLookup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\Lookup\EntityLookup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
		$actual = $outerLookup->getEntity( $entityId );
61
62
		$this->assertNull( $actual );
63
	}
64
65
	/**
66
	 * @dataProvider provideBooleans
67
	 */
68
	public function testHasEntity( $expected ) {
69
		$entityId = new ItemId( 'Q1' );
70
		$innerLookup = $this->createMock( EntityLookup::class );
71
		$innerLookup->expects( $this->once() )
72
			->method( 'hasEntity' )
73
			->with( $entityId )
74
			->willReturn( $expected );
75
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
0 ignored issues
show
Documentation introduced by Lucas Werkmeister
$innerLookup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\Lookup\EntityLookup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77
		$actual = $outerLookup->hasEntity( $entityId );
78
79
		$this->assertSame( $expected, $actual );
80
	}
81
82
	public function provideBooleans() {
83
		return [
84
			[ true ],
85
			[ false ],
86
		];
87
	}
88
89
}
90