testSerializationWithValueForOneProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 30
rs 9.44
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\SimpleItemSerializer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class SimpleItemSerializerTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testGivenNonItem_exceptionIsThrown() {
20
		$serializer = ( new SerializerFactory() )->newSimpleItemSerializer();
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( 'fluffiness' )
42
				->withType( 'number' )
43
				->withValues( [ new NumberValue( 9001 ) ] ),
44
45
			SimpleStatement::newInstance()
46
				->withPropertyName( 'awesome' )
47
				->withType( 'string' )
48
				->withValues( [ new StringValue( 'Jeroen' ), new StringValue( 'Abraham' ) ] ),
49
		];
50
51
		return $item;
52
	}
53
54
	public function testSerializationWithValueForOneProperty() {
55
		$serializer = ( new SerializerFactory() )->newSimpleItemSerializer();
56
		$serialized = $serializer->serialize( $this->newSimpleItem() );
57
58
		$expected = [
59
			'id' => [
60
				'wikidata' => 'Q1337',
61
				'en.wikipedia' => 'Kitten',
62
				'de.wikipedia' => 'Katzen',
63
			],
64
65
			'label' => 'kittens',
66
			'description' => 'lots of kittens',
67
			'aliases' => [ 'cats' ],
68
69
			'data' => [
70
				'fluffiness' => [
71
					'value' => 9001,
72
					'type' => 'number'
73
				],
74
				'awesome' => [
75
					'value' => 'Jeroen',
76
					'values' => [ 'Jeroen', 'Abraham' ],
77
					'type' => 'string'
78
				],
79
			]
80
		];
81
82
		$this->assertEquals( $expected, $serialized );
83
	}
84
85
}
86