1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016, 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, 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; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\CrudDeleteAction; |
15
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
16
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
17
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
18
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
19
|
|
|
|
20
|
|
|
class CrudDeleteActionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use ConfigTrait; |
24
|
|
|
use FixturesTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CrudDeleteAction |
28
|
|
|
*/ |
29
|
|
|
public $Action; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* setUp method |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* tearDown method |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function tearDown() |
47
|
|
|
{ |
48
|
|
|
ServiceRegistry::clear(); |
49
|
|
|
unset($this->Service, $this->Action, $this->request); |
50
|
|
|
parent::tearDown(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test load value method |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function testExecuteSuccess() |
59
|
|
|
{ |
60
|
|
|
$this->_initializeAction(1, [ |
61
|
|
|
'title' => 'New message' |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
$onFindEntity = false; |
65
|
|
|
$this->Action->eventManager()->on('Action.Crud.onFindEntity', function () use (&$onFindEntity) { |
66
|
|
|
$onFindEntity = true; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$result = $this->Action->execute(); |
70
|
|
|
$this->assertTrue($result); |
71
|
|
|
$this->assertTrue($onFindEntity); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Test load value method |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
* @expectedException \Cake\Datasource\Exception\RecordNotFoundException |
79
|
|
|
*/ |
80
|
|
|
public function testExecuteNotFound() |
81
|
|
|
{ |
82
|
|
|
$this->_initializeAction(999, [ |
83
|
|
|
'title' => 'New message' |
84
|
|
|
]); |
85
|
|
|
$this->Action->execute(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function _initializeAction($id, $post = []) |
89
|
|
|
{ |
90
|
|
|
$this->_initializeRequest([ |
91
|
|
|
'params' => [ |
92
|
|
|
'service' => 'articles', |
93
|
|
|
'pass' => [ |
94
|
|
|
$id, |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
'post' => $post, |
98
|
|
|
], 'DELETE'); |
99
|
|
|
$options = [ |
100
|
|
|
'version' => null, |
101
|
|
|
'service' => $this->request['service'], |
|
|
|
|
102
|
|
|
'request' => $this->request, |
103
|
|
|
'response' => $this->response, |
|
|
|
|
104
|
|
|
'baseUrl' => '/articles/' . $id, |
105
|
|
|
]; |
106
|
|
|
$this->Service = ServiceRegistry::get($this->request['service'], $options); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$this->Action = new CrudDeleteAction([ |
109
|
|
|
'service' => $this->Service, |
110
|
|
|
'id' => $id, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: