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
<?php
/**
* File PathTest.php
*
* @author Edward Pfremmer <epfremme@nerdery.com>
*/
namespace Epfremme\Swagger\Tests\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Epfremme\Swagger\Entity\Operation;
use Epfremme\Swagger\Entity\Parameters;
use Epfremme\Swagger\Entity\Path;
use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
/**
* Class PathTest
*
* @package Epfremme\Swagger
* @subpackage Tests\Entity
*/
class PathTest extends \PHPUnit_Framework_TestCase
{
use SerializerContextTrait;
/**
* @var Path
*/
protected $path;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->path = new Path();
}
/**
* @covers Epfremme\Swagger\Entity\Path::getOperations
* @covers Epfremme\Swagger\Entity\Path::setOperations
*/
public function testOperations()
{
$operations = new ArrayCollection([
'foo' => new Operation(),
'bar' => new Operation(),
'baz' => new Operation(),
]);
$this->assertClassHasAttribute('operations', Path::class);
$this->assertInstanceOf(Path::class, $this->path->setOperations($operations));
$this->assertAttributeInstanceOf(ArrayCollection::class, 'operations', $this->path);
$this->assertAttributeEquals($operations, 'operations', $this->path);
$this->assertEquals($operations, $this->path->getOperations());
$this->assertContainsOnlyInstancesOf(Operation::class, $this->path->getOperations());
}
/**
* @covers Epfremme\Swagger\Entity\Path
*/
public function testSerialize()
{
$data = json_encode([
'get' => [
'summary' => 'foo',
'description' => 'bar',
'responses' => [
'200' => [
'description' => 'Pet updated.'
],
'405' => [
'description' => 'Invalid input'
]
],
'schemes' => ['http', 'https'],
'security' => [
[
'petstore_auth' => [
'read:pets',
]
]
],
'deprecated' => false,
],
'post' => [
'tags' => [
'foo'
],
'summary' => 'foo',
'description' => 'bar',
'externalDocs' => (object)[],
'operationId' => 'baz',
'consumes' => [
'application/x-www-form-urlencoded'
],
'produces' => [
'application/json',
'application/xml'
],
'parameters' => [
[
'name' => 'petId',
'in' => Parameters\AbstractParameter::IN_PATH,
'description' => 'ID of pet that needs to be updated',
'required' => true,
'type' => Parameters\AbstractTypedParameter::STRING_TYPE
],
[
'name' => 'name',
'in' => Parameters\AbstractParameter::IN_FORM_DATA,
'description' => 'Updated name of the pet',
'required' => false,
'type' => Parameters\AbstractTypedParameter::STRING_TYPE
],
[
'name' => 'status',
'in' => Parameters\AbstractParameter::IN_FORM_DATA,
'description' => 'Updated status of the pet',
'required' => false,
'type' => Parameters\AbstractTypedParameter::STRING_TYPE
]
],
'responses' => [
'200' => [
'description' => 'Pet updated.'
],
'405' => [
'description' => 'Invalid input'
]
],
'schemes' => ['http', 'https'],
'security' => [
[
'petstore_auth' => [
'read:pets',
]
]
],
'deprecated' => true,
],
]);
$path = $this->getSerializer()->deserialize($data, Path::class, 'json');
$this->assertInstanceOf(Path::class, $path);
$this->assertAttributeInstanceOf(ArrayCollection::class, 'operations', $path);
$this->assertAttributeContainsOnly(Operation::class, 'operations', $path);
$json = $this->getSerializer()->serialize($path, 'json');
$this->assertJson($json);
$this->assertJsonStringEqualsJsonString($data, $json);
}
}