Completed
Push — master ( 5f67eb...e271d9 )
by
unknown
48:48 queued 23:12
created

FieldTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 12
loc 69
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testParse() 0 18 1
A testParseExceptionOnMissingSortClause() 0 9 1
A testParseExceptionOnInvalidDirectionFormat() 12 12 1
A internalGetParser() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\SortClause;
8
9
use eZ\Publish\API\Repository\Values\Content\Query;
10
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Field;
11
use eZ\Publish\Core\REST\Server\Input\Parser\SortClause\Field as FieldParser;
12
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest;
13
14
class FieldTest extends BaseTest
15
{
16
    /**
17
     * Tests the Field parser.
18
     */
19
    public function testParse()
20
    {
21
        $inputArray = [
22
            'Field' => [
23
                'identifier' => 'content/field',
24
                'direction' => Query::SORT_ASC,
25
            ],
26
        ];
27
28
        $fieldParser = $this->getParser();
29
        $result = $fieldParser->parse($inputArray, $this->getParsingDispatcherMock());
30
31
        $this->assertEquals(
32
            new Field('content', 'field', Query::SORT_ASC),
33
            $result,
34
            'Field parser not created correctly.'
35
        );
36
    }
37
38
    /**
39
     * Test Field parser throwing exception on missing sort clause.
40
     *
41
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
42
     * @expectedExceptionMessage The <Field> sort clause doesn't exist in the input structure
43
     */
44
    public function testParseExceptionOnMissingSortClause()
45
    {
46
        $inputArray = [
47
            'name' => 'Keep on mocking in the free world',
48
        ];
49
50
        $fieldParser = $this->getParser();
51
        $fieldParser->parse($inputArray, $this->getParsingDispatcherMock());
52
    }
53
54
    /**
55
     * Test Field parser throwing exception on invalid direction format.
56
     *
57
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
58
     * @expectedExceptionMessage Invalid direction format in <Field> sort clause
59
     */
60 View Code Duplication
    public function testParseExceptionOnInvalidDirectionFormat()
61
    {
62
        $inputArray = [
63
            'Field' => [
64
                'identifier' => 'content/field',
65
                'direction' => 'mock',
66
            ],
67
        ];
68
69
        $fieldParser = $this->getParser();
70
        $fieldParser->parse($inputArray, $this->getParsingDispatcherMock());
71
    }
72
73
    /**
74
     * Returns the Field parser.
75
     *
76
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\SortClause\Field
77
     */
78
    protected function internalGetParser()
79
    {
80
        return new FieldParser();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...ser\SortClause\Field(); (eZ\Publish\Core\REST\Ser...Parser\SortClause\Field) 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...
81
    }
82
}
83