|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\DataModel; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\Serializers\DataValueSerializer; |
|
6
|
|
|
use Serializers\Serializer; |
|
7
|
|
|
use Wikibase\DataModel\Entity\Item; |
|
8
|
|
|
use Wikibase\DataModel\Entity\Property; |
|
9
|
|
|
use Wikibase\DataModel\Reference; |
|
10
|
|
|
use Wikibase\DataModel\ReferenceList; |
|
11
|
|
|
use Wikibase\DataModel\SerializerFactory; |
|
12
|
|
|
use Wikibase\DataModel\SiteLink; |
|
13
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
14
|
|
|
use Wikibase\DataModel\Snak\SnakList; |
|
15
|
|
|
use Wikibase\DataModel\Snak\TypedSnak; |
|
16
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
17
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
|
18
|
|
|
use Wikibase\DataModel\Term\AliasGroup; |
|
19
|
|
|
use Wikibase\DataModel\Term\AliasGroupList; |
|
20
|
|
|
use Wikibase\DataModel\Term\Term; |
|
21
|
|
|
use Wikibase\DataModel\Term\TermList; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @license GPL-2.0+ |
|
25
|
|
|
* @author Thomas Pellissier Tanon |
|
26
|
|
|
* @author Bene* < [email protected] > |
|
27
|
|
|
*/ |
|
28
|
|
|
class SerializerFactoryTest extends \PHPUnit_Framework_TestCase { |
|
29
|
|
|
|
|
30
|
|
|
private function buildSerializerFactory() { |
|
31
|
|
|
return new SerializerFactory( new DataValueSerializer() ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function assertSerializesWithoutException( Serializer $serializer, $object ) { |
|
35
|
|
|
$serializer->serialize( $object ); |
|
36
|
|
|
$this->assertTrue( true, 'No exception occurred during serialization' ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testNewItemSerializer() { |
|
40
|
|
|
$this->assertSerializesWithoutException( |
|
41
|
|
|
$this->buildSerializerFactory()->newItemSerializer(), |
|
42
|
|
|
new Item() |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testNewPropertySerializer() { |
|
47
|
|
|
$this->assertSerializesWithoutException( |
|
48
|
|
|
$this->buildSerializerFactory()->newPropertySerializer(), |
|
49
|
|
|
Property::newFromType( 'string' ) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testNewSiteLinkSerializer() { |
|
54
|
|
|
$this->assertSerializesWithoutException( |
|
55
|
|
|
$this->buildSerializerFactory()->newSiteLinkSerializer(), |
|
56
|
|
|
new SiteLink( 'enwiki', 'Nyan Cat' ) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testNewStatementSerializer() { |
|
61
|
|
|
$this->assertSerializesWithoutException( |
|
62
|
|
|
$this->buildSerializerFactory()->newStatementSerializer(), |
|
63
|
|
|
new Statement( new PropertyNoValueSnak( 42 ) ) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testNewStatementListSerializer() { |
|
68
|
|
|
$this->assertSerializesWithoutException( |
|
69
|
|
|
$this->buildSerializerFactory()->newStatementListSerializer(), |
|
70
|
|
|
new StatementList() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testNewReferencesSerializer() { |
|
75
|
|
|
$this->assertSerializesWithoutException( |
|
76
|
|
|
$this->buildSerializerFactory()->newReferencesSerializer(), |
|
77
|
|
|
new ReferenceList() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testNewReferenceSerializer() { |
|
82
|
|
|
$this->assertSerializesWithoutException( |
|
83
|
|
|
$this->buildSerializerFactory()->newReferenceSerializer(), |
|
84
|
|
|
new Reference() |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testNewSnakListSerializer() { |
|
89
|
|
|
$this->assertSerializesWithoutException( |
|
90
|
|
|
$this->buildSerializerFactory()->newSnakListSerializer(), |
|
91
|
|
|
new SnakList( array() ) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testNewSnakSerializer() { |
|
96
|
|
|
$this->assertSerializesWithoutException( |
|
97
|
|
|
$this->buildSerializerFactory()->newSnakSerializer(), |
|
98
|
|
|
new PropertyNoValueSnak( 42 ) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testFactoryCreateWithUnexpectedValue() { |
|
103
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
|
104
|
|
|
new SerializerFactory( new DataValueSerializer(), 1.0 ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testNewSnakListSerializerWithUseObjectsForMaps() { |
|
108
|
|
|
$factory = new SerializerFactory( new DataValueSerializer(), SerializerFactory::OPTION_OBJECTS_FOR_MAPS ); |
|
109
|
|
|
$serializer = $factory->newSnakListSerializer(); |
|
110
|
|
|
$this->assertAttributeSame( true, 'useObjectsForMaps' , $serializer ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testNewTypedSnakSerializer() { |
|
114
|
|
|
$this->assertSerializesWithoutException( |
|
115
|
|
|
$this->buildSerializerFactory()->newTypedSnakSerializer(), |
|
116
|
|
|
new TypedSnak( new PropertyNoValueSnak( 42 ), 'kittens' ) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testNewTermSerializer() { |
|
121
|
|
|
$this->assertSerializesWithoutException( |
|
122
|
|
|
$this->buildSerializerFactory()->newTermSerializer(), |
|
123
|
|
|
new Term( 'en', 'Foo' ) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testNewTermListSerializer() { |
|
128
|
|
|
$this->assertSerializesWithoutException( |
|
129
|
|
|
$this->buildSerializerFactory()->newTermListSerializer(), |
|
130
|
|
|
new TermList( array( new Term( 'de', 'Foo' ) ) ) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testNewAliasGroupSerializer() { |
|
135
|
|
|
$this->assertSerializesWithoutException( |
|
136
|
|
|
$this->buildSerializerFactory()->newAliasGroupSerializer(), |
|
137
|
|
|
new AliasGroup( 'en', array( 'foo', 'bar' ) ) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testNewAliasGroupListSerializer() { |
|
142
|
|
|
$this->assertSerializesWithoutException( |
|
143
|
|
|
$this->buildSerializerFactory()->newAliasGroupListSerializer(), |
|
144
|
|
|
new AliasGroupList( array( new AliasGroup( 'de', array( 'AA', 'BB' ) ) ) ) |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|