Completed
Pull Request — master (#69)
by Cethy
02:54
created

RequestCoercerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 95
Duplicated Lines 13.68 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 4
c 5
b 0
f 1
lcom 1
cbo 8
dl 13
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A willAddDecodedContentAsAttribute() 0 23 1
B willConstructDate() 0 26 1
A willPassWithoutParameters() 13 13 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
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Request;
10
11
use KleijnWeb\SwaggerBundle\Document\OperationObject;
12
use KleijnWeb\SwaggerBundle\Request\ContentDecoder;
13
use KleijnWeb\SwaggerBundle\Request\RequestCoercer;
14
use KleijnWeb\SwaggerBundle\Request\ParameterCoercer;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class RequestCoercerTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var ContentDecoder
24
     */
25
    private $contentDecoderMock;
26
27
    protected function setUp()
28
    {
29
        $this->contentDecoderMock = $this
30
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Request\ContentDecoder')
31
            ->disableOriginalConstructor()
32
            ->getMock();
33
        $this->contentDecoderMock
34
            ->expects($this->any())
35
            ->method('decodeContent')
36
            ->willReturnCallback(function (Request $request) {
37
                return json_decode($request->getContent());
38
            });
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function willAddDecodedContentAsAttribute()
45
    {
46
        $coercer = new RequestCoercer($this->contentDecoderMock);
47
        $content = '[1,2,3,4]';
48
        $request = TestRequestFactory::create($content);
49
50
        $operationDefinition = (object)[
51
            'parameters' => [
52
                (object)[
53
                    'name'   => 'myContent',
54
                    'in'     => 'body',
55
                    'schema' => (object)[
56
                        'type' => 'array'
57
                    ]
58
                ]
59
            ]
60
        ];
61
62
        $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition);
63
        $coercer->coerceRequest($request, $operationObject);
64
65
        $this->assertSame([1, 2, 3, 4], $request->attributes->get('myContent'));
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function willConstructDate()
72
    {
73
        $coercer = new RequestCoercer($this->contentDecoderMock);
74
        $request = TestRequestFactory::create(null, ['foo' => "2015-01-01"]);
75
76
        $operationDefinition = (object)[
77
            'parameters' => [
78
                (object)[
79
                    'name'   => 'foo',
80
                    'in'     => 'query',
81
                    'type'   => 'string',
82
                    'format' => 'date'
83
                ]
84
            ]
85
        ];
86
87
        $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition);
88
        $coercer->coerceRequest($request, $operationObject);
89
90
        $expected = ParameterCoercer::coerceParameter($operationDefinition->parameters[0], "2015-01-01");
91
92
        // Sanity check
93
        $this->assertInstanceOf('DateTime', $expected);
94
95
        $this->assertEquals($expected, $request->attributes->get('foo'));
96
    }
97
98
    /**
99
     * @test
100
     */
101 View Code Duplication
    public function willPassWithoutParameters()
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...
102
    {
103
        $coercer = new RequestCoercer($this->contentDecoderMock);
104
        $content = '[1,2,3]';
105
        $request = TestRequestFactory::create($content);
106
107
        $operationDefinition = (object)[];
108
109
        $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition);
110
        $coercer->coerceRequest($request, $operationObject);
111
112
        $this->assertSame([1, 2, 3], $request->attributes->get('myContent'));
113
    }
114
}
115