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