1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\SortClause; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field; |
11
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser\SortClause\Field as FieldParser; |
12
|
|
|
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest; |
13
|
|
|
|
14
|
|
|
class FieldTest extends BaseTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Tests the Field parser. |
18
|
|
|
*/ |
19
|
|
|
public function testParse() |
20
|
|
|
{ |
21
|
|
|
$inputArray = [ |
22
|
|
|
'Field' => [ |
23
|
|
|
'identifier' => 'content/field', |
24
|
|
|
'direction' => Query::SORT_ASC, |
25
|
|
|
], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$fieldParser = $this->getParser(); |
29
|
|
|
$result = $fieldParser->parse($inputArray, $this->getParsingDispatcherMock()); |
30
|
|
|
|
31
|
|
|
$this->assertEquals( |
32
|
|
|
new Field('content', 'field', Query::SORT_ASC), |
33
|
|
|
$result, |
34
|
|
|
'Field parser not created correctly.' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test Field parser throwing exception on missing sort clause. |
40
|
|
|
* |
41
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
42
|
|
|
* @expectedExceptionMessage The <Field> sort clause doesn't exist in the input structure |
43
|
|
|
*/ |
44
|
|
|
public function testParseExceptionOnMissingSortClause() |
45
|
|
|
{ |
46
|
|
|
$inputArray = [ |
47
|
|
|
'name' => 'Keep on mocking in the free world', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$fieldParser = $this->getParser(); |
51
|
|
|
$fieldParser->parse($inputArray, $this->getParsingDispatcherMock()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test Field parser throwing exception on invalid direction format. |
56
|
|
|
* |
57
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
58
|
|
|
* @expectedExceptionMessage Invalid direction format in <Field> sort clause |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function testParseExceptionOnInvalidDirectionFormat() |
61
|
|
|
{ |
62
|
|
|
$inputArray = [ |
63
|
|
|
'Field' => [ |
64
|
|
|
'identifier' => 'content/field', |
65
|
|
|
'direction' => 'mock', |
66
|
|
|
], |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$fieldParser = $this->getParser(); |
70
|
|
|
$fieldParser->parse($inputArray, $this->getParsingDispatcherMock()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the Field parser. |
75
|
|
|
* |
76
|
|
|
* @return \eZ\Publish\Core\REST\Server\Input\Parser\SortClause\Field |
77
|
|
|
*/ |
78
|
|
|
protected function internalGetParser() |
79
|
|
|
{ |
80
|
|
|
return new FieldParser(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.