Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

ResourceLinkTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 255
Duplicated Lines 31.37 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 80
loc 255
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

10 Methods

Rating   Name   Duplication   Size   Complexity  
B testVisitWithExpansion() 0 45 1
B testVisitWithNoExpansion() 0 38 1
B testVisitUnauthorizedException() 40 40 1
B testVisitMultipleLoadException() 40 40 1
A testVisitCircularLoadException() 0 4 1
A buildValueObject() 0 6 1
A internalGetVisitor() 0 8 1
A getValueLoaderMock() 0 10 2
A getPathExpansionCheckerMock() 0 10 2
A getValueObjectVisitorDispatcherMock() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor;
7
8
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
9
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
10
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\ResourceLink;
11
use eZ\Publish\Core\REST\Server\Output\PathExpansion\Exceptions\MultipleValueLoadException;
12
use eZ\Publish\Core\REST\Server\Values\ResourceLink as ResourceLinkValue;
13
14
class ResourceLinkTest extends ValueObjectVisitorBaseTest
15
{
16
    private $valueLoaderMock;
17
    private $pathExpansionCheckerMock;
18
    private $valueObjectVisitorDispatcherMock;
19
20
    /**
21
     * @param ResourceLinkValue $resourceLink
22
     * @dataProvider buildValueObject
23
     */
24
    public function testVisitWithExpansion(ResourceLinkValue $resourceLink)
25
    {
26
        $visitor = $this->getVisitor();
27
        $generator = $this->getGenerator();
28
29
        $generator->startDocument(null);
30
        $generator->startObjectElement('SomeRoot');
31
        $generator->startObjectElement('SomeObject');
32
33
        $this->getPathExpansionCheckerMock()
34
            ->expects($this->once())
35
            ->method('needsExpansion')
36
            ->with('SomeRoot.SomeObject')
37
            ->will($this->returnValue(true));
38
39
        $this->getValueLoaderMock()
40
            ->expects($this->once())
41
            ->method('load')
42
            ->with($resourceLink->link, $resourceLink->mediaType)
43
            ->will($this->returnValue($loadedValue = new \stdClass()));
44
45
        $this->getValueObjectVisitorDispatcherMock()
46
            ->expects($this->once())
47
            ->method('visit')
48
            ->with(
49
                $loadedValue,
50
                $this->isInstanceOf('eZ\Publish\Core\REST\Server\Output\PathExpansion\ExpansionGenerator'),
51
                $this->getVisitorMock()
52
            );
53
54
        $visitor->visit($this->getVisitorMock(), $generator, $resourceLink);
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...
55
56
        $generator->endObjectElement('SomeObject');
57
        $generator->endObjectElement('SomeRoot');
58
59
        $result = $generator->endDocument(null);
60
61
        $this->assertNotNull($result);
62
63
        $dom = new \DOMDocument();
64
        $dom->loadXml($result);
65
66
        $this->assertXPath($dom, '//SomeObject[@href="' . $resourceLink->link . '"]');
67
        $this->assertXPath($dom, '//SomeObject[@media-type="application/vnd.ez.api.SomeObject+xml"]');
68
    }
69
70
    /**
71
     * @param ResourceLinkValue $resourceLink
72
     * @dataProvider buildValueObject
73
     */
74
    public function testVisitWithNoExpansion(ResourceLinkValue $resourceLink)
75
    {
76
        $visitor = $this->getVisitor();
77
        $generator = $this->getGenerator();
78
79
        $generator->startDocument(null);
80
        $generator->startObjectElement('SomeRoot');
81
        $generator->startObjectElement('SomeObject');
82
83
        $this->getPathExpansionCheckerMock()
84
            ->expects($this->once())
85
            ->method('needsExpansion')
86
            ->with('SomeRoot.SomeObject')
87
            ->will($this->returnValue(false));
88
89
        $this->getValueLoaderMock()
90
            ->expects($this->never())
91
            ->method('load');
92
93
        $this->getValueObjectVisitorDispatcherMock()
94
            ->expects($this->never())
95
            ->method('visit');
96
97
        $visitor->visit($this->getVisitorMock(), $generator, $resourceLink);
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...
98
99
        $generator->endObjectElement('SomeObject');
100
        $generator->endObjectElement('SomeRoot');
101
102
        $result = $generator->endDocument(null);
103
104
        $this->assertNotNull($result);
105
106
        $dom = new \DOMDocument();
107
        $dom->loadXml($result);
108
109
        $this->assertXPath($dom, '//SomeObject[@href="' . $resourceLink->link . '"]');
110
        $this->assertXPath($dom, '//SomeObject[@media-type="application/vnd.ez.api.SomeObject+xml"]');
111
    }
112
113
    /**
114
     * @dataProvider buildValueObject
115
     */
116 View Code Duplication
    public function testVisitUnauthorizedException(ResourceLinkValue $resourceLink)
117
    {
118
        $visitor = $this->getVisitor();
119
        $generator = $this->getGenerator();
120
121
        $generator->startDocument(null);
122
        $generator->startObjectElement('SomeRoot');
123
        $generator->startObjectElement('SomeObject');
124
125
        $this->getPathExpansionCheckerMock()
126
            ->expects($this->once())
127
            ->method('needsExpansion')
128
            ->with('SomeRoot.SomeObject')
129
            ->will($this->returnValue(true));
130
131
        $this->getValueLoaderMock()
132
            ->expects($this->once())
133
            ->method('load')
134
            ->will($this->throwException(new UnauthorizedException('something', 'load')));
135
136
        $this->getValueObjectVisitorDispatcherMock()
137
            ->expects($this->never())
138
            ->method('visit');
139
140
        $visitor->visit($this->getVisitorMock(), $generator, $resourceLink);
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...
141
142
        $generator->endObjectElement('SomeObject');
143
        $generator->endObjectElement('SomeRoot');
144
145
        $result = $generator->endDocument(null);
146
147
        $this->assertNotNull($result);
148
149
        $dom = new \DOMDocument();
150
        $dom->loadXml($result);
151
152
        $this->assertXPath($dom, '//SomeObject[@href="' . $resourceLink->link . '"]');
153
        $this->assertXPath($dom, '//SomeObject[@media-type="application/vnd.ez.api.SomeObject+xml"]');
154
        $this->assertXPath($dom, '//SomeObject[@embed-error="User does not have access to \'load\' \'something\'"]');
155
    }
156
157
    /**
158
     * @dataProvider buildValueObject
159
     */
160 View Code Duplication
    public function testVisitMultipleLoadException(ResourceLinkValue $resourceLink)
161
    {
162
        $visitor = $this->getVisitor();
163
        $generator = $this->getGenerator();
164
165
        $generator->startDocument(null);
166
        $generator->startObjectElement('SomeRoot');
167
        $generator->startObjectElement('SomeObject');
168
169
        $this->getPathExpansionCheckerMock()
170
            ->expects($this->once())
171
            ->method('needsExpansion')
172
            ->with('SomeRoot.SomeObject')
173
            ->will($this->returnValue(true));
174
175
        $this->getValueLoaderMock()
176
            ->expects($this->once())
177
            ->method('load')
178
            ->will($this->throwException(new MultipleValueLoadException()));
179
180
        $this->getValueObjectVisitorDispatcherMock()
181
            ->expects($this->never())
182
            ->method('visit');
183
184
        $visitor->visit($this->getVisitorMock(), $generator, $resourceLink);
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...
185
186
        $generator->endObjectElement('SomeObject');
187
        $generator->endObjectElement('SomeRoot');
188
189
        $result = $generator->endDocument(null);
190
191
        $this->assertNotNull($result);
192
193
        $dom = new \DOMDocument();
194
        $dom->loadXml($result);
195
196
        $this->assertXPath($dom, '//SomeObject[@href="' . $resourceLink->link . '"]');
197
        $this->assertXPath($dom, '//SomeObject[@media-type="application/vnd.ez.api.SomeObject+xml"]');
198
        $this->assertXPath($dom, '//SomeObject[@embed-error="Value was already loaded"]');
199
    }
200
201
    public function testVisitCircularLoadException()
202
    {
203
        self::markTestIncomplete('@todo Implement feature');
204
    }
205
206
    public function buildValueObject()
207
    {
208
        return [
209
            [new ResourceLinkValue('/api/ezp/v2/resource')],
210
        ];
211
    }
212
213
    /**
214
     * Must return an instance of the tested visitor object.
215
     *
216
     * @return \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor
217
     */
218
    protected function internalGetVisitor()
219
    {
220
        return new ResourceLink(
221
            $this->getValueLoaderMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getValueLoaderMock() targeting eZ\Publish\Core\REST\Ser...t::getValueLoaderMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\REST\Ser...urceLink::__construct() does only seem to accept object<eZ\Publish\Core\R...Loaders\UriValueLoader>, 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...
222
            $this->getPathExpansionCheckerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPathExpansionCheckerMock() targeting eZ\Publish\Core\REST\Ser...hExpansionCheckerMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\REST\Ser...urceLink::__construct() does only seem to accept object<eZ\Publish\Core\R...n\PathExpansionChecker>, 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...
223
            $this->getValueObjectVisitorDispatcherMock()
0 ignored issues
show
Bug introduced by
It seems like $this->getValueObjectVisitorDispatcherMock() targeting eZ\Publish\Core\REST\Ser...VisitorDispatcherMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\REST\Ser...urceLink::__construct() does only seem to accept object<eZ\Publish\Core\R...bjectVisitorDispatcher>, 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...
224
        );
225
    }
226
227
    /**
228
     * @return \eZ\Publish\Core\REST\Server\Output\PathExpansion\ValueLoaders\UriValueLoader|\PHPUnit_Framework_MockObject_MockObject
229
     */
230
    protected function getValueLoaderMock()
231
    {
232
        if ($this->valueLoaderMock === null) {
233
            $this->valueLoaderMock = $this
234
                ->getMockBuilder('eZ\Publish\Core\REST\Server\Output\PathExpansion\ValueLoaders\UriValueLoader')
235
                ->getMock();
236
        }
237
238
        return $this->valueLoaderMock;
239
    }
240
241
    /**
242
     * @return \eZ\Publish\Core\REST\Server\Output\PathExpansion\PathExpansionChecker|\PHPUnit_Framework_MockObject_MockObject
243
     */
244
    protected function getPathExpansionCheckerMock()
245
    {
246
        if ($this->pathExpansionCheckerMock === null) {
247
            $this->pathExpansionCheckerMock = $this
248
                ->getMockBuilder('eZ\Publish\Core\REST\Server\Output\PathExpansion\PathExpansionChecker')
249
                ->getMock();
250
        }
251
252
        return $this->pathExpansionCheckerMock;
253
    }
254
255
    /**
256
     * @return \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher|\PHPUnit_Framework_MockObject_MockObject
257
     */
258
    protected function getValueObjectVisitorDispatcherMock()
259
    {
260
        if ($this->valueObjectVisitorDispatcherMock === null) {
261
            $this->valueObjectVisitorDispatcherMock = $this
262
                ->getMockBuilder('eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher')
263
                ->getMock();
264
        }
265
266
        return $this->valueObjectVisitorDispatcherMock;
267
    }
268
}
269