Completed
Push — EZP-30548-Romanian_special_cha... ( 38d0c8...2c2658 )
by
unknown
41:58 queued 18:37
created

LogicalAndTest::internalGetParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 LogicalAndTest extends BaseTest
17
{
18
    /**
19
     * Logical parsing of AND 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 $logicalAndParsedFromXml. This causes the ContentTypeIdentifierCriterion-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 LogicalAnd criterion with 3 criteria inside.
27
     *
28
     * ```
29
     * <AND>
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
     * </AND>
38
     * ```
39
     */
40
    public function testParseLogicalAnd()
41
    {
42
        $logicalAndParsedFromXml = [
43
            'AND' => [
44
                'ContentTypeIdentifierCriterion' => [
45
                    0 => 'author',
46
                    1 => 'book',
47
                ],
48
                'Field' => [
49
                    'name' => 'title',
50
                    'operator' => 'EQ',
51
                    'value' => 'Contributing to projects',
52
                ],
53
            ],
54
        ];
55
56
        $criterionMock = $this->createMock(Content\Query\Criterion::class, [], [], '', false);
57
58
        $parserMock = $this->createMock(\eZ\Publish\Core\REST\Common\Input\Parser::class);
59
        $parserMock->method('parse')->willReturn($criterionMock);
60
61
        $result = $this->internalGetParser()->parse($logicalAndParsedFromXml, new ParsingDispatcher([
62
            'application/vnd.ez.api.internal.criterion.ContentTypeIdentifier' => $parserMock,
63
            'application/vnd.ez.api.internal.criterion.Field' => $parserMock,
64
        ]));
65
66
        self::assertInstanceOf(Content\Query\Criterion\LogicalAnd::class, $result);
67
        self::assertCount(3, (array)$result->criteria);
68
    }
69
70
    /**
71
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
72
     */
73
    public function testThrowsExceptionOnInvalidAndStatement()
74
    {
75
        $this->internalGetParser()->parse(['AND' => 'Should be an array'], new ParsingDispatcher());
76
    }
77
78
    /**
79
     * @return Parser\Criterion\LogicalAnd
80
     */
81
    protected function internalGetParser()
82
    {
83
        return new Parser\Criterion\LogicalAnd();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...Criterion\LogicalAnd(); (eZ\Publish\Core\REST\Ser...er\Criterion\LogicalAnd) is incompatible with the return type declared by the abstract method eZ\Publish\Core\REST\Ser...Test::internalGetParser of type eZ\Publish\Core\REST\Server\Input\Parser\Base.

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...
84
    }
85
}
86