Completed
Push — master ( 7d8921...21f60a )
by Thomas
26s
created

testDeserialization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Deserializers\StatementListDeserializer;
7
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
8
use Wikibase\DataModel\Statement\Statement;
9
use Wikibase\DataModel\Statement\StatementList;
10
11
/**
12
 * @covers Wikibase\DataModel\Deserializers\StatementListDeserializer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Bene* < [email protected] >
16
 */
17
class StatementListDeserializerTest extends PHPUnit_Framework_TestCase {
18
19
	private function buildDeserializer() {
20
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
21
		$statement->setGuid( 'test' );
22
23
		$statementDeserializerMock = $this->getMock( '\Deserializers\Deserializer' );
24
		$statementDeserializerMock->expects( $this->any() )
25
			->method( 'deserialize' )
26
			->with( $this->equalTo( array(
27
				'mainsnak' => array(
28
					'snaktype' => 'novalue',
29
					'property' => 'P42'
30
				),
31
				'type' => 'statement',
32
				'rank' => 'normal'
33
			) ) )
34
			->will( $this->returnValue( $statement ) );
35
36
		return new StatementListDeserializer( $statementDeserializerMock );
37
	}
38
39
	/**
40
	 * @dataProvider nonDeserializableProvider
41
	 */
42
	public function testDeserializeThrowsDeserializationException( $nonDeserializable ) {
43
		$deserializer = $this->buildDeserializer();
44
		$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' );
45
		$deserializer->deserialize( $nonDeserializable );
46
	}
47
48
	public function nonDeserializableProvider() {
49
		return array(
50
			array(
51
				42
52
			),
53
			array(
54
				array(
55
					'id' => 'P10'
56
				)
57
			),
58
			array(
59
				array(
60
					'type' => '42'
61
				)
62
			),
63
		);
64
	}
65
66
	/**
67
	 * @dataProvider deserializationProvider
68
	 */
69
	public function testDeserialization( $object, $serialization ) {
70
		$this->assertEquals( $object, $this->buildDeserializer()->deserialize( $serialization ) );
71
	}
72
73
	public function deserializationProvider() {
74
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
75
		$statement->setGuid( 'test' );
76
77
		return array(
78
			array(
79
				new StatementList(),
80
				array()
81
			),
82
			array(
83
				new StatementList( array(
84
					$statement
85
				) ),
86
				array(
87
					'P42' => array(
88
						array(
89
							'mainsnak' => array(
90
								'snaktype' => 'novalue',
91
								'property' => 'P42'
92
							),
93
							'type' => 'statement',
94
							'rank' => 'normal'
95
						)
96
					)
97
				)
98
			),
99
		);
100
	}
101
102
}
103