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

testGivenKnownProperty_getPropertyForIdReturnsIt()   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\PropertyId;
7
use Wikibase\DataModel\Services\Fixtures\PropertyFixtures;
8
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
9
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
10
use Wikibase\DataModel\Services\Lookup\LegacyAdapterPropertyLookup;
11
use Wikibase\DataModel\Services\Lookup\PropertyLookupException;
12
13
/**
14
 * @covers \Wikibase\DataModel\Services\Lookup\LegacyAdapterPropertyLookup
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class LegacyAdapterPropertyLookupTest extends TestCase {
20
21
	public function testGivenKnownProperty_getPropertyForIdReturnsIt() {
22
		$property = PropertyFixtures::newProperty();
23
24
		$lookup = new LegacyAdapterPropertyLookup( new InMemoryEntityLookup( $property ) );
25
26
		$this->assertEquals(
27
			$property,
28
			$lookup->getPropertyForId( $property->getId() )
0 ignored issues
show
Bug introduced by
It seems like $property->getId() can be null; however, getPropertyForId() 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 testWhenPropertyIsNotKnown_getPropertyForIdReturnsNull() {
33
		$lookup = new LegacyAdapterPropertyLookup( new InMemoryEntityLookup() );
34
35
		$this->assertNull(
36
			$lookup->getPropertyForId( new PropertyId( 'P1' ) )
37
		);
38
	}
39
40
	public function testGetPropertyForIdThrowsCorrectExceptionType() {
41
		$id = new PropertyId( 'P1' );
42
43
		$legacyLookup = new InMemoryEntityLookup();
44
		$legacyLookup->addException( new EntityLookupException( $id ) );
45
46
		$propertyLookup = new LegacyAdapterPropertyLookup( $legacyLookup );
47
48
		$this->expectException( PropertyLookupException::class );
49
		$propertyLookup->getPropertyForId( $id );
50
	}
51
52
}
53