Passed
Push — master ( 35164b...6a3f49 )
by Jeroen De
49s
created

testGetEntity_returnsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Lookup;
4
5
use Wikibase\DataModel\Entity\Item;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Services\Lookup\EntityLookup;
8
use Wikibase\DataModel\Services\Lookup\UnresolvedEntityRedirectException;
9
use Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup;
10
11
/**
12
 * @covers Wikibase\DataModel\Services\Lookup\ExceptionIgnoringEntityLookup
13
 *
14
 * @license GPL-2.0-or-later
15
 */
16
class ExceptionIgnoringEntityLookupTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testGetEntity_returnsEntity() {
19
		$entity = new Item( new ItemId( 'Q1' ) );
20
		$entityId = $entity->getId();
21
		$innerLookup = $this->createMock( EntityLookup::class );
22
		$innerLookup->expects( $this->once() )
23
			->method( 'getEntity' )
24
			->with( $entityId )
25
			->willReturn( $entity );
26
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
27
28
		$actual = $outerLookup->getEntity( $entityId );
0 ignored issues
show
Bug introduced by Lucas Werkmeister
It seems like $entityId defined by $entity->getId() on line 20 can also be of type null; however, Wikibase\DataModel\Servi...tityLookup::getEntity() does only seem to accept object<Wikibase\DataModel\Entity\EntityId>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
29
30
		$this->assertSame( $entity, $actual );
31
	}
32
33
	public function testGetEntity_returnsNull() {
34
		$entityId = new ItemId( 'Q999999999' );
35
		$innerLookup = $this->createMock( EntityLookup::class );
36
		$innerLookup->expects( $this->once() )
37
			->method( 'getEntity' )
38
			->with( $entityId )
39
			->willReturn( null );
40
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
41
42
		$actual = $outerLookup->getEntity( $entityId );
43
44
		$this->assertNull( $actual );
45
	}
46
47
	public function testGetEntity_catchesUnresolvedEntityRedirectException() {
48
		$entityId = new ItemId( 'Q2' );
49
		$innerLookup = $this->createMock( EntityLookup::class );
50
		$innerLookup->expects( $this->once() )
51
			->method( 'getEntity' )
52
			->with( $entityId )
53
			->willThrowException( new UnresolvedEntityRedirectException(
54
				$entityId,
55
				new ItemId( 'Q1' )
56
			) );
57
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
58
59
		$actual = $outerLookup->getEntity( $entityId );
60
61
		$this->assertNull( $actual );
62
	}
63
64
	/**
65
	 * @dataProvider provideBooleans
66
	 */
67
	public function testHasEntity( $expected ) {
68
		$entityId = new ItemId( 'Q1' );
69
		$innerLookup = $this->createMock( EntityLookup::class );
70
		$innerLookup->expects( $this->once() )
71
			->method( 'hasEntity' )
72
			->with( $entityId )
73
			->willReturn( $expected );
74
		$outerLookup = new ExceptionIgnoringEntityLookup( $innerLookup );
75
76
		$actual = $outerLookup->hasEntity( $entityId );
77
78
		$this->assertSame( $expected, $actual );
79
	}
80
81
	public function provideBooleans() {
82
		return [
83
			[ true ],
84
			[ false ],
85
		];
86
	}
87
88
}
89