AliasGroupListSerializerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSerializer() 0 10 1
A testSerialization() 0 7 1
A serializationProvider() 0 35 1
A testWithUnsupportedObject() 0 6 1
A testAliasGroupListSerializerWithOptionObjectsForMaps() 0 10 1
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Serializers;
4
5
use PHPUnit\Framework\TestCase;
6
use Serializers\Exceptions\UnsupportedObjectException;
7
use Serializers\Serializer;
8
use stdClass;
9
use Wikibase\DataModel\Serializers\AliasGroupListSerializer;
10
use Wikibase\DataModel\Term\AliasGroup;
11
use Wikibase\DataModel\Term\AliasGroupList;
12
13
/**
14
 * @covers Wikibase\DataModel\Serializers\AliasGroupListSerializer
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Addshore
18
 * @author Bene* < [email protected] >
19
 */
20
class AliasGroupListSerializerTest extends TestCase {
21
22
	/**
23
	 * @param bool $useObjectsForMaps
24
	 *
25
	 * @return AliasGroupListSerializer
26
	 */
27
	private function buildSerializer( $useObjectsForMaps = false ) {
28
		$aliasGroupSerializer = $this->getMockBuilder( Serializer::class )->getMock();
29
		$aliasGroupSerializer->expects( $this->any() )
30
			->method( 'serialize' )
31
			->will( $this->returnCallback( function( AliasGroup $aliasGroup ) {
32
				return $aliasGroup->getAliases();
33
			} ) );
34
35
		return new AliasGroupListSerializer( $aliasGroupSerializer, $useObjectsForMaps );
0 ignored issues
show
Documentation introduced by
$aliasGroupSerializer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
	}
37
38
	/**
39
	 * @dataProvider serializationProvider
40
	 */
41
	public function testSerialization( AliasGroupList $input, $useObjectsForMaps, $expected ) {
42
		$serializer = $this->buildSerializer( $useObjectsForMaps );
43
44
		$output = $serializer->serialize( $input );
45
46
		$this->assertEquals( $expected, $output );
47
	}
48
49
	public function serializationProvider() {
50
		return [
51
			[
52
				new AliasGroupList( [ new AliasGroup( 'en', [] ) ] ),
53
				false,
54
				[],
55
			],
56
			[
57
				new AliasGroupList( [ new AliasGroup( 'en', [] ) ] ),
58
				true,
59
				new \stdClass()
60
			],
61
			[
62
				new AliasGroupList( [ new AliasGroup( 'en', [ 'One' ] ) ] ),
63
				false,
64
				[ 'en' => [ 'One' ] ]
65
			],
66
			[
67
				new AliasGroupList( [ new AliasGroup( 'en', [ 'One', 'Pony' ] ) ] ),
68
				false,
69
				[ 'en' => [ 'One', 'Pony' ] ]
70
			],
71
			[
72
				new AliasGroupList( [
73
					new AliasGroup( 'en', [ 'One', 'Pony' ] ),
74
					new AliasGroup( 'de', [ 'foo', 'bar' ] )
75
				] ),
76
				false,
77
				[
78
					'en' => [ 'One', 'Pony' ],
79
					'de' => [ 'foo', 'bar' ],
80
				]
81
			],
82
		];
83
	}
84
85
	public function testWithUnsupportedObject() {
86
		$serializer = $this->buildSerializer();
87
88
		$this->expectException( UnsupportedObjectException::class );
89
		$serializer->serialize( new stdClass() );
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<Wikibase\DataModel\Term\AliasGroupList>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
	}
91
92
	public function testAliasGroupListSerializerWithOptionObjectsForMaps() {
93
		$serializer = $this->buildSerializer( true );
94
95
		$aliases = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo', 'bar' ] ) ] );
96
97
		$serial = new \stdClass();
98
		$serial->en = [ 'foo', 'bar' ];
99
100
		$this->assertEquals( $serial, $serializer->serialize( $aliases ) );
101
	}
102
103
}
104