|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\DataModel\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Serializers\Exceptions\UnsupportedObjectException; |
|
7
|
|
|
use Serializers\Serializer; |
|
8
|
|
|
use stdClass; |
|
9
|
|
|
use Wikibase\DataModel\Serializers\AliasGroupListSerializer; |
|
10
|
|
|
use Wikibase\DataModel\Term\AliasGroup; |
|
11
|
|
|
use Wikibase\DataModel\Term\AliasGroupList; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @covers Wikibase\DataModel\Serializers\AliasGroupListSerializer |
|
15
|
|
|
* |
|
16
|
|
|
* @license GPL-2.0-or-later |
|
17
|
|
|
* @author Addshore |
|
18
|
|
|
* @author Bene* < [email protected] > |
|
19
|
|
|
*/ |
|
20
|
|
|
class AliasGroupListSerializerTest extends TestCase { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param bool $useObjectsForMaps |
|
24
|
|
|
* |
|
25
|
|
|
* @return AliasGroupListSerializer |
|
26
|
|
|
*/ |
|
27
|
|
|
private function buildSerializer( $useObjectsForMaps = false ) { |
|
28
|
|
|
$aliasGroupSerializer = $this->getMockBuilder( Serializer::class )->getMock(); |
|
29
|
|
|
$aliasGroupSerializer->expects( $this->any() ) |
|
30
|
|
|
->method( 'serialize' ) |
|
31
|
|
|
->will( $this->returnCallback( function( AliasGroup $aliasGroup ) { |
|
32
|
|
|
return $aliasGroup->getAliases(); |
|
33
|
|
|
} ) ); |
|
34
|
|
|
|
|
35
|
|
|
return new AliasGroupListSerializer( $aliasGroupSerializer, $useObjectsForMaps ); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider serializationProvider |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testSerialization( AliasGroupList $input, $useObjectsForMaps, $expected ) { |
|
42
|
|
|
$serializer = $this->buildSerializer( $useObjectsForMaps ); |
|
43
|
|
|
|
|
44
|
|
|
$output = $serializer->serialize( $input ); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals( $expected, $output ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function serializationProvider() { |
|
50
|
|
|
return [ |
|
51
|
|
|
[ |
|
52
|
|
|
new AliasGroupList( [ new AliasGroup( 'en', [] ) ] ), |
|
53
|
|
|
false, |
|
54
|
|
|
[], |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
new AliasGroupList( [ new AliasGroup( 'en', [] ) ] ), |
|
58
|
|
|
true, |
|
59
|
|
|
new \stdClass() |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
new AliasGroupList( [ new AliasGroup( 'en', [ 'One' ] ) ] ), |
|
63
|
|
|
false, |
|
64
|
|
|
[ 'en' => [ 'One' ] ] |
|
65
|
|
|
], |
|
66
|
|
|
[ |
|
67
|
|
|
new AliasGroupList( [ new AliasGroup( 'en', [ 'One', 'Pony' ] ) ] ), |
|
68
|
|
|
false, |
|
69
|
|
|
[ 'en' => [ 'One', 'Pony' ] ] |
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
new AliasGroupList( [ |
|
73
|
|
|
new AliasGroup( 'en', [ 'One', 'Pony' ] ), |
|
74
|
|
|
new AliasGroup( 'de', [ 'foo', 'bar' ] ) |
|
75
|
|
|
] ), |
|
76
|
|
|
false, |
|
77
|
|
|
[ |
|
78
|
|
|
'en' => [ 'One', 'Pony' ], |
|
79
|
|
|
'de' => [ 'foo', 'bar' ], |
|
80
|
|
|
] |
|
81
|
|
|
], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testWithUnsupportedObject() { |
|
86
|
|
|
$serializer = $this->buildSerializer(); |
|
87
|
|
|
|
|
88
|
|
|
$this->expectException( UnsupportedObjectException::class ); |
|
89
|
|
|
$serializer->serialize( new stdClass() ); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testAliasGroupListSerializerWithOptionObjectsForMaps() { |
|
93
|
|
|
$serializer = $this->buildSerializer( true ); |
|
94
|
|
|
|
|
95
|
|
|
$aliases = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo', 'bar' ] ) ] ); |
|
96
|
|
|
|
|
97
|
|
|
$serial = new \stdClass(); |
|
98
|
|
|
$serial->en = [ 'foo', 'bar' ]; |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals( $serial, $serializer->serialize( $aliases ) ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
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: