Passed
Push — master ( 4ffe17...21fc8e )
by Jeroen De
02:43 queued 10s
created

testGivenEntityWithoutIdInConstructor_exceptionIsThrown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
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\Services\Fixtures\FakeEntityDocument;
8
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
9
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
10
11
/**
12
 * @covers \Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class InMemoryEntityLookupTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testGivenUnknownEntityId_getEntityReturnsNull() {
20
		$lookup = new InMemoryEntityLookup();
21
		$this->assertNull( $lookup->getEntity( new ItemId( 'Q1' ) ) );
22
	}
23
24
	public function testGivenKnownEntityId_getEntityReturnsTheEntity() {
25
		$lookup = new InMemoryEntityLookup();
26
27
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
28
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
29
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
30
31
		$this->assertEquals(
32
			new FakeEntityDocument( new ItemId( 'Q2' ) ),
33
			$lookup->getEntity( new ItemId( 'Q2' ) )
34
		);
35
	}
36
37
	public function testGivenUnknownEntityId_hasEntityReturnsFalse() {
38
		$lookup = new InMemoryEntityLookup();
39
		$this->assertFalse( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
40
	}
41
42
	public function testGivenKnownEntityId_hasEntityReturnsTrue() {
43
		$lookup = new InMemoryEntityLookup();
44
45
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
46
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) );
47
		$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) );
48
49
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
50
	}
51
52
	public function testGivenIdInExceptionList_getEntityThrowsException() {
53
		$lookup = new InMemoryEntityLookup();
54
55
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
56
57
		$lookup->getEntity( new ItemId( 'Q2' ) );
58
		$this->expectException( EntityLookupException::class );
59
		$lookup->getEntity( new ItemId( 'Q1' ) );
60
	}
61
62
	public function testGivenIdInExceptionList_hasEntityThrowsException() {
63
		$lookup = new InMemoryEntityLookup();
64
65
		$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) );
66
67
		$lookup->hasEntity( new ItemId( 'Q2' ) );
68
		$this->expectException( EntityLookupException::class );
69
		$lookup->hasEntity( new ItemId( 'Q1' ) );
70
	}
71
72
	public function testGivenConstructorVarArgEntities_theyCanBeRetrieved() {
73
		$lookup = new InMemoryEntityLookup(
74
			new FakeEntityDocument( new ItemId( 'Q1' ) ),
75
			new FakeEntityDocument( new ItemId( 'Q2' ) )
76
		);
77
78
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q1' ) ) );
79
		$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) );
80
	}
81
82
	public function testGivenEntityWithoutIdInConstructor_exceptionIsThrown() {
83
		$this->expectException( InvalidArgumentException::class );
84
85
		new InMemoryEntityLookup(
86
			new FakeEntityDocument()
87
		);
88
	}
89
90
}
91