Completed
Push — 6.7 ( 0a350a...28169c )
by André
18:05
created

testParseExceptionOnMissingSortField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 11
loc 11
rs 9.9
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;
10
11
use eZ\Publish\Core\REST\Server\Input\Parser\LocationUpdate;
12
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
15
class LocationUpdateTest extends BaseTest
16
{
17
    /**
18
     * Tests the LocationUpdate parser.
19
     */
20
    public function testParse()
21
    {
22
        $inputArray = array(
23
            'priority' => 0,
24
            'remoteId' => 'remote-id',
25
            'hidden' => 'true',
26
            'sortField' => 'PATH',
27
            'sortOrder' => 'ASC',
28
        );
29
30
        $locationUpdate = $this->getParser();
31
        $result = $locationUpdate->parse($inputArray, $this->getParsingDispatcherMock());
32
33
        $this->assertInstanceOf(
34
            '\\eZ\\Publish\\Core\\REST\\Server\\Values\\RestLocationUpdateStruct',
35
            $result,
36
            'LocationUpdateStruct not created correctly.'
37
        );
38
39
        $this->assertInstanceOf(
40
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationUpdateStruct',
41
            $result->locationUpdateStruct,
42
            'LocationUpdateStruct not created correctly.'
43
        );
44
45
        $this->assertEquals(
46
            0,
47
            $result->locationUpdateStruct->priority,
48
            'LocationUpdateStruct priority property not created correctly.'
49
        );
50
51
        $this->assertEquals(
52
            'remote-id',
53
            $result->locationUpdateStruct->remoteId,
54
            'LocationUpdateStruct remoteId property not created correctly.'
55
        );
56
57
        $this->assertTrue(
58
            $result->hidden,
59
            'hidden property not created correctly.'
60
        );
61
62
        $this->assertEquals(
63
            Location::SORT_FIELD_PATH,
64
            $result->locationUpdateStruct->sortField,
65
            'LocationUpdateStruct sortField property not created correctly.'
66
        );
67
68
        $this->assertEquals(
69
            Location::SORT_ORDER_ASC,
70
            $result->locationUpdateStruct->sortOrder,
71
            'LocationUpdateStruct sortOrder property not created correctly.'
72
        );
73
    }
74
75
    /**
76
     * Test LocationUpdate parser throwing exception on missing sort field.
77
     *
78
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
79
     * @expectedExceptionMessage Missing 'sortField' element for LocationUpdate.
80
     */
81 View Code Duplication
    public function testParseExceptionOnMissingSortField()
82
    {
83
        $inputArray = array(
84
            'priority' => 0,
85
            'remoteId' => 'remote-id',
86
            'sortOrder' => 'ASC',
87
        );
88
89
        $locationUpdate = $this->getParser();
90
        $locationUpdate->parse($inputArray, $this->getParsingDispatcherMock());
91
    }
92
93
    /**
94
     * Test LocationUpdate parser throwing exception on missing sort order.
95
     *
96
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
97
     * @expectedExceptionMessage Missing 'sortOrder' element for LocationUpdate.
98
     */
99 View Code Duplication
    public function testParseExceptionOnMissingSortOrder()
100
    {
101
        $inputArray = array(
102
            'priority' => 0,
103
            'remoteId' => 'remote-id',
104
            'sortField' => 'PATH',
105
        );
106
107
        $locationUpdate = $this->getParser();
108
        $locationUpdate->parse($inputArray, $this->getParsingDispatcherMock());
109
    }
110
111
    /**
112
     * Returns the LocationUpdateStruct parser.
113
     *
114
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\LocationUpdate
115
     */
116
    protected function internalGetParser()
117
    {
118
        return new LocationUpdate(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...his->getParserTools()); (eZ\Publish\Core\REST\Ser...t\Parser\LocationUpdate) 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...
119
            $this->getLocationServiceMock(),
120
            $this->getParserTools()
121
        );
122
    }
123
124
    /**
125
     * Get the location service mock object.
126
     *
127
     * @return \eZ\Publish\API\Repository\LocationService
128
     */
129
    protected function getLocationServiceMock()
130
    {
131
        $locationServiceMock = $this->getMock(
132
            'eZ\\Publish\\Core\\Repository\\LocationService',
133
            array(),
134
            array(),
135
            '',
136
            false
137
        );
138
139
        $locationServiceMock->expects($this->any())
140
            ->method('newLocationUpdateStruct')
141
            ->will(
142
                $this->returnValue(new LocationUpdateStruct())
143
            );
144
145
        return $locationServiceMock;
146
    }
147
}
148