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 Wikimedia\Assert\ParameterAssertionException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup |
17
|
|
|
* |
18
|
|
|
* @license GPL-2.0+ |
19
|
|
|
*/ |
20
|
|
|
class DispatchingEntityLookupTest extends \PHPUnit_Framework_TestCase { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @dataProvider provideInvalidForeignLookups |
24
|
|
|
*/ |
25
|
|
|
public function testGivenInvalidForeignLookups_exceptionIsThrown( array $lookups ) { |
26
|
|
|
$this->setExpectedException( ParameterAssertionException::class ); |
27
|
|
|
new DispatchingEntityLookup( $lookups ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function provideInvalidForeignLookups() { |
31
|
|
|
return [ |
32
|
|
|
'no lookups given' => [ [] ], |
33
|
|
|
'not an implementation of EntityLookup given as a lookup' => [ |
34
|
|
|
[ '' => new ItemId( 'Q123' ) ], |
35
|
|
|
], |
36
|
|
|
'non-string keys' => [ |
37
|
|
|
[ '' => new InMemoryEntityLookup(), 100 => new InMemoryEntityLookup(), ], |
38
|
|
|
], |
39
|
|
|
'repo name containing colon' => [ |
40
|
|
|
[ '' => new InMemoryEntityLookup(), 'fo:oo' => new InMemoryEntityLookup(), ], |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGivenExistingEntityId_getEntityReturnsTheEntity() { |
46
|
|
|
$localLookup = new InMemoryEntityLookup(); |
47
|
|
|
$localLookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) ); |
48
|
|
|
$fooLookup = new InMemoryEntityLookup(); |
49
|
|
|
$fooLookup->addEntity( new FakeEntityDocument( new PropertyId( 'foo:P11' ) ) ); |
50
|
|
|
|
51
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] ); |
52
|
|
|
|
53
|
|
|
$expected = new FakeEntityDocument( new ItemId( 'Q1' ) ); |
54
|
|
|
$actual = $dispatchingLookup->getEntity( new ItemId( 'Q1' ) ); |
55
|
|
|
$this->assertTrue( $actual->equals( $expected ) ); |
56
|
|
|
$this->assertTrue( $actual->getId()->equals( new ItemId( 'Q1' ) ) ); |
57
|
|
|
|
58
|
|
|
$expected = new FakeEntityDocument( new PropertyId( 'foo:P11' ) ); |
59
|
|
|
$actual = $dispatchingLookup->getEntity( new PropertyId( 'foo:P11' ) ); |
60
|
|
|
$this->assertTrue( $actual->equals( $expected ) ); |
61
|
|
|
$this->assertTrue( $actual->getId()->equals( new PropertyId( 'foo:P11' ) ) ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGivenNotExistingEntityIdFromKnownRepository_getEntityReturnsNull() { |
65
|
|
|
$localLookup = new InMemoryEntityLookup(); |
66
|
|
|
$fooLookup = new InMemoryEntityLookup(); |
67
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] ); |
68
|
|
|
$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'Q1' ) ) ); |
69
|
|
|
$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'foo:Q19' ) ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGivenEntityIdFromUnknownRepository_getEntityReturnsNull() { |
73
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => new InMemoryEntityLookup(), ] ); |
74
|
|
|
|
75
|
|
|
$this->assertNull( $dispatchingLookup->getEntity( new ItemId( 'foo:Q1' ) ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Exception $exception |
80
|
|
|
* |
81
|
|
|
* @return EntityLookup |
82
|
|
|
*/ |
83
|
|
|
private function getExceptionThrowingLookup( Exception $exception ) { |
84
|
|
|
$lookup = $this->getMock( EntityLookup::class ); |
85
|
|
|
$lookup->expects( $this->any() ) |
86
|
|
|
->method( $this->anything() ) |
87
|
|
|
->will( $this->throwException( $exception ) ); |
88
|
|
|
return $lookup; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testLookupExceptionsAreNotCaughtInGetEntity() { |
92
|
|
|
$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) ); |
93
|
|
|
|
94
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] ); |
95
|
|
|
|
96
|
|
|
$this->setExpectedException( EntityLookupException::class ); |
97
|
|
|
$dispatchingLookup->getEntity( new ItemId( 'Q321' ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGivenExistingEntityId_hasEntityReturnsTrue() { |
101
|
|
|
$localLookup = new InMemoryEntityLookup(); |
102
|
|
|
$localLookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) ); |
103
|
|
|
$fooLookup = new InMemoryEntityLookup(); |
104
|
|
|
$fooLookup->addEntity( new FakeEntityDocument( new PropertyId( 'foo:P11' ) ) ); |
105
|
|
|
|
106
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] ); |
107
|
|
|
|
108
|
|
|
$this->assertTrue( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) ); |
109
|
|
|
$this->assertTrue( $dispatchingLookup->hasEntity( new PropertyId( 'foo:P11' ) ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGivenNotExistingEntityIdFromKnownRepository_hasEntityReturnsFalse() { |
113
|
|
|
$localLookup = new InMemoryEntityLookup(); |
114
|
|
|
$fooLookup = new InMemoryEntityLookup(); |
115
|
|
|
|
116
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $localLookup, 'foo' => $fooLookup ] ); |
117
|
|
|
|
118
|
|
|
$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'Q1' ) ) ); |
119
|
|
|
$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'foo:Q19' ) ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testGivenEntityIdFromUnknownRepository_hasEntityReturnsFalse() { |
123
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $this->getMock( EntityLookup::class ), ] ); |
124
|
|
|
|
125
|
|
|
$this->assertFalse( $dispatchingLookup->hasEntity( new ItemId( 'foo:Q1' ) ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testLookupExceptionsAreNotCaughtInHasEntity() { |
129
|
|
|
$lookup = $this->getExceptionThrowingLookup( new EntityLookupException( new ItemId( 'Q321' ) ) ); |
130
|
|
|
|
131
|
|
|
$dispatchingLookup = new DispatchingEntityLookup( [ '' => $lookup ] ); |
132
|
|
|
|
133
|
|
|
$this->setExpectedException( EntityLookupException::class ); |
134
|
|
|
$dispatchingLookup->hasEntity( new ItemId( 'Q321' ) ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|