|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Versioning Document Entity class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\CoreBundle\Tests\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\SchemaBundle\Constraint\VersionServiceConstraint; |
|
9
|
|
|
use GravitonDyn\TestCaseVersioningEntityBundle\DataFixtures\MongoDB\LoadTestCaseVersioningEntityData; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class VersioningDocumentsTest extends RestTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* load fixtures |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
if (!class_exists(LoadTestCaseVersioningEntityData::class)) { |
|
29
|
|
|
$this->markTestSkipped('Test definitions are not loaded'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->loadFixtures( |
|
33
|
|
|
[ |
|
34
|
|
|
LoadTestCaseVersioningEntityData::class |
|
35
|
|
|
], |
|
36
|
|
|
null, |
|
37
|
|
|
'doctrine_mongodb' |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Test Document as embedded |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testPut() |
|
47
|
|
|
{ |
|
48
|
|
|
// check document |
|
49
|
|
|
$client = static::createRestClient(); |
|
50
|
|
|
$client->request('GET', '/testcase/versioning-entity/one'); |
|
51
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
52
|
|
|
$original = $client->getResults(); |
|
53
|
|
|
$this->assertObjectHasAttribute('version', $original, 'Response have no version: '.json_encode($original)); |
|
54
|
|
|
|
|
55
|
|
|
$initialVersion = $original->version; |
|
56
|
|
|
|
|
57
|
|
|
// Let's change something |
|
58
|
|
|
$original->data = "one-one"; |
|
59
|
|
|
|
|
60
|
|
|
$client = static::createRestClient(); |
|
61
|
|
|
$client->put('/testcase/versioning-entity/one', $original); |
|
62
|
|
|
$respo = $client->getResponse(); |
|
63
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $respo->getStatusCode(), $respo->getContent()); |
|
64
|
|
|
|
|
65
|
|
|
// Version has been updated with put |
|
66
|
|
|
$initialVersion++; |
|
67
|
|
|
|
|
68
|
|
|
$client->request('GET', '/testcase/versioning-entity/one'); |
|
69
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
70
|
|
|
|
|
71
|
|
|
$originalB = $client->getResults(); |
|
72
|
|
|
$this->assertEquals("one-one", $originalB->data, json_encode($originalB)); |
|
73
|
|
|
$this->assertEquals($initialVersion, $originalB->version, json_encode($originalB)); |
|
74
|
|
|
|
|
75
|
|
|
// Let's change something, version should not be possible |
|
76
|
|
|
$original->data = "one"; |
|
77
|
|
|
$original->version = 1; |
|
78
|
|
|
|
|
79
|
|
|
$client = static::createRestClient(); |
|
80
|
|
|
$client->put('/testcase/versioning-entity/one', $original); |
|
81
|
|
|
$response = $client->getResponse(); |
|
82
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
83
|
|
|
$this->assertEquals($initialVersion, $response->headers->get(VersionServiceConstraint::HEADER_NAME)); |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// Update with correct version |
|
87
|
|
|
// Let's change something, version should not be possible |
|
88
|
|
|
$original->data = "one"; |
|
89
|
|
|
$original->version = $initialVersion; |
|
90
|
|
|
|
|
91
|
|
|
$client = static::createRestClient(); |
|
92
|
|
|
$client->put('/testcase/versioning-entity/one', $original); |
|
93
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* there was a bug that clients could 'reset' the version by just not sending it. |
|
98
|
|
|
* that's because version is not a required field per se; so the _field_ validator would not execute. |
|
99
|
|
|
* this is testing the service validator now. |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testSubsequentCreateWithNoVersion() |
|
104
|
|
|
{ |
|
105
|
|
|
// create a record, specify no version |
|
106
|
|
|
$record = (object) [ |
|
107
|
|
|
'id' => 'dude', |
|
108
|
|
|
'data' => 'mydata' |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
$client = static::createRestClient(); |
|
112
|
|
|
$client->put('/testcase/versioning-entity/dude', $record); |
|
113
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
114
|
|
|
|
|
115
|
|
|
// now again with empty id |
|
116
|
|
|
$client = static::createRestClient(); |
|
117
|
|
|
$client->put('/testcase/versioning-entity/dude', $record); |
|
118
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode()); |
|
119
|
|
|
$this->assertEquals(1, $client->getResponse()->headers->get(VersionServiceConstraint::HEADER_NAME)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Test Document as embedded |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public function testPost() |
|
128
|
|
|
{ |
|
129
|
|
|
$new = new \stdClass(); |
|
130
|
|
|
$new->data = 'something-to-test'; |
|
131
|
|
|
|
|
132
|
|
|
$client = static::createRestClient(); |
|
133
|
|
|
$client->post('/testcase/versioning-entity/', $new); |
|
134
|
|
|
$resp = $client->getResponse(); |
|
135
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $resp->getStatusCode(), $resp->getContent()); |
|
136
|
|
|
$urlNew = $resp->headers->get('Location'); |
|
137
|
|
|
|
|
138
|
|
|
// check document |
|
139
|
|
|
$client = static::createRestClient(); |
|
140
|
|
|
$client->request('GET', $urlNew); |
|
141
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
142
|
|
|
$original = $client->getResults(); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertEquals($new->data, $original->data); |
|
145
|
|
|
$this->assertEquals(1, $original->version); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Test Document as embedded |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testPatching() |
|
156
|
|
|
{ |
|
157
|
|
|
// create a record, specify no version |
|
158
|
|
|
$record = (object) [ |
|
159
|
|
|
'id' => 'patch-id-test', |
|
160
|
|
|
'data' => 'mydata' |
|
161
|
|
|
]; |
|
162
|
|
|
|
|
163
|
|
|
$client = static::createRestClient(); |
|
164
|
|
|
$client->put('/testcase/versioning-entity/'.($record->id), $record); |
|
165
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
166
|
|
|
|
|
167
|
|
|
// PATCH to fail, no version field |
|
168
|
|
|
$client = static::createRestClient(); |
|
169
|
|
|
$patch = json_encode( |
|
170
|
|
|
[ |
|
171
|
|
|
[ |
|
172
|
|
|
'op' => 'replace', |
|
173
|
|
|
'path' => '/data', |
|
174
|
|
|
'value' => 'fail here' |
|
175
|
|
|
] |
|
176
|
|
|
] |
|
177
|
|
|
); |
|
178
|
|
|
$client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
|
179
|
|
|
$response = $client->getResponse(); |
|
180
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
181
|
|
|
|
|
182
|
|
|
// PATCH to fail, wrong version number |
|
183
|
|
|
$client = static::createRestClient(); |
|
184
|
|
|
$patch = json_encode( |
|
185
|
|
|
[ |
|
186
|
|
|
[ |
|
187
|
|
|
'op' => 'replace', |
|
188
|
|
|
'path' => '/data', |
|
189
|
|
|
'value' => 'should be KO' |
|
190
|
|
|
], |
|
191
|
|
|
[ |
|
192
|
|
|
'op' => 'replace', |
|
193
|
|
|
'path' => '/version', |
|
194
|
|
|
'value' => 7 |
|
195
|
|
|
] |
|
196
|
|
|
] |
|
197
|
|
|
); |
|
198
|
|
|
$client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
|
199
|
|
|
$response = $client->getResponse(); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
202
|
|
|
$this->assertEquals(1, $response->headers->get(VersionServiceConstraint::HEADER_NAME)); |
|
203
|
|
|
|
|
204
|
|
|
// PATCH, checking header from failed and use it to patch version. |
|
205
|
|
|
$patch = json_encode( |
|
206
|
|
|
[ |
|
207
|
|
|
[ |
|
208
|
|
|
'op' => 'replace', |
|
209
|
|
|
'path' => '/data', |
|
210
|
|
|
'value' => 'should be OK' |
|
211
|
|
|
], |
|
212
|
|
|
[ |
|
213
|
|
|
'op' => 'replace', |
|
214
|
|
|
'path' => '/version', |
|
215
|
|
|
'value' => $response->headers->get(VersionServiceConstraint::HEADER_NAME) |
|
216
|
|
|
] |
|
217
|
|
|
] |
|
218
|
|
|
); |
|
219
|
|
|
|
|
220
|
|
|
$client->request('PATCH', '/testcase/versioning-entity/' . ($record->id), array(), array(), array(), $patch); |
|
221
|
|
|
$response = $client->getResponse(); |
|
222
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
223
|
|
|
|
|
224
|
|
|
// Let's check that the patch updated counter version. |
|
225
|
|
|
$client->request('GET', '/testcase/versioning-entity/' . ($record->id)); |
|
226
|
|
|
$response = $client->getResponse(); |
|
227
|
|
|
$current = json_decode($response->getContent()); |
|
228
|
|
|
$this->assertEquals(2, $current->version); |
|
229
|
|
|
$this->assertEquals('should be OK', $current->data); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|