1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Lookup; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
7
|
|
|
use Wikibase\DataModel\Entity\Item; |
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
9
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookup; |
10
|
|
|
use Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup; |
11
|
|
|
use Wikibase\DataModel\Services\Lookup\UnresolvedEntityRedirectException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
* @author Daniel Kinzler |
18
|
|
|
*/ |
19
|
|
|
class RedirectResolvingEntityLookupTest extends TestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param EntityId $id |
23
|
|
|
* |
24
|
|
|
* @return null|Item |
25
|
|
|
* @throws UnresolvedEntityRedirectException |
26
|
|
|
*/ |
27
|
|
|
public function getEntity( EntityId $id ) { |
28
|
|
|
switch ( $id->getSerialization() ) { |
29
|
|
|
case 'Q10': |
30
|
|
|
return new Item( $id ); |
|
|
|
|
31
|
|
|
case 'Q11': |
32
|
|
|
throw new UnresolvedEntityRedirectException( new ItemId( 'Q11' ), new ItemId( 'Q10' ) ); |
33
|
|
|
case 'Q12': |
34
|
|
|
throw new UnresolvedEntityRedirectException( new ItemId( 'Q12' ), new ItemId( 'Q11' ) ); |
35
|
|
|
case 'Q21': |
36
|
|
|
throw new UnresolvedEntityRedirectException( new ItemId( 'Q21' ), new ItemId( 'Q20' ) ); |
37
|
|
|
default: |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return EntityLookup |
44
|
|
|
*/ |
45
|
|
|
public function getLookupDouble() { |
46
|
|
|
$mock = $this->createMock( EntityLookup::class ); |
47
|
|
|
|
48
|
|
|
$mock->expects( $this->any() ) |
49
|
|
|
->method( 'getEntity' ) |
50
|
|
|
->will( $this->returnCallback( [ $this, 'getEntity' ] ) ); |
51
|
|
|
|
52
|
|
|
$mock->expects( $this->any() ) |
53
|
|
|
->method( 'hasEntity' ) |
54
|
|
|
->will( $this->returnCallback( function ( EntityId $id ) { |
55
|
|
|
return $this->getEntity( $id ) !== null; |
56
|
|
|
} ) ); |
57
|
|
|
|
58
|
|
|
return $mock; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getEntityProvider() { |
62
|
|
|
return [ |
63
|
|
|
'no redirect' => [ new ItemId( 'Q10' ), new ItemId( 'Q10' ) ], |
64
|
|
|
'one redirect' => [ new ItemId( 'Q11' ), new ItemId( 'Q10' ) ], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider getEntityProvider |
70
|
|
|
*/ |
71
|
|
|
public function testGetEntity( EntityId $id, EntityId $expected ) { |
72
|
|
|
$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() ); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$entity = $lookup->getEntity( $id ); |
75
|
|
|
|
76
|
|
|
if ( $expected === null ) { |
77
|
|
|
$this->assertNull( $entity ); |
78
|
|
|
} else { |
79
|
|
|
$this->assertTrue( $expected->equals( $entity->getId() ) ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetEntity_missing() { |
84
|
|
|
$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() ); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$id = new ItemId( 'Q7' ); // entity Q7 is not known |
87
|
|
|
$this->assertNull( $lookup->getEntity( $id ) ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testGetEntity_brokenRedirect() { |
91
|
|
|
$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() ); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$id = new ItemId( 'Q20' ); // Q20 is a broken redirect |
94
|
|
|
$this->assertNull( $lookup->getEntity( $id ) ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetEntity_doubleRedirect() { |
98
|
|
|
$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() ); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$id = new ItemId( 'Q12' ); // Q12 is a double redirect |
101
|
|
|
|
102
|
|
|
$this->expectException( UnresolvedEntityRedirectException::class ); |
103
|
|
|
$lookup->getEntity( $id ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function hasEntityProvider() { |
107
|
|
|
return [ |
108
|
|
|
'unknown entity' => [ new ItemId( 'Q7' ), false ], |
109
|
|
|
'no redirect' => [ new ItemId( 'Q10' ), true ], |
110
|
|
|
'one redirect' => [ new ItemId( 'Q11' ), true ], |
111
|
|
|
'broken redirect' => [ new ItemId( 'Q21' ), false ], |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @dataProvider hasEntityProvider |
117
|
|
|
*/ |
118
|
|
|
public function testHasEntity( EntityId $id, $exists ) { |
119
|
|
|
$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() ); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->assertEquals( $exists, $lookup->hasEntity( $id ) ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
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: