testGivenNotExistingEntityIdFromKnownRepository_hasEntityReturnsFalse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
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 Exception;
6
use PHPUnit\Framework\TestCase;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument;
10
use Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup;
11
use Wikibase\DataModel\Services\Lookup\EntityLookup;
12
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
13
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
14
use Wikimedia\Assert\ParameterAssertionException;
15
16
/**
17
 * @covers \Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup
18
 *
19
 * @license GPL-2.0-or-later
20
 */
21
class DispatchingEntityLookupTest extends TestCase {
22
23
	/**
24
	 * @dataProvider provideInvalidForeignLookups
25
	 */
26
	public function testGivenInvalidForeignLookups_exceptionIsThrown( array $lookups ) {
27
		$this->expectException( ParameterAssertionException::class );
28
		new DispatchingEntityLookup( $lookups );
29
	}
30
31
	public function provideInvalidForeignLookups() {
32
		return [
33
			'no lookups given' => [ [] ],
34
			'not an implementation of EntityLookup given as a lookup' => [
35
				[ '' => new ItemId( 'Q123' ) ],
36
			],
37
			'non-string keys' => [
38
				[ '' => new InMemoryEntityLookup(), 100 => new InMemoryEntityLookup(), ],
39
			],
40
			'repo name containing colon' => [
41
				[ '' => new InMemoryEntityLookup(),	'fo:oo' => new InMemoryEntityLookup(), ],
42
			],
43
		];
44
	}
45
46
	public function testGivenExistingEntityId_getEntityReturnsTheEntity() {
47
		$localLookup = new InMemoryEntityLookup();
48
		$localLookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
49
		$fooLookup = new InMemoryEntityLookup();
50
		$fooLookup->addEntity( new FakeEntityDocument( new PropertyId( 'foo:P11' ) ) );
51
52
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
53
54
		$expected = new FakeEntityDocument( new ItemId( 'Q1' ) );
55
		$actual = $dispatchingLookup->getEntity( new ItemId( 'Q1' ) );
56
		$this->assertTrue( $actual->equals( $expected ) );
57
		$this->assertTrue( $actual->getId()->equals( new ItemId( 'Q1' ) ) );
58
59
		$expected = new FakeEntityDocument( new PropertyId( 'foo:P11' ) );
60
		$actual = $dispatchingLookup->getEntity( new PropertyId( 'foo:P11' ) );
61
		$this->assertTrue( $actual->equals( $expected ) );
62
		$this->assertTrue( $actual->getId()->equals( new PropertyId( 'foo:P11' ) ) );
63
	}
64
65
	public function testGivenNotExistingEntityIdFromKnownRepository_getEntityReturnsNull() {
66
		$localLookup = new InMemoryEntityLookup();
67
		$fooLookup = new InMemoryEntityLookup();
68
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
69
		$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'Q1' ) ) );
70
		$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'foo:Q19' ) ) );
71
	}
72
73
	public function testGivenEntityIdFromUnknownRepository_getEntityReturnsNull() {
74
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => new InMemoryEntityLookup(), ] );
75
76
		$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'foo:Q1' ) ) );
77
	}
78
79
	/**
80
	 * @param Exception $exception
81
	 *
82
	 * @return EntityLookup
83
	 */
84
	private function getExceptionThrowingLookup( Exception $exception ) {
85
		$lookup = $this->createMock( EntityLookup::class );
86
		$lookup->expects( $this->any() )
87
			->method( $this->anything() )
88
			->will( $this->throwException( $exception ) );
89
		return $lookup;
90
	}
91
92
	public function testLookupExceptionsAreNotCaughtInGetEntity() {
93
		$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) );
94
95
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] );
0 ignored issues
show
Documentation introduced by
array('' => $lookup) is of type array<string,object<PHPU...kObject\\MockObject>"}>, but the function expects a array<integer,object<Wik...s\Lookup\EntityLookup>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
		$this->expectException( EntityLookupException::class );
98
		$dispatchingLookup->getEntity( new ItemId( 'Q321' ) );
99
	}
100
101
	public function testGivenExistingEntityId_hasEntityReturnsTrue() {
102
		$localLookup = new InMemoryEntityLookup();
103
		$localLookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
104
		$fooLookup = new InMemoryEntityLookup();
105
		$fooLookup->addEntity( new FakeEntityDocument( new PropertyId( 'foo:P11' ) ) );
106
107
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
108
109
		$this->assertTrue( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) );
110
		$this->assertTrue( $dispatchingLookup->hasEntity( new PropertyId( 'foo:P11' ) ) );
111
	}
112
113
	public function testGivenNotExistingEntityIdFromKnownRepository_hasEntityReturnsFalse() {
114
		$localLookup = new InMemoryEntityLookup();
115
		$fooLookup = new InMemoryEntityLookup();
116
117
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
118
119
		$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) );
120
		$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'foo:Q19' ) ) );
121
	}
122
123
	public function testGivenEntityIdFromUnknownRepository_hasEntityReturnsFalse() {
124
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $this->createMock( EntityLookup::class ), ] );
0 ignored issues
show
Documentation introduced by
array('' => $this->creat...p\EntityLookup::class)) is of type array<string,object<PHPU...kObject\\MockObject>"}>, but the function expects a array<integer,object<Wik...s\Lookup\EntityLookup>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
126
		$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'foo:Q1' ) ) );
127
	}
128
129
	public function testLookupExceptionsAreNotCaughtInHasEntity() {
130
		$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) );
131
132
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] );
0 ignored issues
show
Documentation introduced by
array('' => $lookup) is of type array<string,object<PHPU...kObject\\MockObject>"}>, but the function expects a array<integer,object<Wik...s\Lookup\EntityLookup>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
134
		$this->expectException( EntityLookupException::class );
135
		$dispatchingLookup->hasEntity( new ItemId( 'Q321' ) );
136
	}
137
138
}
139