1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataAccess\Tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Wikibase\DataAccess\EntitySource; |
8
|
|
|
use Wikibase\DataAccess\EntitySourceDefinitions; |
9
|
|
|
use Wikibase\Lib\EntityTypeDefinitions; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \Wikibase\DataAccess\EntitySourceDefinitions |
13
|
|
|
* |
14
|
|
|
* @group Wikibase |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
*/ |
18
|
|
|
class EntitySourceDefinitionsTest extends TestCase { |
19
|
|
|
|
20
|
|
|
public function testGivenInvalidArguments_constructorThrowsException() { |
21
|
|
|
$this->expectException( InvalidArgumentException::class ); |
22
|
|
|
new EntitySourceDefinitions( [ 'foobar' ], new EntityTypeDefinitions( [] ) ); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGivenEntityTypeProvidedByMultipleSources_constructorThrowsException() { |
26
|
|
|
$itemSourceOne = $this->newItemSource(); |
27
|
|
|
$itemSourceTwo = new EntitySource( 'dupe test', 'foodb', [ 'item' => [ 'namespaceId' => 100, 'slot' => 'main' ] ], '', '', '', '' ); |
28
|
|
|
|
29
|
|
|
$this->expectException( InvalidArgumentException::class ); |
30
|
|
|
new EntitySourceDefinitions( [ $itemSourceOne, $itemSourceTwo ], new EntityTypeDefinitions( [] ) ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testTwoSourcesWithSameName_constructorThrowsException() { |
34
|
|
|
$sourceOne = new EntitySource( 'same name', 'aaa', [ 'entityOne' => [ 'namespaceId' => 100, 'slot' => 'main' ] ], '', '', '', '' ); |
35
|
|
|
$sourceTwo = new EntitySource( 'same name', 'bbb', [ 'entityTwo' => [ 'namespaceId' => 101, 'slot' => 'main2' ] ], '', '', '', '' ); |
36
|
|
|
|
37
|
|
|
$this->expectException( InvalidArgumentException::class ); |
38
|
|
|
new EntitySourceDefinitions( [ $sourceOne, $sourceTwo ], new EntityTypeDefinitions( [] ) ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGivenKnownType_getSourceForEntityTypeReturnsTheConfiguredSource() { |
42
|
|
|
$itemSource = $this->newItemSource(); |
43
|
|
|
$propertySource = $this->newPropertySource(); |
44
|
|
|
|
45
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( [ $itemSource, $propertySource ], new EntityTypeDefinitions( [] ) ); |
46
|
|
|
|
47
|
|
|
$this->assertEquals( $itemSource, $sourceDefinitions->getSourceForEntityType( 'item' ) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGivenSubEntityOfKnownType_getSourceForEntityTypeReturnsTheRelevantSource() { |
51
|
|
|
$itemSource = $this->newItemSource(); |
52
|
|
|
$propertySource = $this->newPropertySource(); |
53
|
|
|
|
54
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( |
55
|
|
|
[ $itemSource, $propertySource ], |
56
|
|
|
new EntityTypeDefinitions( [ 'item' => [ EntityTypeDefinitions::SUB_ENTITY_TYPES => [ 'subitem' ] ] ] ) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( $itemSource, $sourceDefinitions->getSourceForEntityType( 'subitem' ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGivenUnknownType_getSourceForEntityTypeReturnsNull() { |
63
|
|
|
$itemSource = $this->newItemSource(); |
64
|
|
|
|
65
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( [ $itemSource ], new EntityTypeDefinitions( [] ) ); |
66
|
|
|
|
67
|
|
|
$this->assertNull( $sourceDefinitions->getSourceForEntityType( 'property' ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetEntityTypeToSourceMapping() { |
71
|
|
|
$itemSource = $this->newItemSource(); |
72
|
|
|
$propertySource = $this->newPropertySource(); |
73
|
|
|
$otherSource = $this->newOtherSource(); |
74
|
|
|
|
75
|
|
|
$sources = [ $itemSource, $propertySource, $otherSource ]; |
76
|
|
|
$entityTypeDefinitions = [ |
77
|
|
|
'other' => [ |
78
|
|
|
EntityTypeDefinitions::SUB_ENTITY_TYPES => [ 'otherSub' ], |
79
|
|
|
], |
80
|
|
|
'otherSub' => [], |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( $sources, new EntityTypeDefinitions( $entityTypeDefinitions ) ); |
84
|
|
|
|
85
|
|
|
$this->assertEquals( |
86
|
|
|
[ 'item' => $itemSource, 'property' => $propertySource, 'other' => $otherSource, 'otherSub' => $otherSource ], |
87
|
|
|
$sourceDefinitions->getEntityTypeToSourceMapping() |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetConceptBaseUris() { |
92
|
|
|
$itemSource = $this->newItemSource(); |
93
|
|
|
$propertySource = $this->newPropertySource(); |
94
|
|
|
|
95
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( [ $itemSource, $propertySource ], new EntityTypeDefinitions( [] ) ); |
96
|
|
|
|
97
|
|
|
$this->assertEquals( [ 'items' => 'itemsource:', 'properties' => 'propertysource:' ], $sourceDefinitions->getConceptBaseUris() ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetRdfNodeNamespacePrefixes() { |
101
|
|
|
$itemSource = $this->newItemSource(); |
102
|
|
|
$propertySource = $this->newPropertySource(); |
103
|
|
|
|
104
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( [ $itemSource, $propertySource ], new EntityTypeDefinitions( [] ) ); |
105
|
|
|
|
106
|
|
|
$this->assertEquals( [ 'items' => 'it', 'properties' => 'pro' ], $sourceDefinitions->getRdfNodeNamespacePrefixes() ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetRdfPredicateNamespacePrefixes() { |
110
|
|
|
$itemSource = $this->newItemSource(); |
111
|
|
|
$propertySource = $this->newPropertySource(); |
112
|
|
|
|
113
|
|
|
$sourceDefinitions = new EntitySourceDefinitions( [ $itemSource, $propertySource ], new EntityTypeDefinitions( [] ) ); |
114
|
|
|
|
115
|
|
|
$this->assertEquals( [ 'items' => '', 'properties' => 'pro' ], $sourceDefinitions->getRdfPredicateNamespacePrefixes() ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function newItemSource() { |
119
|
|
|
return new EntitySource( |
120
|
|
|
'items', |
121
|
|
|
false, |
122
|
|
|
[ 'item' => [ 'namespaceId' => 100, 'slot' => 'main' ] ], |
123
|
|
|
'itemsource:', |
124
|
|
|
'it', |
125
|
|
|
'', |
126
|
|
|
'' |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function newPropertySource() { |
131
|
|
|
return new EntitySource( |
132
|
|
|
'properties', |
133
|
|
|
false, |
134
|
|
|
[ 'property' => [ 'namespaceId' => 200, 'slot' => 'main' ] ], |
135
|
|
|
'propertysource:', |
136
|
|
|
'pro', |
137
|
|
|
'pro', |
138
|
|
|
'' |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function newOtherSource() { |
143
|
|
|
return new EntitySource( |
144
|
|
|
'others', |
145
|
|
|
false, |
146
|
|
|
[ 'other' => [ 'namespaceId' => 666, 'slot' => 'other' ] ], |
147
|
|
|
'othersource:', |
148
|
|
|
'ot', |
149
|
|
|
'', |
150
|
|
|
'' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
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: