Completed
Push — master ( 9c5fdf...27a5d2 )
by John
7s
created

setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.8571
c 4
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php declare(strict_types = 1);
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\DocumentRepository;
13
use KleijnWeb\SwaggerBundle\Document\Specification;
14
use KleijnWeb\SwaggerBundle\Document\Specification\Operation;
15
use KleijnWeb\SwaggerBundle\Request\ContentDecoder;
16
use KleijnWeb\SwaggerBundle\Serialize\SerializationTypeResolver;
17
use KleijnWeb\SwaggerBundle\Serialize\Serializer\Factory\JmsSerializerFactory;
18
use KleijnWeb\SwaggerBundle\Serialize\Serializer\JmsSerializerAdapter;
19
use KleijnWeb\SwaggerBundle\Tests\Request\TestRequestFactory;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * @author John Kleijn <[email protected]>
24
 */
25
class ContentDecoderJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var ContentDecoder
29
     */
30
    private $contentDecoder;
31
32
    /**
33
     * @var Serializer
34
     */
35
    private $serializer;
36
37
    /**
38
     * Create serializer
39
     */
40
    protected function setUp()
41
    {
42
        $this->serializer = new JmsSerializerAdapter(JmsSerializerFactory::factory());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \KleijnWeb\SwaggerBu...izerFactory::factory()) of type object<KleijnWeb\Swagger...r\JmsSerializerAdapter> 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...
43
44
        $typeResolver = $this
45
            ->getMockBuilder(SerializationTypeResolver::class)
46
            ->disableOriginalConstructor()
47
            ->getMock();
48
49
        $typeResolver
50
            ->expects($this->any())
51
            ->method('resolveOperationBodyType')
52
            ->willReturn(JmsAnnotatedResourceStub::class);
53
54
        $documentRepository = $this
55
            ->getMockBuilder(DocumentRepository::class)
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
59
        $documentRepository
60
            ->expects($this->any())
61
            ->method('get')
62
            ->willReturn(new Specification(new \stdClass));
63
64
        /** @noinspection PhpParamsInspection */
65
        $this->contentDecoder = new ContentDecoder($this->serializer, $documentRepository, $typeResolver);
66
    }
67
68
    /**
69
     * @test
70
     */
71 View Code Duplication
    public function canDeserializeIntoObject()
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...
72
    {
73
        $content = [
74
            'foo' => 'bar'
75
        ];
76
        $request = TestRequestFactory::create(json_encode($content), [], 'faux');
77
        $request->headers->set('Content-Type', 'application/json');
78
79
        $operationDefinition = (object)[
80
            'parameters' => [
81
                (object)[
82
                    "in"     => "body",
83
                    "name"   => "body",
84
                    "schema" => (object)[
85
                        '$ref' => "#/definitions/JmsAnnotatedResourceStub"
86
                    ]
87
                ]
88
            ]
89
        ];
90
91
        $operationObject = Operation::createFromOperationDefinition((object)$operationDefinition);
92
93
        $actual = $this->contentDecoder->decodeContent($request, $operationObject);
94
95
        $expected = (new JmsAnnotatedResourceStub)->setFoo('bar');
96
97
        $this->assertEquals($expected, $actual);
98
    }
99
100
    /**
101
     * @test
102
     *
103
     * @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException
104
     */
105 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...
106
    {
107
        $content = 'lkjhlkj';
108
        $request = TestRequestFactory::create($content, [], 'faux');
109
        $request->headers->set('Content-Type', 'application/json');
110
111
        $operationObject = Operation::createFromOperationDefinition((object)[]);
112
        $this->contentDecoder->decodeContent($request, $operationObject);
113
    }
114
115
    /**
116
     * @test
117
     * @dataProvider contentTypeProvider
118
     *
119
     * @param string $contentType
120
     */
121 View Code Duplication
    public function willAlwaysDecodeJson($contentType)
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...
122
    {
123
        $content = '{ "foo": "bar" }';
124
        $request = TestRequestFactory::create($content, [], 'faux');
125
        $request->headers->set('Content-Type', $contentType);
126
127
        $operationDefinition = (object)[
128
            'parameters' => [
129
                (object)[
130
                    "in"     => "body",
131
                    "name"   => "body",
132
                    "schema" => (object)[
133
                        '$ref' => "#/definitions/JmsAnnotatedResourceStub"
134
                    ]
135
                ]
136
            ]
137
        ];
138
139
        $operationObject = Operation::createFromOperationDefinition((object)$operationDefinition);
140
141
        $actual   = $this->contentDecoder->decodeContent($request, $operationObject);
142
        $expected = (new JmsAnnotatedResourceStub)->setFoo('bar');
143
        $this->assertEquals($expected, $actual);
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public static function contentTypeProvider()
150
    {
151
        return [
152
            ['application/json'],
153
            ['application/vnd.api+json']
154
        ];
155
    }
156
}
157