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

TermDeserializerTest::nonDeserializableProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 24
rs 8.9713
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\TermDeserializer;
7
use Wikibase\DataModel\Term\Term;
8
9
/**
10
 * @covers Wikibase\DataModel\Deserializers\TermDeserializer
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Addshore
14
 */
15
class TermDeserializerTest extends DeserializerBaseTest {
16
17
	/**
18
	 * @return Deserializer
19
	 */
20
	public function buildDeserializer() {
21
		return new TermDeserializer();
22
	}
23
24
	public function deserializableProvider() {
25
		return array( array() );
26
	}
27
28
	/**
29
	 * @return array[] things that aren't deserialized by the deserializer
30
	 */
31
	public function nonDeserializableProvider() {
32
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('must be an...', 'source' => 'de'))); (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...
33
			'must be an array' => array( new \stdClass() ),
34
			'must contain language' => array( array(
35
				'value' => 'FooBar',
36
			) ),
37
			'must contain value' => array( array(
38
				'language' => 'de',
39
			) ),
40
			'language must be string' => array( array(
41
				'language' => 123,
42
				'value' => 'FooBar',
43
			) ),
44
			'value must be string' => array( array(
45
				'language' => 'de',
46
				'value' => 999,
47
			) ),
48
			'must not contain source' => array( array(
49
				'language' => 'fr',
50
				'value' => 'Fr to DE hehe',
51
				'source' => 'de',
52
			) ),
53
		);
54
	}
55
56
	/**
57
	 * @return array[] an array of array( object deserialized, serialization )
58
	 */
59
	public function deserializationProvider() {
60
		return array(
61
			array(
62
				new Term( 'en', 'Value' ),
63
				array(
64
					'language' => 'en',
65
					'value' => 'Value',
66
				),
67
			),
68
		);
69
	}
70
71
}
72