1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Test\TestCase\Service\Action\Collection; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Exception\ValidationException; |
15
|
|
|
use CakeDC\Api\Service\Action\Collection\AddEditAction; |
16
|
|
|
use CakeDC\Api\Service\FallbackService; |
17
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
18
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
19
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
20
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
21
|
|
|
use Cake\ORM\TableRegistry; |
22
|
|
|
|
23
|
|
|
class AddEditActionTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
use ConfigTrait; |
26
|
|
|
use FixturesTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AddEditAction |
30
|
|
|
*/ |
31
|
|
|
public $Action; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* setUp method |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
parent::setUp(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* tearDown method |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function tearDown() |
49
|
|
|
{ |
50
|
|
|
ServiceRegistry::clear(); |
51
|
|
|
unset($this->Service, $this->Action, $this->request); |
52
|
|
|
parent::tearDown(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function testExecuteSuccess() |
59
|
|
|
{ |
60
|
|
|
$ArticlesTable = TableRegistry::get('Articles'); |
61
|
|
|
$initialCount = $ArticlesTable->find()->count(); |
62
|
|
|
$this->_initializeAction([ |
63
|
|
|
['title' => 'Article1'], |
64
|
|
|
['title' => 'Article2'] |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->Action->execute(); |
68
|
|
|
$finalCount = $ArticlesTable->find()->count(); |
69
|
|
|
$this->assertEquals(2, $finalCount - $initialCount, 'We should have added 2 new articles'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return void |
74
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
75
|
|
|
*/ |
76
|
|
|
public function testValidationPostNotArray() |
77
|
|
|
{ |
78
|
|
|
$this->_initializeAction( |
79
|
|
|
['title' => 'Article1'] |
80
|
|
|
); |
81
|
|
|
$this->Action->execute(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
87
|
|
|
*/ |
88
|
|
|
public function testValidationPostEmpty() |
89
|
|
|
{ |
90
|
|
|
$this->_initializeAction(); |
91
|
|
|
$this->Action->execute(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return void |
96
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
97
|
|
|
*/ |
98
|
|
|
public function testValidationPostString() |
99
|
|
|
{ |
100
|
|
|
$this->_initializeAction('something'); |
|
|
|
|
101
|
|
|
$this->Action->execute(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return void |
106
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
107
|
|
|
* @expectedExceptionMessage Validation on Articles failed |
108
|
|
|
*/ |
109
|
|
|
public function testExecuteValidationEntityNotValid() |
110
|
|
|
{ |
111
|
|
|
$ArticlesTable = TableRegistry::get('Articles'); |
112
|
|
|
$initialCount = $ArticlesTable->find()->count(); |
113
|
|
|
$this->_initializeAction([ |
114
|
|
|
['title' => 'Article1'], |
115
|
|
|
['title' => ''] |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->Action->execute(); |
119
|
|
|
$finalCount = $ArticlesTable->find()->count(); |
120
|
|
|
$this->assertEquals(2, $finalCount - $initialCount, 'We should have added 2 new articles'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function testValidatesEntity() |
127
|
|
|
{ |
128
|
|
|
$this->_initializeAction([ |
129
|
|
|
['title' => 'Article1'], |
130
|
|
|
['title' => 'Article2'] |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$this->assertTrue($this->Action->validates()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testValidatesEntityNotValid() |
140
|
|
|
{ |
141
|
|
|
$this->_initializeAction([ |
142
|
|
|
['title' => 'Article1'], |
143
|
|
|
['title' => ''] |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
try { |
147
|
|
|
$this->Action->validates(); |
148
|
|
|
$this->fail('ValidationException was expected'); |
149
|
|
|
} catch (ValidationException $ex) { |
150
|
|
|
$this->assertSame([ |
151
|
|
|
// note the index here is important, first entity (0) is valid |
152
|
|
|
1 => [ |
153
|
|
|
'title' => [ |
154
|
|
|
'_empty' => 'This field cannot be left empty' |
155
|
|
|
] |
156
|
|
|
] |
157
|
|
|
], $ex->getValidationErrors()); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testIntegrationArticlesCollection() |
162
|
|
|
{ |
163
|
|
|
$ArticlesTable = TableRegistry::get('Articles'); |
164
|
|
|
$initialCount = $ArticlesTable->find()->count(); |
165
|
|
|
$post = [ |
166
|
|
|
['title' => 'Article1'], |
167
|
|
|
['title' => 'Article2'] |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
$this->_initializeRequest([ |
171
|
|
|
'params' => [ |
172
|
|
|
'service' => 'articlesCollection', |
173
|
|
|
], |
174
|
|
|
'post' => $post, |
175
|
|
|
], 'POST'); |
176
|
|
|
$options = [ |
177
|
|
|
'version' => null, |
178
|
|
|
'service' => null, |
179
|
|
|
'request' => $this->request, |
|
|
|
|
180
|
|
|
'response' => $this->response, |
|
|
|
|
181
|
|
|
'baseUrl' => '/articles_collection/collection/add' |
182
|
|
|
]; |
183
|
|
|
$Service = ServiceRegistry::get($this->request['service'], $options); |
184
|
|
|
$this->assertTrue($Service instanceof FallbackService); |
185
|
|
|
$this->assertEquals('articles_collection', $Service->getName()); |
186
|
|
|
|
187
|
|
|
$action = $Service->buildAction(); |
188
|
|
|
$action->Auth->allow('*'); |
189
|
|
|
|
190
|
|
|
$action->setTable($ArticlesTable); |
191
|
|
|
$result = $action->process(); |
|
|
|
|
192
|
|
|
$finalCount = $ArticlesTable->find()->count(); |
193
|
|
|
$this->assertEquals(2, $finalCount - $initialCount, 'We should have added 3 new articles'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function _initializeAction($post = []) |
197
|
|
|
{ |
198
|
|
|
$this->_initializeRequest([ |
199
|
|
|
'params' => [ |
200
|
|
|
'service' => 'articlesCollection', |
201
|
|
|
], |
202
|
|
|
'post' => $post, |
203
|
|
|
], 'POST'); |
204
|
|
|
$options = [ |
205
|
|
|
'version' => null, |
206
|
|
|
'service' => null, |
207
|
|
|
'request' => $this->request, |
208
|
|
|
'response' => $this->response, |
209
|
|
|
'baseUrl' => '/articles_collection/collection/add' |
210
|
|
|
]; |
211
|
|
|
$this->Service = ServiceRegistry::get($this->request['service'], $options); |
|
|
|
|
212
|
|
|
|
213
|
|
|
$this->Action = new AddEditAction([ |
214
|
|
|
'service' => $this->Service, |
215
|
|
|
]); |
216
|
|
|
$this->Action->setTable(TableRegistry::get('Articles')); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: