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
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\Criterion; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content; |
12
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
13
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser; |
14
|
|
|
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest; |
15
|
|
|
|
16
|
|
|
class LogicalOrTest extends BaseTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Test parsing of OR statement. |
20
|
|
|
* |
21
|
|
|
* Notice regarding multiple criteria of same type: |
22
|
|
|
* |
23
|
|
|
* The XML decoder of EZ is not creating numeric arrays, instead using the tag as the array key. See |
24
|
|
|
* variable $logicalOrParsedFromXml. This causes the Field Tag to appear as one-element |
25
|
|
|
* (type numeric array) and two criteria configuration inside. The logical or parser will take care |
26
|
|
|
* of this and return a flatt LogicalOr criterion with 4 criteria inside. |
27
|
|
|
* |
28
|
|
|
* ``` |
29
|
|
|
* <OR> |
30
|
|
|
* <ContentTypeIdentifierCriterion>author</ContentTypeIdentifierCriterion> |
31
|
|
|
* <ContentTypeIdentifierCriterion>book</ContentTypeIdentifierCriterion> |
32
|
|
|
* <Field> |
33
|
|
|
* <name>title</name> |
34
|
|
|
* <operator>EQ</operator> |
35
|
|
|
* <value>Contributing to projects</value> |
36
|
|
|
* </Field> |
37
|
|
|
* <Field> |
38
|
|
|
* <name>title</name> |
39
|
|
|
* <operator>EQ</operator> |
40
|
|
|
* <value>Contributing to projects</value> |
41
|
|
|
* </Field> |
42
|
|
|
* </OR> |
43
|
|
|
* ``` |
44
|
|
|
*/ |
45
|
|
|
public function testParseLogicalOr() |
46
|
|
|
{ |
47
|
|
|
$logicalOrParsedFromXml = [ |
48
|
|
|
'OR' => [ |
49
|
|
|
'ContentTypeIdentifierCriterion' => [ |
50
|
|
|
0 => 'author', |
51
|
|
|
1 => 'book', |
52
|
|
|
], |
53
|
|
|
'Field' => [ |
54
|
|
|
0 => [ |
55
|
|
|
'name' => 'title', |
56
|
|
|
'operator' => 'EQ', |
57
|
|
|
'value' => 'Contributing to projects', |
58
|
|
|
], |
59
|
|
|
1 => [ |
60
|
|
|
'name' => 'title', |
61
|
|
|
'operator' => 'EQ', |
62
|
|
|
'value' => 'Contributing to projects', |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$criterionMock = $this->createMock(Content\Query\Criterion::class, [], [], '', false); |
69
|
|
|
|
70
|
|
|
$parserMock = $this->createMock(\eZ\Publish\Core\REST\Common\Input\Parser::class); |
71
|
|
|
$parserMock->method('parse')->willReturn($criterionMock); |
72
|
|
|
|
73
|
|
|
$result = $this->internalGetParser()->parse($logicalOrParsedFromXml, new ParsingDispatcher([ |
74
|
|
|
'application/vnd.ez.api.internal.criterion.ContentTypeIdentifier' => $parserMock, |
75
|
|
|
'application/vnd.ez.api.internal.criterion.Field' => $parserMock, |
76
|
|
|
])); |
77
|
|
|
|
78
|
|
|
self::assertInstanceOf(Content\Query\Criterion\LogicalOr::class, $result); |
79
|
|
|
self::assertCount(4, (array)$result->criteria); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
84
|
|
|
*/ |
85
|
|
|
public function testThrowsExceptionOnInvalidAndStatement() |
86
|
|
|
{ |
87
|
|
|
$this->internalGetParser()->parse(['OR' => 'Wrong type'], new ParsingDispatcher()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Parser\Criterion\LogicalOr |
92
|
|
|
*/ |
93
|
|
|
protected function internalGetParser() |
94
|
|
|
{ |
95
|
|
|
return new Parser\Criterion\LogicalOr(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.