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

UserMetadataTest::testParseProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 17
rs 9.4285
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