AutoJsonResponseListenerTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 17.6 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 2
dl 22
loc 125
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnKernelViewWithNonMasterRequest() 0 6 1
A testOnKernelViewWithNonJsonRequest() 0 6 1
A testOnKernelViewWithNullControllerResult() 0 7 1
A testOnKernelViewWithResponseControllerResult() 0 6 1
A testOnKernelViewWithObjectControllerResultButNoSerializer() 0 7 1
A testOnKernelViewWithObjectControllerResult() 11 11 1
A testOnKernelViewWithPostRequestAndObjectControllerResult() 11 11 1
B prophesizeEvent() 0 26 6
A doTest() 0 5 2
A prophesizeSerializer() 0 7 1
A testOnKernelViewWithScalarControllerResult() 0 8 1
A testOnKernelViewWithPostRequestAndNonObjectControllerResult() 0 8 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 ChrisyueAutoJsonResponseBundle package.
5
 *
6
 * (c) Chrisyue <http://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\Bundle\AutoJsonResponseBundle\Tests\EventListener;
13
14
use Chrisyue\Bundle\AutoJsonResponseBundle\EventListener\AutoJsonResponseListener;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
20
use Symfony\Component\Serializer\Serializer;
21
22
class AutoJsonResponseListenerTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testOnKernelViewWithNonMasterRequest()
25
    {
26
        $event = $this->prophesizeEvent(false);
27
28
        $this->doTest($event);
29
    }
30
31
    public function testOnKernelViewWithNonJsonRequest()
32
    {
33
        $event = $this->prophesizeEvent(true);
34
35
        $this->doTest($event);
36
    }
37
38
    public function testOnKernelViewWithNullControllerResult()
39
    {
40
        $event = $this->prophesizeEvent(true, true, null);
41
        $event->setResponse(new JsonResponse(null, 204))->shouldBeCalledTimes(1);
42
43
        $this->doTest($event);
44
    }
45
46
    public function testOnKernelViewWithResponseControllerResult()
47
    {
48
        $event = $this->prophesizeEvent(true, true, new JsonResponse());
49
50
        $this->doTest($event);
51
    }
52
53
    public function testOnKernelViewWithScalarControllerResult()
54
    {
55
        $nonObject = 'foo';
56
        $event = $this->prophesizeEvent(true, true, $nonObject);
57
        $event->setResponse(new JsonResponse($nonObject))->shouldBeCalledTimes(1);
58
59
        $this->doTest($event);
60
    }
61
62
    public function testOnKernelViewWithPostRequestAndNonObjectControllerResult()
63
    {
64
        $nonObject = 'hello world';
65
        $event = $this->prophesizeEvent(true, true, $nonObject, true);
66
        $event->setResponse(new JsonResponse($nonObject, 201))->shouldBeCalledTimes(1);
67
68
        $this->doTest($event);
69
    }
70
71
    /**
72
     * @expectedException BadMethodCallException
73
     */
74
    public function testOnKernelViewWithObjectControllerResultButNoSerializer()
75
    {
76
        $object = new \stdClass();
77
        $event = $this->prophesizeEvent(true, true, $object);
78
79
        $this->doTest($event);
80
    }
81
82 View Code Duplication
    public function testOnKernelViewWithObjectControllerResult()
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...
83
    {
84
        $object = new \stdClass();
85
        $event = $this->prophesizeEvent(true, true, $object);
86
87
        $normalized = ['foo' => 'bar'];
88
        $serializer = $this->prophesizeSerializer($object, $normalized);
89
        $event->setResponse(new JsonResponse($normalized))->shouldBeCalledTimes(1);
90
91
        $this->doTest($event, $serializer);
92
    }
93
94 View Code Duplication
    public function testOnKernelViewWithPostRequestAndObjectControllerResult()
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...
95
    {
96
        $object = new \stdClass();
97
        $event = $this->prophesizeEvent(true, true, $object, true);
98
99
        $normalized = ['bar' => 'foo'];
100
        $serializer = $this->prophesizeSerializer($object, $normalized);
101
        $event->setResponse(new JsonResponse($normalized, 201))->shouldBeCalledTimes(1);
102
103
        $this->doTest($event, $serializer);
104
    }
105
106
    private function prophesizeEvent($isMasterRequest, $isRequestJson = false, $controllerResult = null, $isMethodPost = false)
107
    {
108
        $event = $this->prophesize(GetResponseForControllerResultEvent::class);
109
        $event->isMasterRequest()->shouldBeCalledTimes(1)->willReturn($isMasterRequest);
110
111
        if (!$isMasterRequest) {
112
            return $event;
113
        }
114
115
        $request = $this->prophesize(Request::class);
116
        $request->getRequestFormat()->shouldBeCalledTimes(1)->willReturn($isRequestJson ? 'json' : '!json');
117
        $event->getRequest()->shouldBeCalledTimes(1)->willReturn($request->reveal());
118
119
        if (!$isRequestJson) {
120
            return $event;
121
        }
122
123
        $event->getControllerResult()->shouldBeCalledTimes(1)->willReturn($controllerResult);
124
        if ($controllerResult instanceof Response || null === $controllerResult) {
125
            return $event;
126
        }
127
128
        $request->isMethod('POST')->shouldBeCalledTimes(1)->willReturn($isMethodPost);
129
130
        return $event;
131
    }
132
133
    private function doTest(ObjectProphecy $event, ObjectProphecy $serializer = null)
134
    {
135
        $listener = new AutoJsonResponseListener(null === $serializer ? null : $serializer->reveal(), ['serialization_default_groups' => null]);
136
        $listener->onKernelView($event->reveal());
137
    }
138
139
    private function prophesizeSerializer(\stdClass $object, array $normalized)
140
    {
141
        $serializer = $this->prophesize(Serializer::class);
142
        $serializer->normalize($object, null, ['groups' => null])->shouldBeCalledTimes(1)->willReturn($normalized);
143
144
        return $serializer;
145
    }
146
}
147