EditorResponseListenerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 217
Duplicated Lines 37.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 81
loc 217
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
B testEditorIsNotRenderedOnKernelResponse() 0 37 4
B getOnKernelResponseTestData() 0 45 1
B testEditorIsNotRenderedWhenNotBehindFirewall() 27 27 1
B testEditorIsInjectedOnKernelResponse() 27 27 1
B testCustomEditorInjectionIsPossible() 27 27 1

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
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Extension;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Editor\DefaultEditor;
15
use Ivoaz\Bundle\ContentEditableBundle\Editor\EditorInterface;
16
use Ivoaz\Bundle\ContentEditableBundle\EventListener\EditorResponseListener;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
20
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
21
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
22
23
class EditorResponseListenerTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var EditorResponseListener
27
     */
28
    private $listener;
29
30
    /**
31
     * @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $authorizationChecker;
34
35
    /**
36
     * @var DefaultEditor
37
     */
38
    private $editor;
39
40
    /**
41
     * @var FilterResponseEvent|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $event;
44
45
    public function setUp()
46
    {
47
        $this->editor = $this->getMock(EditorInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48
        $this->authorizationChecker = $this->getMock(AuthorizationCheckerInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
        $this->listener = new EditorResponseListener($this->editor, $this->authorizationChecker);
50
        $this->event = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
            FilterResponseEvent::class,
52
            ['getRequest', 'getResponse', 'isMasterRequest'],
53
            [],
54
            '',
55
            false
56
        );
57
    }
58
59
    /**
60
     * @dataProvider getOnKernelResponseTestData
61
     *
62
     * @param bool   $isMasterRequest
63
     * @param bool   $isXmlHttpRequest
64
     * @param bool   $isRedirection
65
     * @param bool   $isHtml
66
     * @param bool   $isAuthorized
67
     * @param string $message
68
     */
69
    public function testEditorIsNotRenderedOnKernelResponse(
70
        $isMasterRequest,
71
        $isXmlHttpRequest,
72
        $isRedirection,
73
        $isHtml,
74
        $isAuthorized,
75
        $message
76
    ) {
77
        $this->editor->method('renderEditor')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Ivoaz\Bundle\Cont...e\Editor\DefaultEditor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            ->willReturn('<div>editor</div>');
79
80
        $this->authorizationChecker->method('isGranted')
81
            ->with('ROLE_ADMIN')
82
            ->willReturn($isAuthorized);
83
84
        $request = new Request();
85
        if ($isXmlHttpRequest) {
86
            $request->headers->add(['X-Requested-With' => 'XMLHttpRequest']);
87
        }
88
89
        $response = new Response(
90
            '<body></body>',
91
            $isRedirection ? 300 : 200,
92
            ['Content-Type' => $isHtml ? 'text/html' : 'text/plain']
93
        );
94
95
        $this->event->method('getRequest')
96
            ->willReturn($request);
97
        $this->event->method('getResponse')
98
            ->willReturn($response);
99
        $this->event->method('isMasterRequest')
100
            ->willReturn($isMasterRequest);
101
102
        $this->listener->onKernelResponse($this->event);
103
104
        $this->assertSame('<body></body>', $response->getContent(), $message);
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getOnKernelResponseTestData()
111
    {
112
        return [
113
            [
114
                false,
115
                false,
116
                false,
117
                true,
118
                true,
119
                'Editor should not be rendered when it is not master request.',
120
            ],
121
            [
122
                true,
123
                true,
124
                false,
125
                true,
126
                true,
127
                'Editor should not be rendered when it is xml http request.',
128
            ],
129
            [
130
                true,
131
                false,
132
                true,
133
                true,
134
                true,
135
                'Editor should not be rendered on redirection.',
136
            ],
137
            [
138
                true,
139
                false,
140
                false,
141
                false,
142
                true,
143
                'Editor should not be rendered when response is not html.',
144
            ],
145
            [
146
                true,
147
                false,
148
                false,
149
                true,
150
                false,
151
                'Editor should not be rendered when not authorized.',
152
            ],
153
        ];
154
    }
155
156 View Code Duplication
    public function testEditorIsNotRenderedWhenNotBehindFirewall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $this->editor->method('renderEditor')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Ivoaz\Bundle\Cont...e\Editor\DefaultEditor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
            ->willReturn('<div>editor</div>');
160
161
        $this->authorizationChecker->method('isGranted')
162
            ->with('ROLE_ADMIN')
163
            ->willThrowException(new AuthenticationCredentialsNotFoundException());
164
165
        $request = new Request();
166
        $response = new Response(
167
            '<body></body>',
168
            200,
169
            ['Content-Type' => 'text/html']
170
        );
171
172
        $this->event->method('getRequest')
173
            ->willReturn($request);
174
        $this->event->method('getResponse')
175
            ->willReturn($response);
176
        $this->event->method('isMasterRequest')
177
            ->willReturn(true);
178
179
        $this->listener->onKernelResponse($this->event);
180
181
        $this->assertSame('<body></body>', $response->getContent());
182
    }
183
184 View Code Duplication
    public function testEditorIsInjectedOnKernelResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
        $this->editor->method('renderEditor')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Ivoaz\Bundle\Cont...e\Editor\DefaultEditor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
            ->willReturn('<div>editor</div>');
188
189
        $this->authorizationChecker->method('isGranted')
190
            ->with('ROLE_ADMIN')
191
            ->willReturn(true);
192
193
        $request = new Request();
194
        $response = new Response(
195
            '<body><h1>Some content</h1></body>',
196
            200,
197
            ['Content-Type' => 'text/html']
198
        );
199
200
        $this->event->method('getRequest')
201
            ->willReturn($request);
202
        $this->event->method('getResponse')
203
            ->willReturn($response);
204
        $this->event->method('isMasterRequest')
205
            ->willReturn(true);
206
207
        $this->listener->onKernelResponse($this->event);
208
209
        $this->assertSame('<body><h1>Some content</h1><div>editor</div></body>', $response->getContent());
210
    }
211
212 View Code Duplication
    public function testCustomEditorInjectionIsPossible()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        $this->authorizationChecker->method('isGranted')
215
            ->with('ROLE_ADMIN')
216
            ->willReturn(true);
217
218
        $request = new Request();
219
        $response = new Response(
220
            '<body></body>',
221
            200,
222
            ['Content-Type' => 'text/html']
223
        );
224
225
        $this->event->method('getRequest')
226
            ->willReturn($request);
227
        $this->event->method('getResponse')
228
            ->willReturn($response);
229
        $this->event->method('isMasterRequest')
230
            ->willReturn(true);
231
232
        $this->editor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ivoaz\Bundle\Cont...e\Editor\DefaultEditor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
233
            ->method('renderEditor')
234
            ->with($response)
235
            ->willReturn(null);
236
237
        $this->listener->onKernelResponse($this->event);
238
    }
239
}
240