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( |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.