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\DeleteAction; |
16
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
17
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
18
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
19
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
20
|
|
|
use Cake\ORM\TableRegistry; |
21
|
|
|
|
22
|
|
|
class DeleteActionTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
use ConfigTrait; |
25
|
|
|
use FixturesTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DeleteAction |
29
|
|
|
*/ |
30
|
|
|
public $Action; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* setUp method |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* tearDown method |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function tearDown() |
48
|
|
|
{ |
49
|
|
|
ServiceRegistry::clear(); |
50
|
|
|
unset($this->Service, $this->Action, $this->request); |
51
|
|
|
parent::tearDown(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function testExecuteSuccess() |
58
|
|
|
{ |
59
|
|
|
$ArticlesTable = TableRegistry::get('Articles'); |
60
|
|
|
$initialCount = $ArticlesTable->find()->count(); |
61
|
|
|
$this->_initializeAction([ |
62
|
|
|
['id' => 1], |
63
|
|
|
['id' => 2] |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$this->Action->execute(); |
67
|
|
|
$finalCount = $ArticlesTable->find()->count(); |
68
|
|
|
$this->assertEquals(-2, $finalCount - $initialCount, 'We should have added 2 new articles'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return void |
73
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
74
|
|
|
*/ |
75
|
|
|
public function testValidationPostNotArray() |
76
|
|
|
{ |
77
|
|
|
$this->_initializeAction( |
78
|
|
|
['id' => 1] |
79
|
|
|
); |
80
|
|
|
$this->Action->execute(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
86
|
|
|
*/ |
87
|
|
|
public function testValidationPostEmpty() |
88
|
|
|
{ |
89
|
|
|
$this->_initializeAction(); |
90
|
|
|
$this->Action->execute(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
96
|
|
|
*/ |
97
|
|
|
public function testValidationPostString() |
98
|
|
|
{ |
99
|
|
|
$this->_initializeAction('something'); |
|
|
|
|
100
|
|
|
$this->Action->execute(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
106
|
|
|
* @expectedExceptionMessage Validation failed |
107
|
|
|
*/ |
108
|
|
|
public function testExecuteValidationEntityNotValid() |
109
|
|
|
{ |
110
|
|
|
$this->_initializeAction([ |
111
|
|
|
['not-id' => 'something'], |
112
|
|
|
['blank' => new \ArrayObject()] |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$this->Action->execute(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function testValidatesEntity() |
122
|
|
|
{ |
123
|
|
|
$this->_initializeAction([ |
124
|
|
|
['id' => 1], |
125
|
|
|
['id' => 7] |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
$this->assertTrue($this->Action->validates()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function testValidatesEntityNotValid() |
135
|
|
|
{ |
136
|
|
|
$this->_initializeAction([ |
137
|
|
|
['id' => 1], |
138
|
|
|
['id' => ''] |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
$this->Action->validates(); |
143
|
|
|
$this->fail('ValidationException was expected'); |
144
|
|
|
} catch (ValidationException $ex) { |
145
|
|
|
$this->assertSame([ |
146
|
|
|
// note the index here is important, first entity (0) is valid |
147
|
|
|
1 => [ |
148
|
|
|
'id' => [ |
149
|
|
|
'_empty' => 'Missing id' |
150
|
|
|
] |
151
|
|
|
] |
152
|
|
|
], $ex->getValidationErrors()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function _initializeAction($post = []) |
157
|
|
|
{ |
158
|
|
|
$this->_initializeRequest([ |
159
|
|
|
'params' => [ |
160
|
|
|
'service' => 'articlesCollection', |
161
|
|
|
], |
162
|
|
|
'post' => $post, |
163
|
|
|
], 'POST'); |
164
|
|
|
$options = [ |
165
|
|
|
'version' => null, |
166
|
|
|
'service' => null, |
167
|
|
|
'request' => $this->request, |
|
|
|
|
168
|
|
|
'response' => $this->response, |
|
|
|
|
169
|
|
|
'baseUrl' => '/articles_collection/collection/delete' |
170
|
|
|
]; |
171
|
|
|
$this->Service = ServiceRegistry::get($this->request['service'], $options); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->Action = new DeleteAction([ |
174
|
|
|
'service' => $this->Service, |
175
|
|
|
]); |
176
|
|
|
$this->Action->setTable(TableRegistry::get('Articles')); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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: