CrudDeleteActionTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 6 1
A testExecuteSuccess() 0 15 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\CrudDeleteAction;
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
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();
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...
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->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...
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->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...
102
            'request' => $this->request,
103
            '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...
104
            'baseUrl' => '/articles/' . $id,
105
        ];
106
        $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...
107
108
        $this->Action = new CrudDeleteAction([
109
            'service' => $this->Service,
110
            'id' => $id,
111
        ]);
112
    }
113
}
114