| 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\CrudEditAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 CrudEditActionTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     use ConfigTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     use FixturesTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var CrudEditAction | 
            
                                                                        
                            
            
                                    
            
            
                | 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 instanceof \Cake\Datasource\EntityInterface); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertTrue($onFindEntity); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Test load value method | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @expectedException \CakeDC\Api\Exception\ValidationException | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function testExecuteValidationError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->_initializeAction(1, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'title' => '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $result = $this->Action->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertTrue($result instanceof \Cake\Datasource\EntityInterface); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * Test load value method | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @expectedException \Cake\Datasource\Exception\RecordNotFoundException | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function testExecuteNotFound() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->_initializeAction(999, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'title' => 'New message' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->Action->execute(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     protected function _initializeAction($id, $post = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->_initializeRequest([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'params' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 'service' => 'articles', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 'pass' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'post' => $post, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ], 'PUT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             'version' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			'service' => $this->request['service'], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             'request' => $this->request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             'response' => $this->response, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             'baseUrl' => '/articles/' . $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->Service = ServiceRegistry::get($this->request['service'], $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->Action = new CrudEditAction([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'service' => $this->Service, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  | 
            
                        
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: