Completed
Push — ezp26001-permissions_lookup_re... ( 2e76c0...5b8460 )
by
unknown
25:19
created

getException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.0856
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\Output\ValueObjectVisitor;
10
11
use eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException as CoreContentFieldValidationException;
12
use eZ\Publish\Core\FieldType\ValidationError;
13
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
14
use eZ\Publish\Core\REST\Server\Exceptions;
15
use Symfony\Component\Translation\Translator;
16
17
class ContentFieldValidationExceptionTest extends ExceptionTest
18
{
19
    /**
20
     * Test the ContentFieldValidationException visitor.
21
     *
22
     * @return string
23
     */
24
    public function testVisit()
25
    {
26
        $visitor = $this->getVisitor();
27
        $generator = $this->getGenerator();
28
29
        $generator->startDocument(null);
30
31
        $exception = $this->getException();
32
33
        $visitor->visit(
34
            $this->getVisitorMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getVisitorMock() targeting eZ\Publish\Core\REST\Com...eTest::getVisitorMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\REST\Com...eObjectVisitor::visit() does only seem to accept object<eZ\Publish\Core\R...\Common\Output\Visitor>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
            $generator,
36
            $exception
37
        );
38
39
        $result = $generator->endDocument(null);
40
41
        $this->assertNotNull($result);
42
43
        return $result;
44
    }
45
46
    /**
47
     * Test if result contains ErrorMessage element and description.
48
     *
49
     * @param string $result
50
     *
51
     * @depends testVisit
52
     */
53
    public function testResultContainsErrorDescription($result)
54
    {
55
        $this->assertXMLTag(
56
            [
57
                'tag' => 'errorDescription',
58
                'content' => $this->getExpectedDescription(),
59
            ],
60
            $result,
61
            'Missing <errorDescription> element.'
62
        );
63
    }
64
65
    /**
66
     * Test if result contains ErrorMessage element and details.
67
     *
68
     * @param string $result
69
     *
70
     * @depends testVisit
71
     */
72
    public function testResultContainsErrorDetails($result)
73
    {
74
        $this->assertXMLTag(
75
            [
76
                'tag' => 'errorDetails',
77
            ],
78
            $result,
79
            'Missing <errorDetails> element.'
80
        );
81
82
        $this->assertXMLTag(
83
            [
84
                'tag' => 'field',
85
            ],
86
            $result,
87
            'Missing <field> element.'
88
        );
89
    }
90
91
    /**
92
     * Get expected status code.
93
     *
94
     * @return int
95
     */
96
    protected function getExpectedStatusCode()
97
    {
98
        return 400;
99
    }
100
101
    /**
102
     * Get expected message.
103
     *
104
     * @return string
105
     */
106
    protected function getExpectedMessage()
107
    {
108
        return 'Bad Request';
109
    }
110
111
    /**
112
     * Get expected description.
113
     *
114
     * @return string
115
     */
116
    protected function getExpectedDescription()
117
    {
118
        return 'Content fields did not validate';
119
    }
120
121
    /**
122
     * Gets the exception.
123
     *
124
     * @return \Exception
125
     */
126
    protected function getException()
127
    {
128
        return new Exceptions\ContentFieldValidationException(
129
            new CoreContentFieldValidationException([
0 ignored issues
show
Documentation introduced by
array(1 => array('eng-GB...ll, array(), 'email'))) is of type array<integer,array<stri...ValidationError>\"}>"}>, but the function expects a array<integer,object<eZ\...dType\ValidationError>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
                1 => [
131
                    'eng-GB' => new ValidationError(
132
                        "Value for required field definition '%identifier%' with language '%languageCode%' is empty",
133
                        null,
134
                        ['%identifier%' => 'name', '%languageCode%' => 'eng-GB'],
135
                        'empty'
136
                    ),
137
                ],
138
                2 => [
139
                    'eng-GB' => new ValidationError(
140
                        'The value must be a valid email address.',
141
                        null,
142
                        [],
143
                        'email'
144
                    ),
145
                ],
146
            ])
147
        );
148
    }
149
150
    /**
151
     * Gets the exception visitor.
152
     *
153
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\ContentFieldValidationException
154
     */
155
    protected function internalGetVisitor()
156
    {
157
        return new ValueObjectVisitor\ContentFieldValidationException(false, new Translator('eng-GB'));
158
    }
159
}
160