Completed
Push — travis_trusty ( e78e49...6f0d6f )
by André
54:54 queued 34:46
created

DataKeyValueObjectClassTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 85
rs 10
wmc 5
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testParse() 0 15 1
A testParseExceptionOnMissingSortClause() 0 9 1
A testParseExceptionOnInvalidDirectionFormat() 0 9 1
A testParseExceptionOnNonexistingValueObjectClass() 0 12 1
A internalGetParser() 0 7 1
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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\SortClause;
12
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished;
15
use eZ\Publish\Core\REST\Server\Input\Parser\SortClause\DataKeyValueObjectClass;
16
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest;
17
18
class DataKeyValueObjectClassTest extends BaseTest
19
{
20
    /**
21
     * Tests the DataKeyValueObjectClass parser.
22
     */
23
    public function testParse()
24
    {
25
        $inputArray = array(
26
            'DatePublished' => Query::SORT_ASC,
27
        );
28
29
        $dataKeyValueObjectClass = $this->getParser();
30
        $result = $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
31
32
        $this->assertEquals(
33
            new DatePublished(Query::SORT_ASC),
34
            $result,
35
            'DataKeyValueObjectClass parser not created correctly.'
36
        );
37
    }
38
39
    /**
40
     * Test DataKeyValueObjectClass parser throwing exception on missing sort clause.
41
     *
42
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
43
     * @expectedExceptionMessage The <DatePublished> sort clause doesn't exist in the input structure
44
     */
45
    public function testParseExceptionOnMissingSortClause()
46
    {
47
        $inputArray = array(
48
            'name' => 'Keep on mocking in the free world',
49
        );
50
51
        $dataKeyValueObjectClass = $this->getParser();
52
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
53
    }
54
55
    /**
56
     * Test DataKeyValueObjectClass parser throwing exception on invalid direction format.
57
     *
58
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
59
     * @expectedExceptionMessage Invalid direction format in <DatePublished> sort clause
60
     */
61
    public function testParseExceptionOnInvalidDirectionFormat()
62
    {
63
        $inputArray = array(
64
            'DatePublished' => 'Jailhouse Mock',
65
        );
66
67
        $dataKeyValueObjectClass = $this->getParser();
68
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
69
    }
70
71
    /**
72
     * Test DataKeyValueObjectClass parser throwing exception on nonexisting value object class.
73
     *
74
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
75
     * @expectedExceptionMessage Value object class <eC\Pubish\APl\Repudiatory\BadValues\Discontent\Queezy\SantaClause\ThisClassIsExistentiallyChallenged> is not defined
76
     */
77
    public function testParseExceptionOnNonexistingValueObjectClass()
78
    {
79
        $inputArray = array(
80
            'DatePublished' => Query::SORT_ASC,
81
        );
82
83
        $dataKeyValueObjectClass = new DataKeyValueObjectClass(
84
            'DatePublished',
85
            'eC\Pubish\APl\Repudiatory\BadValues\Discontent\Queezy\SantaClause\ThisClassIsExistentiallyChallenged'
86
        );
87
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
88
    }
89
90
    /**
91
     * Returns the DataKeyValueObjectClass parser.
92
     *
93
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\SortClause\DataKeyValueObjectClass
94
     */
95
    protected function internalGetParser()
96
    {
97
        return new DataKeyValueObjectClass(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...lause\\DatePublished'); (eZ\Publish\Core\REST\Ser...DataKeyValueObjectClass) 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...
98
            'DatePublished',
99
            'eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished'
100
        );
101
    }
102
}
103