Completed
Push — master ( 8ec403...fa21f3 )
by Evgeny
03:18
created

CrudEditActionTest::_initializeAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
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'],
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...
118
            'request' => $this->request,
119
            '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...
120
            'baseUrl' => '/articles/' . $id,
121
        ];
122
        $this->Service = ServiceRegistry::get($this->request['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...
123
124
        $this->Action = new CrudEditAction([
125
            'service' => $this->Service,
126
            'id' => $id,
127
        ]);
128
    }
129
}
130