CrudEditActionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 6 1
A testExecuteSuccess() 0 15 1
A testExecuteValidationError() 0 9 1
A testExecuteNotFound() 0 7 1
A _initializeAction() 0 25 1
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();
0 ignored issues
show
Deprecated Code introduced by
The method CakeDC\Api\Service\ServiceRegistry::clear() has been deprecated with message: 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::clear() instead.

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.

Loading history...
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) {
0 ignored issues
show
Documentation introduced by
function () use(&$onFind...$onFindEntity = true; } is of type object<Closure>, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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'),
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
120
            'request' => $this->request,
121
            'response' => $this->response,
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
122
            'baseUrl' => '/articles/' . $id,
123
        ];
124
        $this->Service = ServiceRegistry::get($this->request->getParam('service'), $options);
0 ignored issues
show
Bug introduced by
The property Service does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Deprecated Code introduced by
The method CakeDC\Api\Service\ServiceRegistry::get() has been deprecated with message: 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::get() instead.

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.

Loading history...
125
126
        $this->Action = new CrudEditAction([
127
            'service' => $this->Service,
128
            'id' => $id,
129
        ]);
130
    }
131
}
132