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