Passed
Push — master ( cb35bf...01d583 )
by Marius
35s
created

testGivenKnownItem_getItemForIdReturnsIt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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 PHPUnit\Framework\TestCase;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Services\Fixtures\ItemFixtures;
8
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
9
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
10
use Wikibase\DataModel\Services\Lookup\ItemLookupException;
11
use Wikibase\DataModel\Services\Lookup\LegacyAdapterItemLookup;
12
13
/**
14
 * @covers \Wikibase\DataModel\Services\Lookup\LegacyAdapterItemLookup
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class LegacyAdapterItemLookupTest extends TestCase {
20
21
	public function testGivenKnownItem_getItemForIdReturnsIt() {
22
		$item = ItemFixtures::newItem();
23
24
		$lookup = new LegacyAdapterItemLookup( new InMemoryEntityLookup( $item ) );
25
26
		$this->assertEquals(
27
			$item,
28
			$lookup->getItemForId( $item->getId() )
0 ignored issues
show
Bug introduced by
It seems like $item->getId() can be null; however, getItemForId() 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...
29
		);
30
	}
31
32
	public function testWhenItemIsNotKnown_getItemForIdReturnsNull() {
33
		$lookup = new LegacyAdapterItemLookup( new InMemoryEntityLookup() );
34
35
		$this->assertNull(
36
			$lookup->getItemForId( new ItemId( 'Q1' ) )
37
		);
38
	}
39
40
	public function testGetItemForIdThrowsCorrectExceptionType() {
41
		$id = new ItemId( 'Q1' );
42
43
		$legacyLookup = new InMemoryEntityLookup();
44
		$legacyLookup->addException( new EntityLookupException( $id ) );
45
46
		$itemLookup = new LegacyAdapterItemLookup( $legacyLookup );
47
48
		$this->expectException( ItemLookupException::class );
49
		$itemLookup->getItemForId( $id );
50
	}
51
52
}
53