testInitializeActionByServiceConfigMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 33
rs 9.392
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\App\Service;
13
14
use CakeDC\Api\Service\ConfigReader;
15
use CakeDC\Api\Service\FallbackService;
16
use CakeDC\Api\Service\Service;
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\Controller\Controller;
22
use Cake\Core\Configure;
23
24
class ServiceTest extends TestCase
25
{
26
    use ConfigTrait;
27
    use FixturesTrait;
28
29
    /**
30
     * setUp method
31
     *
32
     * @return void
33
     */
34
    public function setUp()
35
    {
36
        parent::setUp();
37
    }
38
39
    /**
40
     * tearDown method
41
     *
42
     * @return void
43
     */
44
    public function tearDown()
45
    {
46
        ServiceRegistry::getServiceLocator()->clear();
47
        parent::tearDown();
48
    }
49
50
    /**
51
     * Test construct
52
     *
53
     * @return void
54
     * expectedException \CakeDC\Api\Service\Exception\MissingAdapterException
55
     */
56
    public function testConstructWithoutAdapter()
57
    {
58
        $this->_initializeRequest();
59
        $this->Service = new FallbackService([
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...
60
            'service' => 'authors',
61
            '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...
62
            '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...
63
            'baseUrl' => '/authors'
64
        ]);
65
        $this->assertInstanceOf(FallbackService::class, $this->Service);
66
    }
67
68
    /**
69
     * Test construct
70
     *
71
     * @return void
72
     */
73
    public function testConstructWithRendererAsParameter()
74
    {
75
        $this->_initializeRequest();
76
        $this->Service = new FallbackService([
77
            'service' => 'authors',
78
            'request' => $this->request,
79
            'response' => $this->response,
80
            'baseUrl' => '/authors',
81
            'rendererClass' => 'CakeDC/Api.Raw'
82
        ]);
83
        $this->assertInstanceOf(FallbackService::class, $this->Service);
84
    }
85
86
    /**
87
     * Test load value method
88
     *
89
     * @return void
90
     * @expectedException \Cake\Routing\Exception\MissingRouteException
91
     */
92
    public function testActionNotFound()
93
    {
94
        $this->_initializeRequest([
95
            'params' => [
96
                'service' => 'authors',
97
            ]
98
        ], 'DELETE');
99
        $service = $this->request->getParam('service');
100
        $options = [
101
            'version' => null,
102
            'service' => $service,
103
            'request' => $this->request,
104
            'response' => $this->response,
105
            'baseUrl' => '/authors'
106
        ];
107
        $Service = ServiceRegistry::get($service, $options);
0 ignored issues
show
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...
108
        $this->assertTrue($Service instanceof Service);
109
        $this->assertEquals('authors', $Service->getName());
110
111
        $this->assertTextEquals('/authors', $Service->getBaseUrl());
112
        $action = $Service->buildAction();
0 ignored issues
show
Unused Code introduced by
$action 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...
113
    }
114
115
    /**
116
     * Test load value method
117
     *
118
     * @return void
119
     */
120
    public function testActionInitialize()
121
    {
122
        $this->_initializeRequest([
123
            'params' => [
124
                'service' => 'authors',
125
            ]
126
        ]);
127
        $service = $this->request->getParam('service');
128
        $options = [
129
            'version' => null,
130
            'service' => $service,
131
            'request' => $this->request,
132
            'response' => $this->response,
133
            'baseUrl' => '/authors'
134
        ];
135
        $Service = ServiceRegistry::get($service, $options);
0 ignored issues
show
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...
136
        $this->assertTrue($Service instanceof Service);
137
        $this->assertEquals('authors', $Service->getName());
138
139
        $this->assertTextEquals('/authors', $Service->getBaseUrl());
140
        $action = $Service->buildAction();
141
        $this->assertEquals('authors', $action->getService()->getName());
142
        $this->assertEquals('authors', $action->getTable()->getTable());
143
    }
144
145
    /**
146
     * Test nested action generation.
147
     *
148
     * @return void
149
     */
150
    public function testNestedActionInitialize()
151
    {
152
        $this->_initializeRequest([
153
            'params' => [
154
                'service' => 'authors',
155
                'pass' => [
156
                    '1',
157
                    'articles'
158
                ]
159
            ]
160
        ]);
161
        $service = $this->request->getParam('service');
162
        $options = [
163
            'version' => null,
164
            'service' => $service,
165
            'request' => $this->request,
166
            'response' => $this->response,
167
            'baseUrl' => '/authors/1/articles',
168
        ];
169
        $Service = ServiceRegistry::get($service, $options);
0 ignored issues
show
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...
170
        $this->assertTrue($Service instanceof Service);
171
        $this->assertTextEquals('/authors/1/articles', $Service->getBaseUrl());
172
        $action = $Service->buildAction();
173
        $this->assertEquals('1', $action->getParentId());
174
        $this->assertEquals('author_id', $action->getParentIdName());
175
        $this->assertEquals('articles', $action->getService()->getName());
176
        $this->assertEquals('authors', $action->getService()->getParentService()->getName());
177
        $this->assertEquals('articles', $action->getTable()->getTable());
178
    }
179
180
    /**
181
     * Test nested action generation.
182
     *
183
     * @return void
184
     */
185
    public function testInitializeActionStoredAsExistsClass()
186
    {
187
        $this->_initializeRequest([
188
            'params' => [
189
                'service' => 'articles',
190
                'pass' => [
191
                    'tag',
192
                    '1'
193
                ],
194
                'post' => [
195
                    'tag_id' => 1
196
                ]
197
            ]
198
        ], 'PUT');
199
        $service = $this->request->getParam('service');
200
        $options = [
201
            'version' => null,
202
            'service' => $service,
203
            'request' => $this->request,
204
            'response' => $this->response,
205
            'baseUrl' => '/articles/tag/1',
206
        ];
207
        $Service = ServiceRegistry::get($service, $options);
0 ignored issues
show
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...
208
        $this->assertTrue($Service instanceof Service);
209
        $this->assertTextEquals('/articles/tag/1', $Service->getBaseUrl());
210
        $action = $Service->buildAction();
211
        $this->assertEquals('CakeDC\Api\Test\App\Service\Action\ArticlesTagAction', get_class($action));
212
    }
213
214
    /**
215
     * Test nested action generation.
216
     *
217
     * @return void
218
     */
219
    public function testInitializeActionByServiceConfigMap()
220
    {
221
        $actionClass = 'CakeDC\Api\Test\App\Service\Action\Author\IndexAction';
222
        $this->_addSettingByPath('Service.authors.options', [
223
            'classMap' => [
224
                'index' => $actionClass
225
            ]
226
        ]);
227
        $config = require(CONFIG . 'api.php');
228
        Configure::write($config);
229
        $this->_initializeRequest([
230
            'params' => [
231
                'service' => 'authors',
232
                'pass' => [],
233
            ]
234
        ], 'GET');
235
        $service = $this->request->getParam('service');
236
        $version = null;
237
        $options = [
238
            'version' => $version,
239
            'service' => $service,
240
            'request' => $this->request,
241
            'response' => $this->response,
242
            'baseUrl' => '/authors'
243
        ];
244
        $options += (new ConfigReader())->serviceOptions($service, $version);
245
        $Service = ServiceRegistry::get($service, $options);
0 ignored issues
show
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...
246
        $this->assertTrue($Service instanceof Service);
247
        $this->assertTextEquals('/authors', $Service->getBaseUrl());
248
        $action = $Service->buildAction();
249
        $this->assertEquals($actionClass, get_class($action));
250
        $this->assertTextEquals('custom action applied', $action->process());
251
    }
252
}
253