Completed
Pull Request — master (#40)
by
unknown
10:59
created

AddEditActionTest::testValidatesEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2017, 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 - 2017, 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\Collection;
13
14
use CakeDC\Api\Exception\ValidationException;
15
use CakeDC\Api\Service\Action\Collection\AddEditAction;
16
use CakeDC\Api\Service\FallbackService;
17
use CakeDC\Api\Service\ServiceRegistry;
18
use CakeDC\Api\TestSuite\TestCase;
19
use CakeDC\Api\Test\ConfigTrait;
20
use CakeDC\Api\Test\FixturesTrait;
21
use Cake\ORM\TableRegistry;
22
23
class AddEditActionTest extends TestCase
24
{
25
    use ConfigTrait;
26
    use FixturesTrait;
27
28
    /**
29
     * @var AddEditAction
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();
51
        unset($this->Service, $this->Action, $this->request);
52
        parent::tearDown();
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    public function testExecuteSuccess()
59
    {
60
        $ArticlesTable = TableRegistry::get('Articles');
61
        $initialCount = $ArticlesTable->find()->count();
62
        $this->_initializeAction([
63
            ['title' => 'Article1'],
64
            ['title' => 'Article2']
65
        ]);
66
67
        $this->Action->execute();
68
        $finalCount = $ArticlesTable->find()->count();
69
        $this->assertEquals(2, $finalCount - $initialCount, 'We should have added 2 new articles');
70
    }
71
72
    /**
73
     * @return void
74
     * @expectedException \CakeDC\Api\Exception\ValidationException
75
     */
76
    public function testValidationPostNotArray()
77
    {
78
        $this->_initializeAction(
79
            ['title' => 'Article1']
80
        );
81
        $this->Action->execute();
82
    }
83
84
    /**
85
     * @return void
86
     * @expectedException \CakeDC\Api\Exception\ValidationException
87
     */
88
    public function testValidationPostEmpty()
89
    {
90
        $this->_initializeAction();
91
        $this->Action->execute();
92
    }
93
94
    /**
95
     * @return void
96
     * @expectedException \CakeDC\Api\Exception\ValidationException
97
     */
98
    public function testValidationPostString()
99
    {
100
        $this->_initializeAction('something');
0 ignored issues
show
Documentation introduced by
'something' is of type string, 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...
101
        $this->Action->execute();
102
    }
103
104
    /**
105
     * @return void
106
     * @expectedException \CakeDC\Api\Exception\ValidationException
107
     * @expectedExceptionMessage Validation on Articles failed
108
     */
109
    public function testExecuteValidationEntityNotValid()
110
    {
111
        $ArticlesTable = TableRegistry::get('Articles');
112
        $initialCount = $ArticlesTable->find()->count();
113
        $this->_initializeAction([
114
            ['title' => 'Article1'],
115
            ['title' => '']
116
        ]);
117
118
        $this->Action->execute();
119
        $finalCount = $ArticlesTable->find()->count();
120
        $this->assertEquals(2, $finalCount - $initialCount, 'We should have added 2 new articles');
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function testValidatesEntity()
127
    {
128
        $this->_initializeAction([
129
            ['title' => 'Article1'],
130
            ['title' => 'Article2']
131
        ]);
132
133
        $this->assertTrue($this->Action->validates());
134
    }
135
136
    /**
137
     * @return void
138
     */
139
    public function testValidatesEntityNotValid()
140
    {
141
        $this->_initializeAction([
142
            ['title' => 'Article1'],
143
            ['title' => '']
144
        ]);
145
146
        try {
147
            $this->Action->validates();
148
            $this->fail('ValidationException was expected');
149
        } catch (ValidationException $ex) {
150
            $this->assertSame([
151
                // note the index here is important, first entity (0) is valid
152
                1 => [
153
                    'title' => [
154
                        '_empty' => 'This field cannot be left empty'
155
                    ]
156
                ]
157
            ], $ex->getValidationErrors());
158
        }
159
    }
160
161
    public function testIntegrationArticlesCollection()
162
    {
163
        $ArticlesTable = TableRegistry::get('Articles');
164
        $initialCount = $ArticlesTable->find()->count();
165
        $post = [
166
            ['title' => 'Article1'],
167
            ['title' => 'Article2']
168
        ];
169
170
        $this->_initializeRequest([
171
            'params' => [
172
                'service' => 'articlesCollection',
173
            ],
174
            'post' => $post,
175
        ], 'POST');
176
        $options = [
177
            'version' => null,
178
            'service' => null,
179
            'request' => $this->request,
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...
180
            '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...
181
            'baseUrl' => '/articles_collection/collection/add'
182
        ];
183
        $Service = ServiceRegistry::get($this->request['service'], $options);
184
        $this->assertTrue($Service instanceof FallbackService);
185
        $this->assertEquals('articles_collection', $Service->getName());
186
187
        $action = $Service->buildAction();
188
        $action->Auth->allow('*');
189
190
        $action->setTable($ArticlesTable);
191
        $result = $action->process();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
192
        $finalCount = $ArticlesTable->find()->count();
193
        $this->assertEquals(2, $finalCount - $initialCount, 'We should have added 3 new articles');
194
    }
195
196
    protected function _initializeAction($post = [])
197
    {
198
        $this->_initializeRequest([
199
            'params' => [
200
                'service' => 'articlesCollection',
201
            ],
202
            'post' => $post,
203
        ], 'POST');
204
        $options = [
205
            'version' => null,
206
            'service' => null,
207
            'request' => $this->request,
208
            'response' => $this->response,
209
            'baseUrl' => '/articles_collection/collection/add'
210
        ];
211
        $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...
212
213
        $this->Action = new AddEditAction([
214
            'service' => $this->Service,
215
        ]);
216
        $this->Action->setTable(TableRegistry::get('Articles'));
217
    }
218
}
219