RedirectResolvingEntityLookupTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 106
c 0
b 0
f 0
wmc 14
lcom 1
cbo 8
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntity() 0 14 5
A getLookupDouble() 0 15 1
A getEntityProvider() 0 6 1
A testGetEntity() 0 11 2
A testGetEntity_missing() 0 6 1
A testGetEntity_brokenRedirect() 0 6 1
A testGetEntity_doubleRedirect() 0 8 1
A hasEntityProvider() 0 8 1
A testHasEntity() 0 5 1
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
		];
66
	}
67
68
	/**
69
	 * @dataProvider getEntityProvider
70
	 */
71
	public function testGetEntity( EntityId $id, EntityId $expected ) {
72
		$lookup = new RedirectResolvingEntityLookup( $this->getLookupDouble() );
0 ignored issues
show
Documentation introduced by
$this->getLookupDouble() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\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...
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() );
0 ignored issues
show
Documentation introduced by
$this->getLookupDouble() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\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...
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() );
0 ignored issues
show
Documentation introduced by
$this->getLookupDouble() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\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...
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() );
0 ignored issues
show
Documentation introduced by
$this->getLookupDouble() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\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...
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() );
0 ignored issues
show
Documentation introduced by
$this->getLookupDouble() is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Wikibase\DataMode...es\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...
120
121
		$this->assertEquals( $exists, $lookup->hasEntity( $id ) );
122
	}
123
124
}
125