Completed
Push — master ( da4cd6...5d314e )
by Daniel
32s
created

testLookupExceptionsAreNotCaughtInHasEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Lookup;
4
5
use Exception;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument;
9
use Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup;
10
use Wikibase\DataModel\Services\Lookup\EntityLookup;
11
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
12
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
13
use Wikibase\DataModel\Services\Lookup\UnknownForeignRepositoryException;
14
use Wikimedia\Assert\ParameterAssertionException;
15
16
/**
17
 * @covers Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup
18
 *
19
 * @license GPL-2.0+
20
 */
21
class DispatchingEntityLookupTest extends \PHPUnit_Framework_TestCase {
22
23
	/**
24
	 * @dataProvider provideInvalidForeignLookups
25
	 */
26
	public function testGivenInvalidForeignLookups_exceptionIsThrown( array $lookups ) {
27
		$this->setExpectedException( 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
	/**
74
	 * @param Exception $exception
75
	 * @return \PHPUnit_Framework_MockObject_MockObject|EntityLookup
76
	 */
77
	private function getExceptionThrowingLookup( Exception $exception ) {
78
		$lookup = $this->getMock( EntityLookup::class );
79
		$lookup->expects( $this->any() )
80
			->method( $this->anything() )
81
			->willThrowException( $exception );
82
		return $lookup;
83
	}
84
85
	public function testLookupExceptionsAreNotCaughtInGetEntity() {
86
		$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) );
87
88
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] );
0 ignored issues
show
Documentation introduced by
array('' => $lookup) is of type array<string,object<PHPU...ookup\\EntityLookup>"}>, 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...
89
90
		$this->setExpectedException( EntityLookupException::class );
91
		$dispatchingLookup->getEntity( new ItemId( 'Q321' ) );
92
	}
93
94
	public function testGivenEntityIdFromUnknownRepository_getEntityThrowsException() {
95
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $this->getMock( EntityLookup::class ), ] );
96
97
		$this->setExpectedException( UnknownForeignRepositoryException::class );
98
99
		$dispatchingLookup->getEntity( new ItemId( 'foo:Q1' ) );
100
	}
101
102
	public function testGivenExistingEntityId_hasEntityReturnsTrue() {
103
		$localLookup = new InMemoryEntityLookup();
104
		$localLookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) );
105
		$fooLookup = new InMemoryEntityLookup();
106
		$fooLookup->addEntity( new FakeEntityDocument( new PropertyId( 'foo:P11' ) ) );
107
108
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
109
110
		$this->assertTrue( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) );
111
		$this->assertTrue( $dispatchingLookup->hasEntity( new PropertyId( 'foo:P11' ) ) );
112
	}
113
114
	public function testGivenNotExistingEntityIdFromKnownRepository_getEntityReturnsFalse() {
115
		$localLookup = new InMemoryEntityLookup();
116
		$fooLookup = new InMemoryEntityLookup();
117
118
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] );
119
120
		$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) );
121
		$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'foo:Q19' ) ) );
122
	}
123
124
	public function testLookupExceptionsAreNotCaughtInHasEntity() {
125
		$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) );
126
127
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] );
0 ignored issues
show
Documentation introduced by
array('' => $lookup) is of type array<string,object<PHPU...ookup\\EntityLookup>"}>, 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...
128
129
		$this->setExpectedException( EntityLookupException::class );
130
		$dispatchingLookup->hasEntity( new ItemId( 'Q321' ) );
131
	}
132
133
	public function testGivenEntityIdFromUnknownRepository_hasEntityThrowsException() {
134
		$dispatchingLookup = new DispatchingEntityLookup( [ '' => $this->getMock( EntityLookup::class ), ] );
135
136
		$this->setExpectedException( UnknownForeignRepositoryException::class );
137
138
		$dispatchingLookup->hasEntity( new ItemId( 'foo:Q1' ) );
139
	}
140
141
}
142