ByPropertyIdGrouperTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 6
dl 0
loc 173
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 4 1
A validConstructorArgumentProvider() 0 8 1
A testConstructorThrowsException() 0 4 1
A invalidConstructorArgumentProvider() 0 10 1
A provideGetPropertyIds() 0 31 1
A testGetPropertyIds() 0 5 1
A provideGetByPropertyId() 0 17 1
A testGetByPropertyId() 0 8 1
A testGetByPropertyIdThrowsException() 0 5 1
A provideHasPropertyId() 0 11 1
A testHasPropertyId() 0 4 1
A getPropertyIdProviders() 0 9 1
A getPropertyIdProviderMock() 0 13 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests;
4
5
use ArrayObject;
6
use InvalidArgumentException;
7
use OutOfBoundsException;
8
use PHPUnit\Framework\TestCase;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\PropertyIdProvider;
11
use Wikibase\DataModel\Services\ByPropertyIdGrouper;
12
use Wikibase\DataModel\Snak\Snak;
13
14
/**
15
 * @covers \Wikibase\DataModel\Services\ByPropertyIdGrouper
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Bene* < [email protected] >
19
 * @author Thiemo Kreuz
20
 */
21
class ByPropertyIdGrouperTest extends TestCase {
22
23
	/**
24
	 * @dataProvider validConstructorArgumentProvider
25
	 */
26
	public function testConstructor( $argument ) {
27
		$instance = new ByPropertyIdGrouper( $argument );
28
		$this->assertCount( count( $argument ), $instance->getPropertyIds() );
0 ignored issues
show
Documentation introduced by
$instance->getPropertyIds() is of type array<integer,object<Wik...del\Entity\PropertyId>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
29
	}
30
31
	public function validConstructorArgumentProvider() {
32
		return [
33
			[ [] ],
34
			[ [ $this->getPropertyIdProviderMock( 'P1' ) ] ],
35
			[ new ArrayObject() ],
36
			[ new ArrayObject( [ $this->getPropertyIdProviderMock( 'P1' ) ] ) ],
37
		];
38
	}
39
40
	/**
41
	 * @dataProvider invalidConstructorArgumentProvider
42
	 */
43
	public function testConstructorThrowsException( $argument ) {
44
		$this->expectException( InvalidArgumentException::class );
45
		new ByPropertyIdGrouper( $argument );
46
	}
47
48
	public function invalidConstructorArgumentProvider() {
49
		return [
50
			[ null ],
51
			[ 'notAnObject' ],
52
			[ [ null ] ],
53
			[ [ 'notAnObject' ] ],
54
			[ new ArrayObject( [ null ] ) ],
55
			[ new ArrayObject( [ 'notAnObject' ] ) ],
56
		];
57
	}
58
59
	public function provideGetPropertyIds() {
60
		$cases = [];
61
62
		$cases['empty list'] = [
63
			[],
64
			[]
65
		];
66
67
		$cases['some property ids'] = [
68
			[
69
				$this->getPropertyIdProviderMock( 'P42' ),
70
				$this->getPropertyIdProviderMock( 'P23' )
71
			],
72
			[
73
				new PropertyId( 'P42' ),
74
				new PropertyId( 'P23' )
75
			]
76
		];
77
78
		$cases['duplicate property ids'] = [
79
			$this->getPropertyIdProviders(),
80
			[
81
				new PropertyId( 'P42' ),
82
				new PropertyId( 'P23' ),
83
				new PropertyId( 'P15' ),
84
				new PropertyId( 'P10' )
85
			]
86
		];
87
88
		return $cases;
89
	}
90
91
	/**
92
	 * @dataProvider provideGetPropertyIds
93
	 * @param PropertyIdProvider[] $propertyIdProviders
94
	 * @param PropertyId[] $expectedPropertyIds
95
	 */
96
	public function testGetPropertyIds( array $propertyIdProviders, array $expectedPropertyIds ) {
97
		$byPropertyIdGrouper = new ByPropertyIdGrouper( $propertyIdProviders );
98
		$propertyIds = $byPropertyIdGrouper->getPropertyIds();
99
		$this->assertEquals( $expectedPropertyIds, $propertyIds );
100
	}
101
102
	public function provideGetByPropertyId() {
103
		$cases = [];
104
105
		$cases[] = [
106
			$this->getPropertyIdProviders(),
107
			'P42',
108
			[ 'abc', 'jkl' ]
109
		];
110
111
		$cases[] = [
112
			$this->getPropertyIdProviders(),
113
			'P23',
114
			[ 'def' ]
115
		];
116
117
		return $cases;
118
	}
119
120
	/**
121
	 * @dataProvider provideGetByPropertyId
122
	 */
123
	public function testGetByPropertyId( array $propertyIdProviders, $propertyId, array $expectedValues ) {
124
		$byPropertyIdGrouper = new ByPropertyIdGrouper( $propertyIdProviders );
125
		$values = $byPropertyIdGrouper->getByPropertyId( new PropertyId( $propertyId ) );
126
		array_walk( $values, function( Snak &$value ) {
127
			$value = $value->getType();
128
		} );
129
		$this->assertEquals( $expectedValues, $values );
130
	}
131
132
	public function testGetByPropertyIdThrowsException() {
133
		$byPropertyIdGrouper = new ByPropertyIdGrouper( $this->getPropertyIdProviders() );
0 ignored issues
show
Documentation introduced by
$this->getPropertyIdProviders() is of type array<integer,object<PHP...kObject\\MockObject>"}>, but the function expects a array<integer,object<Wik...r>>|object<Traversable>.

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...
134
		$this->expectException( OutOfBoundsException::class );
135
		$byPropertyIdGrouper->getByPropertyId( new PropertyId( 'P11' ) );
136
	}
137
138
	public function provideHasPropertyId() {
139
		$cases = [];
140
141
		$cases[] = [ $this->getPropertyIdProviders(), 'P42', true ];
142
		$cases[] = [ $this->getPropertyIdProviders(), 'P23', true ];
143
		$cases[] = [ $this->getPropertyIdProviders(), 'P15', true ];
144
		$cases[] = [ $this->getPropertyIdProviders(), 'P10', true ];
145
		$cases[] = [ $this->getPropertyIdProviders(), 'P11', false ];
146
147
		return $cases;
148
	}
149
150
	/**
151
	 * @dataProvider provideHasPropertyId
152
	 */
153
	public function testHasPropertyId( array $propertyIdProviders, $propertyId, $expectedValue ) {
154
		$byPropertyIdGrouper = new ByPropertyIdGrouper( $propertyIdProviders );
155
		$this->assertEquals( $expectedValue, $byPropertyIdGrouper->hasPropertyId( new PropertyId( $propertyId ) ) );
156
	}
157
158
	/**
159
	 * @return PropertyIdProvider[]
160
	 */
161
	private function getPropertyIdProviders() {
162
		return [
163
			$this->getPropertyIdProviderMock( 'P42', 'abc' ),
164
			$this->getPropertyIdProviderMock( 'P23', 'def' ),
165
			$this->getPropertyIdProviderMock( 'P15', 'ghi' ),
166
			$this->getPropertyIdProviderMock( 'P42', 'jkl' ),
167
			$this->getPropertyIdProviderMock( 'P10', 'mno' )
168
		];
169
	}
170
171
	/**
172
	 * Creates a PropertyIdProvider mock which can return a value.
173
	 *
174
	 * @param string $propertyId
175
	 * @param string|null $type
176
	 *
177
	 * @return PropertyIdProvider
178
	 */
179
	private function getPropertyIdProviderMock( $propertyId, $type = null ) {
180
		$propertyIdProvider = $this->createMock( Snak::class );
181
182
		$propertyIdProvider->expects( $this->once() )
183
			->method( 'getPropertyId' )
184
			->will( $this->returnValue( new PropertyId( $propertyId ) ) );
185
186
		$propertyIdProvider->expects( $this->any() )
187
			->method( 'getType' )
188
			->will( $this->returnValue( $type ) );
189
190
		return $propertyIdProvider;
191
	}
192
193
}
194