Passed
Push — meaningfulInterfaces ( 00ef73...ff2825 )
by no
02:44
created

testAliasGroupListSerializerWithOptionObjectsForMaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Serializers;
4
5
use PHPUnit_Framework_TestCase;
6
use stdClass;
7
use Wikibase\DataModel\Serializers\Internal\AliasGroupListSerializer;
8
use Wikibase\DataModel\Term\AliasGroup;
9
use Wikibase\DataModel\Term\AliasGroupList;
10
11
/**
12
 * @covers Wikibase\DataModel\Serializers\Internal\AliasGroupListSerializer
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Addshore
16
 * @author Bene* < [email protected] >
17
 */
18
class AliasGroupListSerializerTest extends PHPUnit_Framework_TestCase {
19
20
	/**
21
	 * @param bool $useObjectsForMaps
22
	 *
23
	 * @return AliasGroupListSerializer
24
	 */
25
	private function buildSerializer( $useObjectsForMaps = false ) {
26
		$aliasGroupSerializer = $this->getMock( 'Wikibase\DataModel\Serializers\AliasGroupSerializer' );
27
		$aliasGroupSerializer->expects( $this->any() )
28
			->method( 'serialize' )
29
			->will( $this->returnCallback( function( AliasGroup $aliasGroup ) {
30
				return $aliasGroup->getAliases();
31
			} ) );
32
33
		return new AliasGroupListSerializer( $aliasGroupSerializer, $useObjectsForMaps );
34
	}
35
36
	/**
37
	 * @dataProvider serializationProvider
38
	 */
39
	public function testSerialization( AliasGroupList $input, $useObjectsForMaps, $expected ) {
40
		$serializer = $this->buildSerializer( $useObjectsForMaps );
41
42
		$output = $serializer->serialize( $input );
43
44
		$this->assertEquals( $expected, $output );
45
	}
46
47
	public function serializationProvider() {
48
		return array(
49
			array(
50
				new AliasGroupList( array( new AliasGroup( 'en', array() ) ) ),
51
				false,
52
				array(),
53
			),
54
			array(
55
				new AliasGroupList( array( new AliasGroup( 'en', array() ) ) ),
56
				true,
57
				new \stdClass()
58
			),
59
			array(
60
				new AliasGroupList( array( new AliasGroup( 'en', array( 'One' ) ) ) ),
61
				false,
62
				array( 'en' => array( 'One' ) )
63
			),
64
			array(
65
				new AliasGroupList( array( new AliasGroup( 'en', array( 'One', 'Pony' ) ) ) ),
66
				false,
67
				array( 'en' => array( 'One', 'Pony' ) )
68
			),
69
			array(
70
				new AliasGroupList( array(
71
					new AliasGroup( 'en', array( 'One', 'Pony' ) ),
72
					new AliasGroup( 'de', array( 'foo', 'bar' ) )
73
				) ),
74
				false,
75
				array(
76
					'en' => array( 'One', 'Pony' ),
77
					'de' => array( 'foo', 'bar' ),
78
				)
79
			),
80
		);
81
	}
82
83
	public function testAliasGroupListSerializerWithOptionObjectsForMaps() {
84
		$serializer = $this->buildSerializer( true );
85
86
		$aliases = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo', 'bar' ) ) ) );
87
88
		$serial = new \stdClass();
89
		$serial->en = array( 'foo', 'bar' );
90
91
		$this->assertEquals( $serial, $serializer->serialize( $aliases ) );
92
	}
93
94
}
95