Completed
Pull Request — master (#199)
by no
02:30
created

testDeserializeThrowsDeserializationException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Deserializers\SiteLinkDeserializer;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\SiteLink;
10
11
/**
12
 * @covers Wikibase\DataModel\Deserializers\SiteLinkDeserializer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Thomas Pellissier Tanon
16
 */
17
class SiteLinkDeserializerTest extends PHPUnit_Framework_TestCase {
18
19
	private function buildDeserializer() {
20
		$entityIdDeserializerMock = $this->getMock( '\Deserializers\Deserializer' );
21
		$entityIdDeserializerMock->expects( $this->any() )
22
			->method( 'deserialize' )
23
			->with( $this->equalTo( 'Q42' ) )
24
			->will( $this->returnValue( new ItemId( 'Q42' ) ) );
25
26
		return new SiteLinkDeserializer( $entityIdDeserializerMock );
27
	}
28
29
	/**
30
	 * @dataProvider nonDeserializableProvider
31
	 */
32
	public function testDeserializeThrowsDeserializationException( $nonDeserializable ) {
33
		$deserializer = $this->buildDeserializer();
34
		$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' );
35
		$deserializer->deserialize( $nonDeserializable );
36
	}
37
38
	public function nonDeserializableProvider() {
39
		return array(
40
			array(
41
				42
42
			),
43
			array(
44
				array()
45
			),
46
			array(
47
				array(
48
					'id' => 'P10'
49
				)
50
			),
51
			array(
52
				array(
53
					'site' => '42value'
54
				)
55
			),
56
			array(
57
				array(
58
					'title' => '42value'
59
				)
60
			),
61
		);
62
	}
63
64
	/**
65
	 * @dataProvider deserializationProvider
66
	 */
67
	public function testDeserialization( $object, $serialization ) {
68
		$this->assertEquals( $object, $this->buildDeserializer()->deserialize( $serialization ) );
69
	}
70
71
	public function deserializationProvider() {
72
		return array(
73
			array(
74
				new SiteLink( 'enwiki', 'Nyan Cat' ),
75
				array(
76
					'site' => 'enwiki',
77
					'title' => 'Nyan Cat'
78
				)
79
			),
80
			array(
81
				new SiteLink( 'enwiki', 'Nyan Cat', array(
82
					new ItemId( 'Q42' )
83
				) ),
84
				array(
85
					'site' => 'enwiki',
86
					'title' => 'Nyan Cat',
87
					'badges' => array( 'Q42' )
88
				)
89
			),
90
		);
91
	}
92
93
	public function testDeserializeItemIdFilterPropertyId() {
94
		$entityIdDeserializerMock = $this->getMock( '\Deserializers\Deserializer' );
95
		$entityIdDeserializerMock->expects( $this->any() )
96
			->method( 'deserialize' )
97
			->with( $this->equalTo( 'P42' ) )
98
			->will( $this->returnValue( new PropertyId( 'P42' ) ) );
99
		$deserializer = new SiteLinkDeserializer( $entityIdDeserializerMock );
100
101
		$this->setExpectedException( '\Deserializers\Exceptions\InvalidAttributeException' );
102
		$deserializer->deserialize( array(
103
			'site' => 'frwikisource',
104
			'title' => 'Nyan Cat',
105
			'badges' => array( 'P42' )
106
		) );
107
	}
108
109
	public function testAssertBadgesIsArray() {
110
		$entityIdDeserializerMock = $this->getMock( '\Deserializers\Deserializer' );
111
		$deserializer = new SiteLinkDeserializer( $entityIdDeserializerMock );
112
113
		$this->setExpectedException( '\Deserializers\Exceptions\InvalidAttributeException' );
114
		$deserializer->deserialize( array(
115
			'site' => 'frwikisource',
116
			'title' => 'Nyan Cat',
117
			'badges' => 'Q42'
118
		) );
119
	}
120
121
}
122