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' ); |
|
|
|
|
23
|
|
|
$serializer->serialize( null ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function newSimpleItem() { |
27
|
|
|
$item = new SimpleItem(); |
28
|
|
|
|
29
|
|
|
$item->ids = [ |
|
|
|
|
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
|
|
|
|
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.