Completed
Push — master ( 0bba95...d49286 )
by Bene
03:40
created

nonDeserializableProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Tests\Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Wikibase\DataModel\Deserializers\AliasGroupListDeserializer;
7
use Wikibase\DataModel\Term\AliasGroup;
8
use Wikibase\DataModel\Term\AliasGroupList;
9
10
/**
11
 * @covers Wikibase\DataModel\Deserializers\AliasGroupListDeserializer
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Addshore
15
 * @author Bene* < [email protected] >
16
 */
17
class AliasGroupListDeserializerTest extends DeserializerBaseTest {
18
19
	/**
20
	 * @return Deserializer
21
	 */
22
	public function buildDeserializer() {
23
		return new AliasGroupListDeserializer();
24
	}
25
26
	public function deserializableProvider() {
27
		return array( array() );
28
	}
29
30
	/**
31
	 * @return array[] things that aren't deserialized by the deserializer
32
	 */
33
	public function nonDeserializableProvider() {
34
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('must be an...ce' => 'fallback'))))); (array<string,array>) is incompatible with the return type declared by the abstract method Tests\Wikibase\DataModel...nDeserializableProvider of type array[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
35
			'must be an array' => array( new \stdClass() ),
36
			'must be an array of arrays' => array( array(
37
				'en' => new \stdClass(),
38
			) ),
39
			'array key must match' => array( array(
40
				'en' => array( array( 'language' => 'de', 'value' => 'Evil language' ) ),
41
			) ),
42
			'must be an array of arrays of arrays' => array( array(
43
				'en' => array( 'A' ),
44
				'de' => array( 'B' ),
45
			) ),
46
			'must contain language' => array( array(
47
				'en' => array( array( 'value' => 'foo' ) ),
48
			) ),
49
			'must contain value' => array( array(
50
				'en' => array( array( 'language' => 'en' ) ),
51
			) ),
52
			'must not contain source' => array( array(
53
				'en' => array(
54
					array( 'language' => 'en', 'value' => 'Evil language', 'source' => 'fallback' ),
55
				),
56
			) ),
57
		);
58
	}
59
60
	/**
61
	 * @return array[] an array of array( object deserialized, serialization )
62
	 */
63
	public function deserializationProvider() {
64
		return array(
65
			array(
66
				new AliasGroupList( array( new AliasGroup( 'en', array() ) ) ),
67
				array( 'en' => array() ),
68
			),
69
			array(
70
				new AliasGroupList( array( new AliasGroup( 'en', array( 'A' ) ) ) ),
71
				array( 'en' => array(
72
					array( 'language' => 'en', 'value' => 'A' ),
73
				) ),
74
			),
75
			array(
76
				new AliasGroupList( array( new AliasGroup( 'en', array( 'A', 'B' ) ) ) ),
77
				array( 'en' => array(
78
					array( 'language' => 'en', 'value' => 'A' ),
79
					array( 'language' => 'en', 'value' => 'B' ),
80
				) ),
81
			),
82
		);
83
	}
84
85
}
86