Completed
Push — ezp26472-rest_previous_excepti... ( 95062a...d37d9c )
by
unknown
72:18 queued 35:52
created

ExceptionTest::testResultContainsPreviousError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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