Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

UserMetadataTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 30 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 36
loc 120
rs 10
wmc 7
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseProvider() 0 17 1
A testParse() 0 11 1
A testParseExceptionOnInvalidCriterionFormat() 0 9 1
A testParseExceptionOnInvalidTargetFormat() 12 12 1
A testParseExceptionOnInvalidValueFormat() 12 12 1
A testParseExceptionOnWrongValueType() 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
 * 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\Criterion;
12
13
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata as UserMetadataCriterion;
14
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion\UserMetadata;
15
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest;
16
17
class UserMetadataTest extends BaseTest
18
{
19
    public function testParseProvider()
20
    {
21
        return [
22
            [
23
                ['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => 14]],
24
                new UserMetadataCriterion('owner', null, [14]),
25
            ],
26
            [
27
                ['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => '14,15,42']],
28
                new UserMetadataCriterion('owner', null, [14, 15, 42]),
29
            ],
30
            [
31
                ['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => [14, 15, 42]]],
32
                new UserMetadataCriterion('owner', null, [14, 15, 42]),
33
            ],
34
        ];
35
    }
36
37
    /**
38
     * Tests the UserMetadata parser.
39
     *
40
     * @dataProvider testParseProvider
41
     */
42
    public function testParse($data, $expected)
43
    {
44
        $userMetadata = $this->getParser();
45
        $result = $userMetadata->parse($data, $this->getParsingDispatcherMock());
46
47
        $this->assertEquals(
48
            $expected,
49
            $result,
50
            'UserMetadata parser not created correctly.'
51
        );
52
    }
53
54
    /**
55
     * Test UserMetadata parser throwing exception on invalid UserMetadataCriterion format.
56
     *
57
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
58
     * @expectedExceptionMessage Invalid <UserMetadataCriterion> format
59
     */
60
    public function testParseExceptionOnInvalidCriterionFormat()
61
    {
62
        $inputArray = [
63
            'foo' => 'Michael learns to mock',
64
        ];
65
66
        $dataKeyValueObjectClass = $this->getParser();
67
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
68
    }
69
70
    /**
71
     * Test UserMetadata parser throwing exception on invalid target format.
72
     *
73
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
74
     * @expectedExceptionMessage Invalid <Target> format
75
     */
76 View Code Duplication
    public function testParseExceptionOnInvalidTargetFormat()
77
    {
78
        $inputArray = [
79
            'UserMetadataCriterion' => [
80
                'foo' => 'Mock around the clock',
81
                'Value' => 42,
82
            ],
83
        ];
84
85
        $dataKeyValueObjectClass = $this->getParser();
86
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
87
    }
88
89
    /**
90
     * Test UserMetadata parser throwing exception on invalid value format.
91
     *
92
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
93
     * @expectedExceptionMessage Invalid <Value> format
94
     */
95 View Code Duplication
    public function testParseExceptionOnInvalidValueFormat()
96
    {
97
        $inputArray = [
98
            'UserMetadataCriterion' => [
99
                'Target' => 'Moxette',
100
                'foo' => 42,
101
            ],
102
        ];
103
104
        $dataKeyValueObjectClass = $this->getParser();
105
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
106
    }
107
108
    /**
109
     * Test UserMetadata parser throwing exception on wrong type of value format.
110
     *
111
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
112
     * @expectedExceptionMessage Invalid <Value> format
113
     */
114 View Code Duplication
    public function testParseExceptionOnWrongValueType()
115
    {
116
        $inputArray = [
117
            'UserMetadataCriterion' => [
118
                'Target' => 'We will mock you',
119
                'Value' => new \stdClass(),
120
            ],
121
        ];
122
123
        $dataKeyValueObjectClass = $this->getParser();
124
        $dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
125
    }
126
127
    /**
128
     * Returns the UserMetadata criterion parser.
129
     *
130
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\UserMetadata
131
     */
132
    protected function internalGetParser()
133
    {
134
        return new UserMetadata();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...iterion\UserMetadata(); (eZ\Publish\Core\REST\Ser...\Criterion\UserMetadata) 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...
135
    }
136
}
137