1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, 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 - 2018, 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\CrudEditAction; |
15
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
16
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
17
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
18
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
19
|
|
|
|
20
|
|
|
use Cake\Datasource\EntityInterface; |
21
|
|
|
|
22
|
|
|
class CrudEditActionTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use ConfigTrait; |
26
|
|
|
use FixturesTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CrudEditAction |
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
|
|
|
* Test load value method |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function testExecuteSuccess() |
61
|
|
|
{ |
62
|
|
|
$this->_initializeAction(1, [ |
63
|
|
|
'title' => 'New message' |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$onFindEntity = false; |
67
|
|
|
$this->Action->getEventManager()->on('Action.Crud.onFindEntity', function () use (&$onFindEntity) { |
|
|
|
|
68
|
|
|
$onFindEntity = true; |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
$result = $this->Action->execute(); |
72
|
|
|
$this->assertTrue($result instanceof EntityInterface); |
73
|
|
|
$this->assertTrue($onFindEntity); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test load value method |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
* @expectedException \CakeDC\Api\Exception\ValidationException |
81
|
|
|
*/ |
82
|
|
|
public function testExecuteValidationError() |
83
|
|
|
{ |
84
|
|
|
$this->_initializeAction(1, [ |
85
|
|
|
'title' => '' |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$result = $this->Action->execute(); |
89
|
|
|
$this->assertTrue($result instanceof EntityInterface); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test load value method |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
* @expectedException \Cake\Datasource\Exception\RecordNotFoundException |
97
|
|
|
*/ |
98
|
|
|
public function testExecuteNotFound() |
99
|
|
|
{ |
100
|
|
|
$this->_initializeAction(999, [ |
101
|
|
|
'title' => 'New message' |
102
|
|
|
]); |
103
|
|
|
$this->Action->execute(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function _initializeAction($id, $post = []) |
107
|
|
|
{ |
108
|
|
|
$this->_initializeRequest([ |
109
|
|
|
'params' => [ |
110
|
|
|
'service' => 'articles', |
111
|
|
|
'pass' => [ |
112
|
|
|
$id, |
113
|
|
|
], |
114
|
|
|
], |
115
|
|
|
'post' => $post, |
116
|
|
|
], 'PUT'); |
117
|
|
|
$options = [ |
118
|
|
|
'version' => null, |
119
|
|
|
'service' => $this->request->getParam('service'), |
|
|
|
|
120
|
|
|
'request' => $this->request, |
121
|
|
|
'response' => $this->response, |
|
|
|
|
122
|
|
|
'baseUrl' => '/articles/' . $id, |
123
|
|
|
]; |
124
|
|
|
$this->Service = ServiceRegistry::get($this->request->getParam('service'), $options); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->Action = new CrudEditAction([ |
127
|
|
|
'service' => $this->Service, |
128
|
|
|
'id' => $id, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.