Completed
Pull Request — master (#64)
by John
02:51
created

willAlwaysDecodeJson()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 1
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\ContentDecoder;
10
11
use JMS\Serializer\Serializer;
12
use KleijnWeb\SwaggerBundle\Document\OperationObject;
13
use KleijnWeb\SwaggerBundle\Request\ContentDecoder;
14
use KleijnWeb\SwaggerBundle\Serializer\JmsSerializerFactory;
15
use KleijnWeb\SwaggerBundle\Serializer\SerializationTypeResolver;
16
use KleijnWeb\SwaggerBundle\Serializer\SerializerAdapter;
17
use KleijnWeb\SwaggerBundle\Tests\Request\TestRequestFactory;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author John Kleijn <[email protected]>
22
 */
23
class ContentDecoderJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var ContentDecoder
27
     */
28
    private $contentDecoder;
29
30
    /**
31
     * @var Serializer
32
     */
33
    private $serializer;
34
35
    /**
36
     * Create serializer
37
     */
38
    protected function setUp()
39
    {
40
        $this->serializer = new SerializerAdapter(JmsSerializerFactory::factory());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \KleijnWeb\SwaggerBu...izerFactory::factory()) of type object<KleijnWeb\Swagger...izer\SerializerAdapter> is incompatible with the declared type object<JMS\Serializer\Serializer> of property $serializer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->contentDecoder = new ContentDecoder(
42
            $this->serializer,
43
            new SerializationTypeResolver('KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder')
44
        );
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function canDeserializeIntoObject()
51
    {
52
        $content = [
53
            'foo' => 'bar'
54
        ];
55
        $request = new Request([], [], [], [], [], [], json_encode($content));
56
        $request->headers->set('Content-Type', 'application/json');
57
58
59
        $operationDefinition = (object)[
60
            'parameters' => [
61
                (object)[
62
                    "in"     => "body",
63
                    "name"   => "body",
64
                    "schema" => (object)[
65
                        '$ref' => "#/definitions/JmsAnnotatedResourceStub"
66
                    ]
67
                ]
68
            ]
69
        ];
70
71
        $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition);
72
73
        $actual = $this->contentDecoder->decodeContent($request, $operationObject);
74
75
        $className = 'KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder\JmsAnnotatedResourceStub';
76
        $expected = (new $className)->setFoo('bar');
77
78
        $this->assertEquals($expected, $actual);
79
    }
80
81
    /**
82
     * @test
83
     *
84
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException
85
     */
86 View Code Duplication
    public function willThrowMalformedContentExceptionWhenDecodingFails()
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...
87
    {
88
        $content = 'lkjhlkj';
89
        $request = TestRequestFactory::create($content);
90
        $request->headers->set('Content-Type', 'application/json');
91
92
        $operationObject = OperationObject::createFromOperationDefinition((object)[]);
93
        $this->contentDecoder->decodeContent($request, $operationObject);
94
    }
95
96
    /**
97
     * @test
98
     * @dataProvider contentTypeProvider
99
     *
100
     * @param string $contentType
101
     */
102
    public function willAlwaysDecodeJson($contentType)
103
    {
104
        $content = '{ "foo": "bar" }';
105
        $request = TestRequestFactory::create($content);
106
        $request->headers->set('Content-Type', $contentType);
107
108
        $operationDefinition = (object)[
109
            'parameters' => [
110
                (object)[
111
                    "in"     => "body",
112
                    "name"   => "body",
113
                    "schema" => (object)[
114
                        '$ref' => "#/definitions/JmsAnnotatedResourceStub"
115
                    ]
116
                ]
117
            ]
118
        ];
119
120
        $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition);
121
122
        $actual = $this->contentDecoder->decodeContent($request, $operationObject);
123
        $className = 'KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder\JmsAnnotatedResourceStub';
124
        $expected = (new $className)->setFoo('bar');
125
        $this->assertEquals($expected, $actual);
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public static function contentTypeProvider()
132
    {
133
        return [
134
            ['application/json'],
135
            ['application/vnd.api+json']
136
        ];
137
    }
138
}
139