1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing a test class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\SortClause; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished; |
15
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser\SortClause\DataKeyValueObjectClass; |
16
|
|
|
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest; |
17
|
|
|
|
18
|
|
|
class DataKeyValueObjectClassTest extends BaseTest |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Tests the DataKeyValueObjectClass parser. |
22
|
|
|
*/ |
23
|
|
|
public function testParse() |
24
|
|
|
{ |
25
|
|
|
$inputArray = array( |
26
|
|
|
'DatePublished' => Query::SORT_ASC, |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
30
|
|
|
$result = $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
31
|
|
|
|
32
|
|
|
$this->assertEquals( |
33
|
|
|
new DatePublished(Query::SORT_ASC), |
34
|
|
|
$result, |
35
|
|
|
'DataKeyValueObjectClass parser not created correctly.' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test DataKeyValueObjectClass parser throwing exception on missing sort clause. |
41
|
|
|
* |
42
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
43
|
|
|
* @expectedExceptionMessage The <DatePublished> sort clause doesn't exist in the input structure |
44
|
|
|
*/ |
45
|
|
|
public function testParseExceptionOnMissingSortClause() |
46
|
|
|
{ |
47
|
|
|
$inputArray = array( |
48
|
|
|
'name' => 'Keep on mocking in the free world', |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
52
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test DataKeyValueObjectClass parser throwing exception on invalid direction format. |
57
|
|
|
* |
58
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
59
|
|
|
* @expectedExceptionMessage Invalid direction format in <DatePublished> sort clause |
60
|
|
|
*/ |
61
|
|
|
public function testParseExceptionOnInvalidDirectionFormat() |
62
|
|
|
{ |
63
|
|
|
$inputArray = array( |
64
|
|
|
'DatePublished' => 'Jailhouse Mock', |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
68
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test DataKeyValueObjectClass parser throwing exception on nonexisting value object class. |
73
|
|
|
* |
74
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
75
|
|
|
* @expectedExceptionMessage Value object class <eC\Pubish\APl\Repudiatory\BadValues\Discontent\Queezy\SantaClause\ThisClassIsExistentiallyChallenged> is not defined |
76
|
|
|
*/ |
77
|
|
|
public function testParseExceptionOnNonexistingValueObjectClass() |
78
|
|
|
{ |
79
|
|
|
$inputArray = array( |
80
|
|
|
'DatePublished' => Query::SORT_ASC, |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$dataKeyValueObjectClass = new DataKeyValueObjectClass( |
84
|
|
|
'DatePublished', |
85
|
|
|
'eC\Pubish\APl\Repudiatory\BadValues\Discontent\Queezy\SantaClause\ThisClassIsExistentiallyChallenged' |
86
|
|
|
); |
87
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the DataKeyValueObjectClass parser. |
92
|
|
|
* |
93
|
|
|
* @return \eZ\Publish\Core\REST\Server\Input\Parser\SortClause\DataKeyValueObjectClass |
94
|
|
|
*/ |
95
|
|
|
protected function internalGetParser() |
96
|
|
|
{ |
97
|
|
|
return new DataKeyValueObjectClass( |
|
|
|
|
98
|
|
|
'DatePublished', |
99
|
|
|
'eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.