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 InvalidArgumentException;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument;
9
use Wikibase\DataModel\Services\Fixtures\ItemFixtures;
10
use Wikibase\DataModel\Services\Fixtures\PropertyFixtures;
11
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
12
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
13
use Wikibase\DataModel\Services\Lookup\ItemLookupException;
14
use Wikibase\DataModel\Services\Lookup\PropertyLookupException;
15
16
/**
17
 * @covers \Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class InMemoryEntityLookupTest extends \PHPUnit_Framework_TestCase {
23
24
	public function testGivenUnknownEntityId_getEntityReturnsNull() {
25
		$lookup = new InMemoryEntityLookup();
26
		$this->assertNull( $lookup->getEntity( new ItemId( 'Q1' ) ) );
27
	}
28
29
	public function testGivenKnownEntityId_getEntityReturnsTheEntity() {
30
		$lookup = new InMemoryEntityLookup();
31
32
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
33
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
34
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
35
36
		$this->assertEquals(
37
			new FakeEntityDocument( new ItemId( 'Q2' ) ),
38
			$lookup->getEntity( new ItemId( 'Q2' ) )
39
		);
40
	}
41
42
	public function testGivenUnknownEntityId_hasEntityReturnsFalse() {
43
		$lookup = new InMemoryEntityLookup();
44
		$this->assertFalse( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
45
	}
46
47
	public function testGivenKnownEntityId_hasEntityReturnsTrue() {
48
		$lookup = new InMemoryEntityLookup();
49
50
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
51
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
52
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
53
54
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
55
	}
56
57
	public function testGivenIdInExceptionList_getEntityThrowsException() {
58
		$lookup = new InMemoryEntityLookup();
59
60
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
61
62
		$lookup->getEntity( new ItemId( 'Q2' ) );
63
		$this->expectException( EntityLookupException::class );
64
		$lookup->getEntity( new ItemId( 'Q1' ) );
65
	}
66
67
	public function testGivenIdInExceptionList_hasEntityThrowsException() {
68
		$lookup = new InMemoryEntityLookup();
69
70
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
71
72
		$lookup->hasEntity( new ItemId( 'Q2' ) );
73
		$this->expectException( EntityLookupException::class );
74
		$lookup->hasEntity( new ItemId( 'Q1' ) );
75
	}
76
77
	public function testGivenConstructorVarArgEntities_theyCanBeRetrieved() {
78
		$lookup = new InMemoryEntityLookup(
79
			new FakeEntityDocument( new ItemId( 'Q1' ) ),
80
			new FakeEntityDocument( new ItemId( 'Q2' ) )
81
		);
82
83
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
84
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
85
	}
86
87
	public function testGivenEntityWithoutIdInConstructor_exceptionIsThrown() {
88
		$this->expectException( InvalidArgumentException::class );
89
90
		new InMemoryEntityLookup(
91
			new FakeEntityDocument()
92
		);
93
	}
94
95
	public function testGivenKnownItem_getItemForIdReturnsIt() {
96
		$item = ItemFixtures::newItem();
97
98
		$lookup = new InMemoryEntityLookup( $item );
99
100
		$this->assertEquals(
101
			$item,
102
			$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...
103
		);
104
	}
105
106
	public function testGivenKnownProperty_getPropertyForIdReturnsIt() {
107
		$property = PropertyFixtures::newProperty();
108
109
		$lookup = new InMemoryEntityLookup( $property );
110
111
		$this->assertEquals(
112
			$property,
113
			$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...
114
		);
115
	}
116
117
	public function testWhenItemIsNotKnown_getItemForIdReturnsNull() {
118
		$this->assertNull(
119
			( new InMemoryEntityLookup() )->getItemForId( new ItemId( 'Q1' ) )
120
		);
121
	}
122
123
	public function testWhenPropertyIsNotKnown_getPropertyForIdReturnsNull() {
124
		$this->assertNull(
125
			( new InMemoryEntityLookup() )->getPropertyForId( new PropertyId( 'P1' ) )
126
		);
127
	}
128
129
	public function testGetItemForIdThrowsCorrectExceptionType() {
130
		$id = new ItemId( 'Q1' );
131
132
		$lookup = new InMemoryEntityLookup();
133
		$lookup->addException( new EntityLookupException( $id ) );
134
135
		$this->expectException( ItemLookupException::class );
136
		$lookup->getItemForId( $id );
137
	}
138
139
	public function testGetPropertyForIdThrowsCorrectExceptionType() {
140
		$id = new PropertyId( 'P1' );
141
142
		$lookup = new InMemoryEntityLookup();
143
		$lookup->addException( new EntityLookupException( $id ) );
144
145
		$this->expectException( PropertyLookupException::class );
146
		$lookup->getPropertyForId( $id );
147
	}
148
149
}
150