TermSerializerTest::testSerialization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Serializers;
4
5
use PHPUnit\Framework\TestCase;
6
use Serializers\Exceptions\UnsupportedObjectException;
7
use Wikibase\DataModel\Serializers\TermSerializer;
8
use Wikibase\DataModel\Term\Term;
9
use Wikibase\DataModel\Term\TermFallback;
10
11
/**
12
 * @covers Wikibase\DataModel\Serializers\TermSerializer
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Addshore
16
 */
17
class TermSerializerTest extends TestCase {
18
19
	/**
20
	 * @dataProvider serializationProvider
21
	 */
22
	public function testSerialization( Term $input, array $expected ) {
23
		$serializer = new TermSerializer();
24
25
		$output = $serializer->serialize( $input );
26
27
		$this->assertEquals( $expected, $output );
28
	}
29
30
	public function serializationProvider() {
31
		return [
32
			[
33
				new Term( 'en', 'SomeValue' ),
34
				[
35
					'language' => 'en',
36
					'value' => 'SomeValue',
37
				]
38
			],
39
			[
40
				new TermFallback( 'en', 'SomeValue', 'en-gb', 'en' ),
41
				[
42
					'language' => 'en-gb',
43
					'value' => 'SomeValue',
44
					'source' => 'en',
45
				]
46
			],
47
		];
48
	}
49
50
	public function testWithUnsupportedObject() {
51
		$serializer = new TermSerializer();
52
53
		$this->expectException( UnsupportedObjectException::class );
54
		$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\Term>.

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...
55
	}
56
57
}
58