Completed
Push — EZP-26146-location-swap-urlali... ( 8c8e66...1d10ee )
by
unknown
327:46 queued 295:37
created

ExceptionTest::testVisit()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
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\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
14
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
15
16
class ExceptionTest extends ValueObjectVisitorBaseTest
17
{
18
    /**
19
     * Test the Exception visitor.
20
     *
21
     * @return string
22
     */
23
    public function testVisit()
24
    {
25
        $visitor = $this->getVisitor();
26
        $generator = $this->getGenerator();
27
28
        $generator->startDocument(null);
29
30
        $previousException = new \Exception('Sub-test');
31
        $exception = new \Exception('Test', 0, $previousException);
32
33
        $this
34
            ->getVisitorMock()
35
            ->expects($this->once())
36
            ->method('visitValueObject')
37
            ->with($previousException);
38
39
        $visitor->visit(
40
            $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...
41
            $generator,
42
            $exception
43
        );
44
45
        $result = $generator->endDocument(null);
46
47
        $this->assertNotNull($result);
48
49
        return $result;
50
    }
51
52
    /**
53
     * Test if result contains ErrorMessage element and error code.
54
     *
55
     * @param string $result
56
     *
57
     * @depends testVisit
58
     */
59
    public function testResultContainsErrorCode($result)
60
    {
61
        $this->assertXMLTag(
62
            array(
63
                'tag' => 'ErrorMessage',
64
                'descendant' => array(
65
                    'tag' => 'errorCode',
66
                    'content' => (string)$this->getExpectedStatusCode(),
67
                ),
68
            ),
69
            $result,
70
            'Invalid <ErrorMessage> element.'
71
        );
72
    }
73
74
    /**
75
     * Test if result contains ErrorMessage element.
76
     *
77
     * @param string $result
78
     *
79
     * @depends testVisit
80
     */
81
    public function testResultContainsErrorMessage($result)
82
    {
83
        $this->assertXMLTag(
84
            array(
85
                'tag' => 'ErrorMessage',
86
                'descendant' => array(
87
                    'tag' => 'errorMessage',
88
                    'content' => $this->getExpectedMessage(),
89
                ),
90
            ),
91
            $result,
92
            'Invalid <ErrorMessage> element.'
93
        );
94
    }
95
96
    /**
97
     * Test if result contains ErrorMessage element and description.
98
     *
99
     * @param string $result
100
     *
101
     * @depends testVisit
102
     */
103
    public function testResultContainsErrorDescription($result)
104
    {
105
        $this->assertXMLTag(
106
            array(
107
                'tag' => 'ErrorMessage',
108
                'descendant' => array(
109
                    'tag' => 'errorDescription',
110
                ),
111
            ),
112
            $result,
113
            'Invalid <ErrorMessage> element.'
114
        );
115
    }
116
117
    /**
118
     * Test if ErrorMessage element contains required attributes.
119
     *
120
     * @param string $result
121
     *
122
     * @depends testVisit
123
     */
124
    public function testResultContainsExceptionAttributes($result)
125
    {
126
        $this->assertXMLTag(
127
            array(
128
                'tag' => 'ErrorMessage',
129
                'attributes' => array(
130
                    'media-type' => 'application/vnd.ez.api.ErrorMessage+xml',
131
                ),
132
            ),
133
            $result,
134
            'Invalid <ErrorMessage> attributes.'
135
        );
136
    }
137
138
    /**
139
     * Test if result contains ErrorMessage element.
140
     *
141
     * @depends testVisit
142
     */
143
    public function testResultContainsPreviousError($result)
144
    {
145
        $dom = new \DOMDocument();
146
        $dom->loadXml($result);
147
148
        $this->assertXPath(
149
            $dom,
150
            '/ErrorMessage/Previous[@media-type="application/vnd.ez.api.ErrorMessage+xml"]'
151
        );
152
    }
153
154
    /**
155
     * Get expected status code.
156
     *
157
     * @return int
158
     */
159
    protected function getExpectedStatusCode()
160
    {
161
        return 500;
162
    }
163
164
    /**
165
     * Get expected message.
166
     *
167
     * @return string
168
     */
169
    protected function getExpectedMessage()
170
    {
171
        return 'Internal Server Error';
172
    }
173
174
    /**
175
     * Gets the exception.
176
     *
177
     * @return \Exception
178
     */
179
    protected function getException()
180
    {
181
        return new \Exception('Test');
182
    }
183
184
    /**
185
     * Gets the exception visitor.
186
     *
187
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception
188
     */
189
    protected function internalGetVisitor()
190
    {
191
        return new ValueObjectVisitor\Exception();
192
    }
193
}
194