1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* test for RecordOriginConstraint |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SchemaBundle\Tests\ConstraintBuilder; |
7
|
|
|
|
8
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
13
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
14
|
|
|
* @link http://swisscom.ch |
15
|
|
|
*/ |
16
|
|
|
class RecordOriginConstraintTest extends RestTestCase |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @TODO Upsert via POST, stuff via PATCH |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* load fixtures |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->loadFixtures( |
31
|
|
|
array( |
32
|
|
|
'GravitonDyn\CustomerBundle\DataFixtures\MongoDB\LoadCustomerData', |
33
|
|
|
), |
34
|
|
|
null, |
35
|
|
|
'doctrine_mongodb' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* test the validation of the RecordOriginConstraint |
41
|
|
|
* |
42
|
|
|
* @dataProvider createDataProvider |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function testRecordOriginHandlingOnCreate($entity, $expectedStatus, $expectedResponse) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$client = static::createRestClient(); |
49
|
|
|
$client->post('/person/customer/', $entity); |
50
|
|
|
|
51
|
|
|
$response = $client->getResponse(); |
52
|
|
|
$this->assertEquals($expectedStatus, $response->getStatusCode()); |
53
|
|
|
$this->assertEquals($expectedResponse, $client->getResults()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* test the validation of the RecordOriginConstraint |
58
|
|
|
* |
59
|
|
|
* @dataProvider updateDataProvider |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function testRecordOriginHandlingOnUpdate($fieldsToSet, $expectedStatus, $expectedResponse, $checkSavedEntry = true) |
64
|
|
|
{ |
65
|
|
|
$client = static::createRestClient(); |
66
|
|
|
$client->request('GET', '/person/customer/100'); |
67
|
|
|
$result = $client->getResults(); |
68
|
|
|
|
69
|
|
|
// apply changes |
70
|
|
|
foreach ($fieldsToSet as $key => $val) { |
71
|
|
|
$result->{$key} = $val; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$expectedObject = $result; |
75
|
|
|
|
76
|
|
|
$client = static::createRestClient(); |
77
|
|
|
$client->put('/person/customer/100', $result); |
78
|
|
|
|
79
|
|
|
$response = $client->getResponse(); |
80
|
|
|
$this->assertEquals($expectedStatus, $response->getStatusCode()); |
81
|
|
|
$this->assertEquals($expectedResponse, $client->getResults()); |
82
|
|
|
|
83
|
|
|
if ($checkSavedEntry) { |
84
|
|
|
// fetch it again and compare |
85
|
|
|
$client = static::createRestClient(); |
86
|
|
|
$client->request('GET', '/person/customer/100'); |
87
|
|
|
$this->assertEquals($expectedObject, $client->getResults()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function createDataProvider() |
93
|
|
|
{ |
94
|
|
|
$baseObj = [ |
95
|
|
|
'customerNumber' => 888, |
96
|
|
|
'name' => 'Muster Hans' |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
|
101
|
|
|
/*** STUFF THAT SHOULD BE ALLOWED ***/ |
102
|
|
|
|
103
|
|
|
'create-allowed-object' => [ |
104
|
|
|
'entity' => (object) array_merge( |
105
|
|
|
$baseObj, |
106
|
|
|
[ |
107
|
|
|
'recordOrigin' => 'hans' |
108
|
|
|
] |
109
|
|
|
), |
110
|
|
|
'httpStatusExpected' => Response::HTTP_CREATED, |
111
|
|
|
'expectedResponse' => null |
112
|
|
|
], |
113
|
|
|
|
114
|
|
|
/*** STUFF THAT SHOULD BE DENIED ***/ |
115
|
|
|
|
116
|
|
|
'create-recordorigin-core' => [ |
117
|
|
|
'entity' => (object) array_merge( |
118
|
|
|
$baseObj, |
119
|
|
|
[ |
120
|
|
|
'recordOrigin' => 'core' |
121
|
|
|
] |
122
|
|
|
), |
123
|
|
|
'httpStatusExpected' => Response::HTTP_BAD_REQUEST, |
124
|
|
|
'expectedResponse' => [ |
125
|
|
|
(object) [ |
126
|
|
|
'propertyPath' => 'recordOrigin', |
127
|
|
|
'message' => 'Creating documents with the recordOrigin field having a '. |
128
|
|
|
'value of core is not permitted.' |
129
|
|
|
] |
130
|
|
|
] |
131
|
|
|
] |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function updateDataProvider() |
136
|
|
|
{ |
137
|
|
|
$expectedErrorOutput = [ |
138
|
|
|
(object) [ |
139
|
|
|
'propertyPath' => 'recordOrigin', |
140
|
|
|
'message' => 'Prohibited modification attempt on record with recordOrigin of core' |
141
|
|
|
] |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
return [ |
145
|
|
|
|
146
|
|
|
/*** STUFF THAT SHOULD BE ALLOWED ***/ |
147
|
|
|
'create-allowed-object' => [ |
148
|
|
|
'fieldsToSet' => [ |
149
|
|
|
'addedField' => (object) [ |
150
|
|
|
'some' => 'property', |
151
|
|
|
'another' => 'one' |
152
|
|
|
] |
153
|
|
|
], |
154
|
|
|
'httpStatusExpected' => Response::HTTP_NO_CONTENT, |
155
|
|
|
'expectedResponse' => null |
156
|
|
|
], |
157
|
|
|
'subproperty-modification' => [ |
158
|
|
|
'fieldsToSet' => [ |
159
|
|
|
'someObject' => (object) [ |
160
|
|
|
'oneField' => 'value', |
161
|
|
|
'twoField' => 'twofield' |
162
|
|
|
] |
163
|
|
|
], |
164
|
|
|
'httpStatusExpected' => Response::HTTP_NO_CONTENT, |
165
|
|
|
'expectedResponse' => null |
166
|
|
|
], |
167
|
|
|
|
168
|
|
|
/*** STUFF THAT NEEDS TO BE DENIED ***/ |
169
|
|
|
'denied-subproperty-modification' => [ |
170
|
|
|
'fieldsToSet' => [ |
171
|
|
|
'someObject' => (object) [ |
172
|
|
|
'oneField' => 'changed-value' |
173
|
|
|
] |
174
|
|
|
], |
175
|
|
|
'httpStatusExpected' => Response::HTTP_BAD_REQUEST, |
176
|
|
|
'expectedResponse' => $expectedErrorOutput, |
177
|
|
|
'checkSavedEntry' => false |
178
|
|
|
], |
179
|
|
|
'denied-try-change-recordorigin' => [ |
180
|
|
|
'fieldsToSet' => [ |
181
|
|
|
'recordOrigin' => 'hans' |
182
|
|
|
], |
183
|
|
|
'httpStatusExpected' => Response::HTTP_BAD_REQUEST, |
184
|
|
|
'expectedResponse' => $expectedErrorOutput, |
185
|
|
|
'checkSavedEntry' => false |
186
|
|
|
], |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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.