TestFactoryBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 11
dl 0
loc 75
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A newLegacyDeserializerFactory() 0 6 1
A newFakeDataValueDeserializer() 0 10 1
A newDeserializerFactory() 0 6 1
A newLegacyDeserializerFactoryWithDataValueSupport() 0 6 1
A newDeserializerFactoryWithDataValueSupport() 0 6 1
A newRealDataValueDeserializer() 0 16 1
A newSerializerFactory() 0 3 1
A newCurrentDeserializerFactory() 0 6 1
1
<?php
2
3
namespace Tests\Integration\Wikibase\InternalSerialization;
4
5
use DataValues\Deserializers\DataValueDeserializer;
6
use DataValues\Serializers\DataValueSerializer;
7
use DataValues\StringValue;
8
use Deserializers\Deserializer;
9
use Wikibase\DataModel\Entity\BasicEntityIdParser;
10
use Wikibase\InternalSerialization\DeserializerFactory;
11
use Wikibase\InternalSerialization\LegacyDeserializerFactory;
12
use Wikibase\InternalSerialization\SerializerFactory;
13
14
/**
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class TestFactoryBuilder {
19
20
	public static function newLegacyDeserializerFactory( \PHPUnit\Framework\TestCase $testCase ) {
21
		return new LegacyDeserializerFactory(
22
			self::newFakeDataValueDeserializer( $testCase ),
0 ignored issues
show
Documentation introduced by
self::newFakeDataValueDeserializer($testCase) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

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...
23
			new BasicEntityIdParser()
24
		);
25
	}
26
27
	/**
28
	 * @param \PHPUnit\Framework\TestCase $testCase
29
	 *
30
	 * @return Deserializer
31
	 */
32
	private static function newFakeDataValueDeserializer( \PHPUnit\Framework\TestCase $testCase ) {
33
		$dataValueDeserializer = $testCase->getMockBuilder( Deserializer::class )->getMock();
34
35
		$dataValueDeserializer->expects( $testCase->any() )
36
			->method( 'deserialize' )
37
			->with( $testCase->equalTo( array( 'type' => 'string', 'value' => 'foo' ) ) )
38
			->will( $testCase->returnValue( new StringValue( 'foo' ) ) );
39
40
		return $dataValueDeserializer;
41
	}
42
43
	public static function newDeserializerFactory( \PHPUnit\Framework\TestCase $testCase ) {
44
		return new DeserializerFactory(
45
			self::newFakeDataValueDeserializer( $testCase ),
0 ignored issues
show
Documentation introduced by
self::newFakeDataValueDeserializer($testCase) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Deserializers\Deserializer>.

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...
46
			new BasicEntityIdParser()
47
		);
48
	}
49
50
	public static function newLegacyDeserializerFactoryWithDataValueSupport() {
51
		return new LegacyDeserializerFactory(
52
			self::newRealDataValueDeserializer(),
53
			new BasicEntityIdParser()
54
		);
55
	}
56
57
	public static function newDeserializerFactoryWithDataValueSupport() {
58
		return new DeserializerFactory(
59
			self::newRealDataValueDeserializer(),
60
			new BasicEntityIdParser()
61
		);
62
	}
63
64
	private static function newRealDataValueDeserializer() {
65
		$dataValueClasses = array(
66
			'boolean' => 'DataValues\BooleanValue',
67
			'number' => 'DataValues\NumberValue',
68
			'string' => 'DataValues\StringValue',
69
			'unknown' => 'DataValues\UnknownValue',
70
			'globecoordinate' => 'DataValues\Geo\Values\GlobeCoordinateValue',
71
			'monolingualtext' => 'DataValues\MonolingualTextValue',
72
			'multilingualtext' => 'DataValues\MultilingualTextValue',
73
			'quantity' => 'DataValues\QuantityValue',
74
			'time' => 'DataValues\TimeValue',
75
			'wikibase-entityid' => 'Wikibase\DataModel\Entity\EntityIdValue',
76
		);
77
78
		return new DataValueDeserializer( $dataValueClasses );
79
	}
80
81
	public static function newSerializerFactory() {
82
		return new SerializerFactory( new DataValueSerializer() );
83
	}
84
85
	public static function newCurrentDeserializerFactory() {
86
		return new \Wikibase\DataModel\DeserializerFactory(
87
			self::newRealDataValueDeserializer(),
88
			new BasicEntityIdParser()
89
		);
90
	}
91
92
}
93