Completed
Push — ezp24853-enhance_rest_when_fai... ( 8760d4...d8be19 )
by
unknown
22:12
created

testResultContainsErrorDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
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\Output\ValueObjectVisitor;
12
13
use eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException as CoreContentFieldValidationException;
14
use eZ\Publish\Core\FieldType\ValidationError;
15
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
16
use eZ\Publish\Core\REST\Server\Exceptions;
17
use Symfony\Component\Translation\Translator;
18
19
class ContentFieldValidationExceptionTest extends ExceptionTest
20
{
21
    /**
22
     * Test the ContentFieldValidationException visitor.
23
     *
24
     * @return string
25
     */
26
    public function testVisit()
27
    {
28
        $visitor = $this->getVisitor();
29
        $generator = $this->getGenerator();
30
31
        $generator->startDocument(null);
32
33
        $exception = $this->getException();
34
35
        $visitor->visit(
36
            $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...
37
            $generator,
38
            $exception
39
        );
40
41
        $result = $generator->endDocument(null);
42
43
        $this->assertNotNull($result);
44
45
        return $result;
46
    }
47
48
    /**
49
     * Test if result contains ErrorMessage element and description.
50
     *
51
     * @param string $result
52
     *
53
     * @depends testVisit
54
     */
55
    public function testResultContainsErrorDescription($result)
56
    {
57
        $this->assertXMLTag(
58
            [
59
                'tag' => 'errorDescription',
60
                'content' => $this->getExpectedDescription(),
61
            ],
62
            $result,
63
            'Missing <errorDescription> element.'
64
        );
65
    }
66
67
    /**
68
     * Test if result contains ErrorMessage element and details.
69
     *
70
     * @param string $result
71
     *
72
     * @depends testVisit
73
     */
74
    public function testResultContainsErrorDetails($result)
75
    {
76
        $this->assertXMLTag(
77
            [
78
                'tag' => 'errorDetails',
79
            ],
80
            $result,
81
            'Missing <errorDetails> element.'
82
        );
83
84
        $this->assertXMLTag(
85
            [
86
                'tag' => 'fields',
87
            ],
88
            $result,
89
            'Missing <fields> element.'
90
        );
91
    }
92
93
    /**
94
     * Get expected status code.
95
     *
96
     * @return int
97
     */
98
    protected function getExpectedStatusCode()
99
    {
100
        return 400;
101
    }
102
103
    /**
104
     * Get expected message.
105
     *
106
     * @return string
107
     */
108
    protected function getExpectedMessage()
109
    {
110
        return 'Bad Request';
111
    }
112
113
    /**
114
     * Get expected description.
115
     *
116
     * @return string
117
     */
118
    protected function getExpectedDescription()
119
    {
120
        return 'Content fields did not validate';
121
    }
122
123
    /**
124
     * Gets the exception.
125
     *
126
     * @return \Exception
127
     */
128
    protected function getException()
129
    {
130
        return new Exceptions\ContentFieldValidationException(
131
            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...
132
                1 => [
133
                    'eng-GB' => new ValidationError(
134
                        "Value for required field definition '%identifier%' with language '%languageCode%' is empty",
135
                        null,
136
                        ['%identifier%' => 'name', '%languageCode%' => 'eng-GB'],
137
                        'empty'
138
                    ),
139
                ],
140
                2 => [
141
                    'eng-GB' => new ValidationError(
142
                        'The value must be a valid email address.',
143
                        null,
144
                        [],
145
                        'email'
146
                    ),
147
                ],
148
            ])
149
        );
150
    }
151
152
    /**
153
     * Gets the exception visitor.
154
     *
155
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\ContentFieldValidationException
156
     */
157
    protected function internalGetVisitor()
158
    {
159
        return new ValueObjectVisitor\ContentFieldValidationException(false, new Translator('eng-GB'));
160
    }
161
}
162