Passed
Push — master ( 747eee...4f558c )
by Leszek
03:40
created

testNewAliasGroupSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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