CrudViewActionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
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\CrudViewAction;
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
use Cake\Datasource\EntityInterface;
20
21
class CrudViewActionTest extends TestCase
22
{
23
24
    use ConfigTrait;
25
    use FixturesTrait;
26
27
    /**
28
     * @var CrudViewAction
29
     */
30
    public $Action;
31
32
    /**
33
     * setUp method
34
     *
35
     * @return void
36
     */
37
    public function setUp()
38
    {
39
        parent::setUp();
40
41
        $this->_initializeRequest([
42
            'params' => [
43
                'service' => 'articles',
44
                'pass' => [
45
                    '1',
46
                ]
47
            ]
48
        ]);
49
        $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...
50
        $options = [
51
            'version' => null,
52
            'service' => $service,
53
            'request' => $this->request,
54
            '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...
55
            'baseUrl' => '/articles/1'
56
        ];
57
        $this->Service = ServiceRegistry::get($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...
58
    }
59
60
    /**
61
     * tearDown method
62
     *
63
     * @return void
64
     */
65
    public function tearDown()
66
    {
67
        unset($this->Action);
68
        parent::tearDown();
69
    }
70
71
    /**
72
     * Test load value method
73
     *
74
     * @return void
75
     */
76
    public function testExecuteSuccess()
77
    {
78
        $this->Action = new CrudViewAction([
79
            'service' => $this->Service,
80
            'id' => 1,
81
        ]);
82
83
        $onFindEntity = false;
84
        $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...
85
            $onFindEntity = true;
86
        });
87
88
        $result = $this->Action->execute();
89
        $this->assertTrue($result instanceof EntityInterface);
90
        $this->assertTrue($onFindEntity);
91
    }
92
93
    /**
94
     * Test load value method
95
     *
96
     * @return void
97
     * @expectedException \Cake\Datasource\Exception\RecordNotFoundException
98
     */
99
    public function testExecuteNotFound()
100
    {
101
        $this->Action = new CrudViewAction([
102
            'service' => $this->Service,
103
            'id' => 999,
104
        ]);
105
106
        $result = $this->Action->execute();
107
        $this->assertTrue($result instanceof EntityInterface);
108
    }
109
}
110