1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* File ResponseTest.php
*
* @author Edward Pfremmer <epfremme@nerdery.com>
*/
namespace Epfremme\Swagger\Tests\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Epfremme\Swagger\Entity\Examples;
use Epfremme\Swagger\Entity\Headers;
use Epfremme\Swagger\Entity\Response;
use Epfremme\Swagger\Entity\Parameters;
use Epfremme\Swagger\Entity\Schemas\AbstractSchema;
use Epfremme\Swagger\Entity\Schemas\ObjectSchema;
use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
/**
* Class ResponseTest
*
* @package Epfremme\Swagger
* @subpackage Tests\Entity
*/
class ResponseTest extends \PHPUnit_Framework_TestCase
{
use SerializerContextTrait;
/**
* @var Response
*/
protected $response;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->response = new Response();
}
/**
* @covers Epfremme\Swagger\Entity\Response::getDescription
* @covers Epfremme\Swagger\Entity\Response::setDescription
*/
public function testDescription()
{
$this->assertClassHasAttribute('description', Response::class);
$this->assertInstanceOf(Response::class, $this->response->setDescription('foo'));
$this->assertAttributeEquals('foo', 'description', $this->response);
$this->assertEquals('foo', $this->response->getDescription());
}
/**
* @covers Epfremme\Swagger\Entity\Response::getSchema
* @covers Epfremme\Swagger\Entity\Response::setSchema
*/
public function testSchema()
{
$schema = new ObjectSchema();
$this->assertClassHasAttribute('schema', Response::class);
$this->assertInstanceOf(Response::class, $this->response->setSchema($schema));
$this->assertAttributeInstanceOf(ObjectSchema::class, 'schema', $this->response);
$this->assertAttributeEquals($schema, 'schema', $this->response);
$this->assertEquals($schema, $this->response->getSchema());
}
/**
* @covers Epfremme\Swagger\Entity\Response::getHeaders
* @covers Epfremme\Swagger\Entity\Response::setHeaders
*/
public function testHeaders()
{
$headers = new ArrayCollection([
'foo' => new Headers\StringHeader(),
'bar' => new Headers\IntegerHeader(),
'baz' => new Headers\StringHeader(),
]);
$this->assertClassHasAttribute('headers', Response::class);
$this->assertInstanceOf(Response::class, $this->response->setHeaders($headers));
$this->assertAttributeInstanceOf(ArrayCollection::class, 'headers', $this->response);
$this->assertAttributeEquals($headers, 'headers', $this->response);
$this->assertEquals($headers, $this->response->getHeaders());
$this->assertContainsOnlyInstancesOf(Headers\AbstractHeader::class, $this->response->getHeaders());
}
/**
* @covers Epfremme\Swagger\Entity\Response::getExamples
* @covers Epfremme\Swagger\Entity\Response::setExamples
*/
public function testExamples()
{
$examples = new Examples();
$this->assertClassHasAttribute('examples', Response::class);
$this->assertInstanceOf(Response::class, $this->response->setExamples($examples));
$this->assertAttributeInstanceOf(Examples::class, 'examples', $this->response);
$this->assertAttributeEquals($examples, 'examples', $this->response);
$this->assertEquals($examples, $this->response->getExamples());
}
/**
* @covers Epfremme\Swagger\Entity\Response
*/
public function testSerialize()
{
$data = json_encode([
'description' => 'bar',
'schema' => [
'type' => AbstractSchema::OBJECT_TYPE,
'format' => 'foo',
'title' => 'bar',
'description' => 'baz',
'example' => 'qux',
'externalDocs' => (object)[],
],
'headers' => [
'X-Rate-Limit-Limit' => [
'description' => 'The number of allowed requests in the current period',
'type' => 'integer'
],
'X-Rate-Limit-Remaining' => [
'description' => 'The number of remaining requests in the current period',
'type' => 'integer'
],
'X-Rate-Limit-Reset' => [
'description' => 'The number of seconds left in the current period',
'type' => 'integer'
],
],
'examples' => [
'text/plain' => [
'foo' => 'bar',
'baz' => 'foo'
],
'application/json' => [
'key' => 'any'
],
]
]);
$response = $this->getSerializer()->deserialize($data, Response::class, 'json');
$this->assertInstanceOf(Response::class, $response);
$this->assertAttributeEquals('bar', 'description', $response);
$this->assertAttributeInstanceOf(AbstractSchema::class, 'schema', $response);
$this->assertAttributeInstanceOf(ArrayCollection::class, 'headers', $response);
$this->assertAttributeContainsOnly(Headers\AbstractHeader::class, 'headers', $response);
$this->assertAttributeInstanceOf(Examples::class, 'examples', $response);
$json = $this->getSerializer()->serialize($response, 'json');
$this->assertJson($json);
$this->assertJsonStringEqualsJsonString($data, $json);
}
}