1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the distributed-configuration-bundle package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Maikuro\DistributedConfigurationBundle\Tests; |
17
|
|
|
|
18
|
|
|
use Maikuro\DistributedConfigurationBundle\Handler\StoreHandler; |
19
|
|
|
use Maikuro\DistributedConfigurationBundle\Model\KeyValue; |
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
21
|
|
|
use Webmozart\KeyValueStore\JsonFileStore; |
22
|
|
|
|
23
|
|
|
class ApiTest extends WebTestCase |
24
|
|
|
{ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->client = static::createClient([], ['HTTP_ACCEPT' => 'application/json']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testApiGetNoKey() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->client->request( |
35
|
|
|
'GET', |
36
|
|
|
'/v1/keys/test_get_ke', |
37
|
|
|
[], |
38
|
|
|
[], |
39
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'] |
40
|
|
|
); |
41
|
|
|
$this->assertTrue( |
42
|
|
|
$this->client->getResponse()->headers->contains( |
43
|
|
|
'Content-Type', |
44
|
|
|
'application/json' |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
$this->assertEquals( |
48
|
|
|
'{"code":404,"message":"The key \"test_get_ke\" does not exist."}', |
49
|
|
|
$this->client->getResponse()->getContent() |
50
|
|
|
); |
51
|
|
|
$this->assertEquals(404, $this->client->getResponse()->getStatusCode()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testApiGet() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->createKeyValue('test_get_key', 'test_get_value'); |
57
|
|
|
|
58
|
|
|
$this->client->request( |
59
|
|
|
'GET', |
60
|
|
|
'/v1/keys/test_get_key', |
61
|
|
|
[], |
62
|
|
|
[], |
63
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'] |
64
|
|
|
); |
65
|
|
|
$this->assertTrue( |
66
|
|
|
$this->client->getResponse()->headers->contains( |
67
|
|
|
'Content-Type', |
68
|
|
|
'application/json' |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
$this->assertEquals( |
72
|
|
|
'{"key":"test_get_key","value":"test_get_value"}', |
73
|
|
|
$this->client->getResponse()->getContent() |
74
|
|
|
); |
75
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testApiCreate() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->client->request( |
81
|
|
|
'POST', |
82
|
|
|
'/v1/keys', |
83
|
|
|
[], |
84
|
|
|
[], |
85
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'], |
86
|
|
|
json_encode(['key' => 'test_key', 'value' => 'test_value']) |
87
|
|
|
); |
88
|
|
|
$this->assertTrue( |
89
|
|
|
$this->client->getResponse()->headers->contains( |
90
|
|
|
'Content-Type', |
91
|
|
|
'application/json' |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
$this->assertEquals( |
95
|
|
|
'{"key":"test_key","value":"test_value"}', |
96
|
|
|
$this->client->getResponse()->getContent() |
97
|
|
|
); |
98
|
|
|
$this->assertEquals(201, $this->client->getResponse()->getStatusCode()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function testApiCreateFormError() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->client->request( |
104
|
|
|
'POST', |
105
|
|
|
'/v1/keys', |
106
|
|
|
[], |
107
|
|
|
[], |
108
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'], |
109
|
|
|
json_encode(['ke' => 'test_key', 'value' => 'test_value']) |
110
|
|
|
); |
111
|
|
|
$this->assertTrue( |
112
|
|
|
$this->client->getResponse()->headers->contains( |
113
|
|
|
'Content-Type', |
114
|
|
|
'application/json' |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
$this->assertEquals( |
118
|
|
|
'{"code":400,"message":"Validation Failed","errors":{"errors":["This form should not contain extra fields."],"children":{"key":{},"value":{}}}}', |
119
|
|
|
$this->client->getResponse()->getContent() |
120
|
|
|
); |
121
|
|
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
public function testApiPatch() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$this->createKeyValue('key_to_patch'); |
127
|
|
|
|
128
|
|
|
$this->client->request( |
129
|
|
|
'PATCH', |
130
|
|
|
'/v1/keys/key_to_patch', |
131
|
|
|
[], |
132
|
|
|
[], |
133
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'], |
134
|
|
|
json_encode(['value' => 'value_to_patch']) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertEquals(204, $this->client->getResponse()->getStatusCode()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function testApiUpdate() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$this->createKeyValue('key_to_update'); |
143
|
|
|
|
144
|
|
|
$this->client->request( |
145
|
|
|
'PUT', |
146
|
|
|
'/v1/keys/key_to_update', |
147
|
|
|
[], |
148
|
|
|
[], |
149
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'], |
150
|
|
|
json_encode(['key' => 'key_to_update', 'value' => 'test_update_value']) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->assertEquals(204, $this->client->getResponse()->getStatusCode()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function testApiDelete() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$this->createKeyValue('key with space'); |
159
|
|
|
|
160
|
|
|
$this->client->request( |
161
|
|
|
'DELETE', |
162
|
|
|
'/v1/keys/key with space', |
163
|
|
|
[], |
164
|
|
|
[], |
165
|
|
|
['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'] |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->assertEquals(204, $this->client->getResponse()->getStatusCode()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* createKeyValue. |
173
|
|
|
* |
174
|
|
|
* @param string $key |
175
|
|
|
* @param string $value |
176
|
|
|
*/ |
177
|
|
|
private function createKeyValue($key = 'test_key', $value = 'test_value') |
178
|
|
|
{ |
179
|
|
|
$keyValue = new KeyValue(); |
180
|
|
|
$keyValue->setKey($key); |
181
|
|
|
$keyValue->setValue($value); |
182
|
|
|
|
183
|
|
|
$storeHandler = new StoreHandler(new JsonFileStore(__DIR__.'/App/test.json')); |
184
|
|
|
$storeHandler->flush($keyValue); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.