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 KleijnWeb\SwaggerBundle\Document\DocumentRepository; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification\Operation; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Request\ContentDecoder; |
15
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer; |
16
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer\ArraySerializer; |
17
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Request\TestRequestFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author John Kleijn <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ContentDecoderTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContentDecoder |
26
|
|
|
*/ |
27
|
|
|
private $contentDecoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ArraySerializer |
31
|
|
|
*/ |
32
|
|
|
private $serializer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set content decoder with default serializer |
36
|
|
|
*/ |
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
$documentRepository = $this |
40
|
|
|
->getMockBuilder(DocumentRepository::class) |
41
|
|
|
->disableOriginalConstructor() |
42
|
|
|
->getMock(); |
43
|
|
|
|
44
|
|
|
$documentRepository |
45
|
|
|
->expects($this->any()) |
46
|
|
|
->method('get') |
47
|
|
|
->willReturn(new Specification(new \stdClass)); |
48
|
|
|
|
49
|
|
|
$this->serializer = $mock = $this->getMockBuilder(Serializer::class)->getMock(); |
50
|
|
|
|
51
|
|
|
$mock->expects($this->any()) |
52
|
|
|
->method('serialize') |
53
|
|
|
->willReturnCallback(function (array $value) { |
54
|
|
|
return json_encode($value); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$mock->expects($this->any()) |
58
|
|
|
->method('deserialize') |
59
|
|
View Code Duplication |
->willReturnCallback(function (string $value) { |
|
|
|
|
60
|
|
|
$array = json_decode($value, true); |
61
|
|
|
|
62
|
|
|
if (!is_array($array)) { |
63
|
|
|
throw new \UnexpectedValueException("Expected result to be an array"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $array; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$documentRepository = $this |
70
|
|
|
->getMockBuilder(DocumentRepository::class) |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->getMock(); |
73
|
|
|
|
74
|
|
|
$documentRepository |
75
|
|
|
->expects($this->any()) |
76
|
|
|
->method('get') |
77
|
|
|
->willReturn(new Specification(new \stdClass)); |
78
|
|
|
|
79
|
|
|
/** @noinspection PhpParamsInspection */ |
80
|
|
|
$this->contentDecoder = new ContentDecoder($this->serializer, $documentRepository); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
public function canDecodeValidJson() |
87
|
|
|
{ |
88
|
|
|
$content = '{ "foo": "bar" }'; |
89
|
|
|
$request = TestRequestFactory::create($content, [], 'faux'); |
90
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
91
|
|
|
|
92
|
|
|
$operationObject = Operation::createFromOperationDefinition((object)[]); |
93
|
|
|
|
94
|
|
|
$actual = $this->contentDecoder->decodeContent($request, $operationObject); |
95
|
|
|
$expected = ['foo' => 'bar']; |
96
|
|
|
$this->assertSame($expected, $actual); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
* |
102
|
|
|
* @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function willThrowMalformedContentExceptionWhenDecodingFails() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$content = 'NOT VALID JSON'; |
107
|
|
|
$request = TestRequestFactory::create($content, [], 'faux'); |
108
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
109
|
|
|
|
110
|
|
|
$operationObject = Operation::createFromOperationDefinition((object)[]); |
111
|
|
|
|
112
|
|
|
$this->contentDecoder->decodeContent($request, $operationObject); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.