Completed
Push — master ( a5872f...16de46 )
by André
28:57 queued 15:11
created

QueryParserTest::testDispatchOneQueryItem()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 24
loc 24
rs 8.9713
1
<?php
2
3
/**
4
 * File containing the SessionInputTest 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;
10
11
use eZ\Publish\API\Repository\Values\Content\Query;
12
use eZ\Publish\Core\REST\Server\Input\Parser\ContentQuery as QueryParser;
13
14
class QueryParserTest extends BaseTest
15
{
16
    public function testParseEmptyQuery()
17
    {
18
        $inputArray = array(
19
            'Filter' => [],
20
            'Criteria' => [],
21
            'Query' => [],
22
        );
23
24
        $parsingDispatcher = $this->getParsingDispatcherMock();
25
        $parser = $this->getParser();
26
27
        $result = $parser->parse($inputArray, $parsingDispatcher);
28
29
        $expectedQuery = new Query();
30
31
        $this->assertEquals($expectedQuery, $result);
32
    }
33
34 View Code Duplication
    public function testDispatchOneFilter()
35
    {
36
        $inputArray = array(
37
            'Filter' => ['ContentTypeIdentifierCriterion' => 'article'],
38
            'Criteria' => [],
39
            'Query' => [],
40
        );
41
42
        $parsingDispatcher = $this->getParsingDispatcherMock();
43
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            ->expects($this->once())
45
            ->method('parse')
46
            ->with(['ContentTypeIdentifierCriterion' => 'article'])
47
            ->will($this->returnValue(new Query\Criterion\ContentTypeIdentifier('article')));
48
49
        $parser = $this->getParser();
50
51
        $result = $parser->parse($inputArray, $parsingDispatcher);
52
53
        $expectedQuery = new Query();
54
        $expectedQuery->filter = new Query\Criterion\ContentTypeIdentifier('article');
55
56
        $this->assertEquals($expectedQuery, $result);
57
    }
58
59 View Code Duplication
    public function testDispatchMoreThanOneFilter()
60
    {
61
        $inputArray = array(
62
            'Filter' => ['ContentTypeIdentifierCriterion' => 'article', 'ParentLocationIdCriterion' => 762],
63
            'Criteria' => [],
64
            'Query' => [],
65
        );
66
67
        $parsingDispatcher = $this->getParsingDispatcherMock();
68
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            ->expects($this->at(0))
70
            ->method('parse')
71
            ->with(['ContentTypeIdentifierCriterion' => 'article'])
72
            ->will($this->returnValue(new Query\Criterion\ContentTypeIdentifier('article')));
73
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            ->expects($this->at(1))
75
            ->method('parse')
76
            ->with(['ParentLocationIdCriterion' => 762])
77
            ->will($this->returnValue(new Query\Criterion\ParentLocationId(762)));
78
79
        $parser = $this->getParser();
80
81
        $result = $parser->parse($inputArray, $parsingDispatcher);
82
83
        $expectedQuery = new Query();
84
        $expectedQuery->filter = new Query\Criterion\LogicalAnd([
85
            new Query\Criterion\ContentTypeIdentifier('article'),
86
            new Query\Criterion\ParentLocationId(762),
87
        ]);
88
89
        $this->assertEquals($expectedQuery, $result);
90
    }
91
92 View Code Duplication
    public function testDispatchOneQueryItem()
93
    {
94
        $inputArray = array(
95
            'Query' => ['ContentTypeIdentifierCriterion' => 'article'],
96
            'Criteria' => [],
97
            'Filter' => [],
98
        );
99
100
        $parsingDispatcher = $this->getParsingDispatcherMock();
101
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            ->expects($this->once())
103
            ->method('parse')
104
            ->with(['ContentTypeIdentifierCriterion' => 'article'])
105
            ->will($this->returnValue(new Query\Criterion\ContentTypeIdentifier('article')));
106
107
        $parser = $this->getParser();
108
109
        $result = $parser->parse($inputArray, $parsingDispatcher);
110
111
        $expectedQuery = new Query();
112
        $expectedQuery->query = new Query\Criterion\ContentTypeIdentifier('article');
113
114
        $this->assertEquals($expectedQuery, $result);
115
    }
116
117 View Code Duplication
    public function testDispatchMoreThanOneQueryItem()
118
    {
119
        $inputArray = array(
120
            'Query' => ['ContentTypeIdentifierCriterion' => 'article', 'ParentLocationIdCriterion' => 762],
121
            'Criteria' => [],
122
            'Filter' => [],
123
        );
124
125
        $parsingDispatcher = $this->getParsingDispatcherMock();
126
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
            ->expects($this->at(0))
128
            ->method('parse')
129
            ->with(['ContentTypeIdentifierCriterion' => 'article'])
130
            ->will($this->returnValue(new Query\Criterion\ContentTypeIdentifier('article')));
131
        $parsingDispatcher
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\R...nput\ParsingDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
            ->expects($this->at(1))
133
            ->method('parse')
134
            ->with(['ParentLocationIdCriterion' => 762])
135
            ->will($this->returnValue(new Query\Criterion\ParentLocationId(762)));
136
137
        $parser = $this->getParser();
138
139
        $result = $parser->parse($inputArray, $parsingDispatcher);
140
141
        $expectedQuery = new Query();
142
        $expectedQuery->query = new Query\Criterion\LogicalAnd([
143
            new Query\Criterion\ContentTypeIdentifier('article'),
144
            new Query\Criterion\ParentLocationId(762),
145
        ]);
146
147
        $this->assertEquals($expectedQuery, $result);
148
    }
149
150
    /**
151
     * Returns the session input parser.
152
     */
153
    protected function internalGetParser()
154
    {
155
        return new QueryParser();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...\Parser\ContentQuery(); (eZ\Publish\Core\REST\Ser...put\Parser\ContentQuery) 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...
156
    }
157
}
158