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() ); |
|
|
|
|
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() ); |
|
|
|
|
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
|
|
|
|
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: