Test Failed
Pull Request — master (#221)
by Amir
02:28
created

testGetEntity_doubleRedirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
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 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 );
0 ignored issues
show
Documentation introduced by
$id is of type object<Wikibase\DataModel\Entity\EntityId>, but the function expects a null|object<Wikibase\DataModel\Entity\ItemId>.

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...
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
			'double redirect' => [ new ItemId( 'Q12' ), new ItemId( 'Q10' ) ],
66
		];
67
	}
68
69
	/**
70
	 * @dataProvider getEntityProvider
71
	 */
72
	public function testGetEntity( EntityId $id, EntityId $expected ) {
73
		$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() );
74
75
		$entity = $lookup->getEntity( $id );
76
77
		if ( $expected === null ) {
78
			$this->assertNull( $entity );
79
		} else {
80
			$this->assertTrue( $expected->equals( $entity->getId() ) );
81
		}
82
	}
83
84
	public function testGetEntity_missing() {
85
		$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() );
86
87
		$id = new ItemId( 'Q7' ); // entity Q7 is not known
88
		$this->assertNull( $lookup->getEntity( $id ) );
89
	}
90
91
	public function testGetEntity_brokenRedirect() {
92
		$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() );
93
94
		$id = new ItemId( 'Q20' ); // Q20 is a broken redirect
95
		$this->assertNull( $lookup->getEntity( $id ) );
96
	}
97
98
	public function hasEntityProvider() {
99
		return [
100
			'unknown entity' => [ new ItemId( 'Q7' ), false ],
101
			'no redirect' => [ new ItemId( 'Q10' ), true ],
102
			'one redirect' => [ new ItemId( 'Q11' ), true ],
103
			'double redirect' => [ new ItemId( 'Q12' ), true ],
104
			'broken redirect' => [ new ItemId( 'Q21' ), false ],
105
		];
106
	}
107
108
	/**
109
	 * @dataProvider hasEntityProvider
110
	 */
111
	public function testHasEntity( EntityId $id, $exists ) {
112
		$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() );
113
114
		$this->assertEquals( $exists, $lookup->hasEntity( $id ) );
115
	}
116
117
}
118