SerializerFactoryTest::testNewTermListSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel;
4
5
use DataValues\Serializers\DataValueSerializer;
6
use InvalidArgumentException;
7
use PHPUnit\Framework\TestCase;
8
use ReflectionObject;
9
use Serializers\Serializer;
10
use Wikibase\DataModel\Entity\Item;
11
use Wikibase\DataModel\Entity\Property;
12
use Wikibase\DataModel\Reference;
13
use Wikibase\DataModel\ReferenceList;
14
use Wikibase\DataModel\SerializerFactory;
15
use Wikibase\DataModel\SiteLink;
16
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
17
use Wikibase\DataModel\Snak\SnakList;
18
use Wikibase\DataModel\Snak\TypedSnak;
19
use Wikibase\DataModel\Statement\Statement;
20
use Wikibase\DataModel\Statement\StatementList;
21
use Wikibase\DataModel\Term\AliasGroup;
22
use Wikibase\DataModel\Term\AliasGroupList;
23
use Wikibase\DataModel\Term\Term;
24
use Wikibase\DataModel\Term\TermList;
25
26
/**
27
 * @license GPL-2.0-or-later
28
 * @author Thomas Pellissier Tanon
29
 * @author Bene* < [email protected] >
30
 */
31
class SerializerFactoryTest extends TestCase {
32
33
	private function buildSerializerFactory() {
34
		return new SerializerFactory( new DataValueSerializer() );
35
	}
36
37
	private function assertSerializesWithoutException( Serializer $serializer, $object ) {
38
		$serializer->serialize( $object );
39
		$this->assertTrue( true, 'No exception occurred during serialization' );
40
	}
41
42
	public function testNewEntitySerializer() {
43
		$this->assertSerializesWithoutException(
44
			$this->buildSerializerFactory()->newEntitySerializer(),
45
			new Item()
46
		);
47
48
		$this->assertSerializesWithoutException(
49
			$this->buildSerializerFactory()->newEntitySerializer(),
50
			Property::newFromType( 'string' )
51
		);
52
	}
53
54
	public function testNewItemSerializer() {
55
		$this->assertSerializesWithoutException(
56
			$this->buildSerializerFactory()->newItemSerializer(),
57
			new Item()
58
		);
59
	}
60
61
	public function testNewPropertySerializer() {
62
		$this->assertSerializesWithoutException(
63
			$this->buildSerializerFactory()->newPropertySerializer(),
64
			Property::newFromType( 'string' )
65
		);
66
	}
67
68
	public function testNewSiteLinkSerializer() {
69
		$this->assertSerializesWithoutException(
70
			$this->buildSerializerFactory()->newSiteLinkSerializer(),
71
			new SiteLink( 'enwiki', 'Nyan Cat' )
72
		);
73
	}
74
75
	public function testNewStatementSerializer() {
76
		$this->assertSerializesWithoutException(
77
			$this->buildSerializerFactory()->newStatementSerializer(),
78
			new Statement( new PropertyNoValueSnak( 42 ) )
79
		);
80
	}
81
82
	public function testNewStatementListSerializer() {
83
		$this->assertSerializesWithoutException(
84
			$this->buildSerializerFactory()->newStatementListSerializer(),
85
			new StatementList()
86
		);
87
	}
88
89
	public function testNewReferencesSerializer() {
90
		$this->assertSerializesWithoutException(
91
			$this->buildSerializerFactory()->newReferencesSerializer(),
92
			new ReferenceList()
93
		);
94
	}
95
96
	public function testNewReferenceSerializer() {
97
		$this->assertSerializesWithoutException(
98
			$this->buildSerializerFactory()->newReferenceSerializer(),
99
			new Reference()
100
		);
101
	}
102
103
	public function testNewSnakListSerializer() {
104
		$this->assertSerializesWithoutException(
105
			$this->buildSerializerFactory()->newSnakListSerializer(),
106
			new SnakList( [] )
107
		);
108
	}
109
110
	public function testNewSnakSerializer() {
111
		$this->assertSerializesWithoutException(
112
			$this->buildSerializerFactory()->newSnakSerializer(),
113
			new PropertyNoValueSnak( 42 )
114
		);
115
	}
116
117
	public function testFactoryCreateWithUnexpectedValue() {
118
		$this->expectException( InvalidArgumentException::class );
119
		new SerializerFactory( new DataValueSerializer(), 1.0 );
120
	}
121
122
	public function testNewSnakListSerializerWithUseObjectsForMaps() {
123
		$factory = new SerializerFactory(
124
			new DataValueSerializer(),
125
			SerializerFactory::OPTION_OBJECTS_FOR_MAPS
126
		);
127
		$serializer = $factory->newSnakListSerializer();
128
129
		$refObject   = new ReflectionObject( $serializer );
130
		$refProperty = $refObject->getProperty( 'useObjectsForMaps' );
131
		$refProperty->setAccessible( true );
132
		$value = $refProperty->getValue( $serializer );
133
134
		$this->assertTrue( $value );
135
	}
136
137
	public function testNewTypedSnakSerializer() {
138
		$this->assertSerializesWithoutException(
139
			$this->buildSerializerFactory()->newTypedSnakSerializer(),
140
			new TypedSnak( new PropertyNoValueSnak( 42 ), 'kittens' )
141
		);
142
	}
143
144
	public function testNewTermSerializer() {
145
		$this->assertSerializesWithoutException(
146
			$this->buildSerializerFactory()->newTermSerializer(),
147
			new Term( 'en', 'Foo' )
148
		);
149
	}
150
151
	public function testNewTermListSerializer() {
152
		$this->assertSerializesWithoutException(
153
			$this->buildSerializerFactory()->newTermListSerializer(),
154
			new TermList( [ new Term( 'de', 'Foo' ) ] )
155
		);
156
	}
157
158
	public function testNewAliasGroupSerializer() {
159
		$this->assertSerializesWithoutException(
160
			$this->buildSerializerFactory()->newAliasGroupSerializer(),
161
			new AliasGroup( 'en', [ 'foo', 'bar' ] )
162
		);
163
	}
164
165
	public function testNewAliasGroupListSerializer() {
166
		$this->assertSerializesWithoutException(
167
			$this->buildSerializerFactory()->newAliasGroupListSerializer(),
168
			new AliasGroupList( [ new AliasGroup( 'de', [ 'AA', 'BB' ] ) ] )
169
		);
170
	}
171
172
	public function testSerializeSnaksWithoutHashConstant() {
173
		$this->assertSame(
174
			// expected:
175
			SerializerFactory::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH |
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...MAIN_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
176
			SerializerFactory::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH |
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...FIER_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
177
			SerializerFactory::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH,
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...ENCE_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
178
			// actual:
179
			SerializerFactory::OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
180
		);
181
	}
182
183
}
184