StableItemSerializerTest::testSerialization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\Queryr\Serialization;
4
5
use DataValues\NumberValue;
6
use DataValues\StringValue;
7
use Queryr\Resources\SimpleItem;
8
use Queryr\Resources\SimpleStatement;
9
use Queryr\Serialization\SerializerFactory;
10
11
/**
12
 * @covers Queryr\Serialization\StableItemSerializer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class StableItemSerializerTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testGivenNonItem_exceptionIsThrown() {
20
		$serializer = ( new SerializerFactory() )->newStableItemSerializer( [] );
21
22
		$this->setExpectedException( 'Serializers\Exceptions\UnsupportedObjectException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
23
		$serializer->serialize( null );
24
	}
25
26
	private function newSimpleItem() {
27
		$item = new SimpleItem();
28
29
		$item->ids = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('wikidata' => 'Q13...wikipedia' => 'Katzen') of type array<string,string,{"wi...e.wikipedia":"string"}> is incompatible with the declared type array<integer,string> of property $ids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
			'wikidata' => 'Q1337',
31
			'en.wikipedia' => 'Kitten',
32
			'de.wikipedia' => 'Katzen',
33
		];
34
35
		$item->label = 'kittens';
36
		$item->description = 'lots of kittens';
37
		$item->aliases = [ 'cats' ];
38
39
		$item->statements = [
40
			SimpleStatement::newInstance()
41
				->withPropertyName( 'Population prop name' )
42
				->withPropertyId( 'P23' )
43
				->withType( 'number' )
44
				->withValues( [ new NumberValue( 9001 ) ] ),
45
46
			SimpleStatement::newInstance()
47
				->withPropertyName( 'foo bar baz' )
48
				->withPropertyId( 'P42' )
49
				->withType( 'string' )
50
				->withValues( [ new StringValue( 'Jeroen' ), new StringValue( 'Abraham' ) ] ),
51
52
			SimpleStatement::newInstance()
53
				->withPropertyName( 'Property that is no in the map' )
54
				->withPropertyId( 'P1337' )
55
				->withType( 'number' )
56
				->withValues( [ new NumberValue( 1337 ) ] ),
57
		];
58
59
		return $item;
60
	}
61
62
	public function testSerialization() {
63
		$serializer = ( new SerializerFactory() )->newStableItemSerializer( [
64
			'P42' => 'Certified by',
65
			'P23' => 'Population',
66
		] );
67
68
		$serialized = $serializer->serialize( $this->newSimpleItem() );
69
70
		$expected = [
71
			'id' => [
72
				'wikidata' => 'Q1337',
73
				'en.wikipedia' => 'Kitten',
74
				'de.wikipedia' => 'Katzen',
75
			],
76
77
			'label' => 'kittens',
78
			'description' => 'lots of kittens',
79
			'aliases' => [ 'cats' ],
80
81
			'data' => [
82
				'Population' => [
83
					'value' => 9001,
84
					'type' => 'number'
85
				],
86
				'Certified by' => [
87
					'value' => 'Jeroen',
88
					'values' => [ 'Jeroen', 'Abraham' ],
89
					'type' => 'string'
90
				],
91
			]
92
		];
93
94
		$this->assertEquals( $expected, $serialized );
95
	}
96
97
}
98