Passed
Push — master ( 40eec0...6215ec )
by Darío
02:02
created

RouterTest::testSimpleRouteMatching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 27
rs 9.7666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RouterTest::testSimpleRouteMatch() 0 19 1
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace DroneTest\Util;
12
13
use Drone\Mvc\Router;
14
use Drone\Mvc\AbstractController;
15
use Drone\Mvc\AbstractModule;
16
use Drone\Mvc\ModuleFactory;
17
use Drone\Mvc\Exception\MethodExecutionNotAllowedException;
18
use PHPUnit\Framework\TestCase;
19
20
class RouterTest extends TestCase
21
{
22
    /**
23
     * Tests if the router can create an instance of a class
24
     *
25
     * @return null
26
     */
27
    public function testSimpleRouteMatch()
28
    {
29
        $router = new Router();
30
31
        # default route for App module
32
        $router->addRoute([
33
            'App' => [
34
                'module'     => 'App',
35
                'controller' => 'Index',
36
                'view'       => 'home'
37
            ],
38
        ]);
39
40
        $router->setIdentifiers('App', 'Index', 'home');
41
        $router->match();
42
43
        $ctrl = $router->getController();
44
45
        $this->assertEquals("App\Index", get_class($ctrl));
46
    }
47
48
    /**
49
     * Tests if the router can create an instance of a class with non-default class name builder
50
     *
51
     * @return null
52
     */
53
    public function testSimpleRouteMatchWithParticularName()
54
    {
55
        $router = new Router();
56
57
        # default route for App module
58
        $router->addRoute([
59
            'App' => [
60
                'module'     => 'App',
61
                'controller' => 'Index',
62
                'view'       => 'about'
63
            ],
64
        ]);
65
66
        $router->setIdentifiers('App', 'Index', 'about');
67
68
        $router->setClassNameBuilder(function($module, $class) {
69
            return "\\$module\Controller\\$class";
70
        });
71
72
        $router->match();
73
74
        $ctrl = $router->getController();
75
76
        $this->assertEquals("App\Controller\Index", get_class($ctrl));
77
    }
78
79
    /**
80
     * Tests method execution behaviour handled by the a module
81
     *
82
     * @return null
83
     */
84
    public function testModuleAndControllerComposition()
85
    {
86
        $router = new Router();
87
88
        # default route for App module
89
        $router->addRoute([
90
            'App' => [
91
                'module'     => 'App',
92
                'controller' => 'Index',
93
                'view'       => 'about'
94
            ],
95
        ]);
96
97
        $router->setIdentifiers('App', 'Index', 'about');
98
99
        $router->setClassNameBuilder(function($module, $class) {
100
            return "\\$module\Controller\\$class";
101
        });
102
103
        $router->match();
104
105
        # inject the module dependency to the controller
106
        $router->getController()->setModule(ModuleFactory::create("App"));
0 ignored issues
show
Bug introduced by
Are you sure the usage of Drone\Mvc\ModuleFactory::create('App') targeting Drone\Mvc\ModuleFactory::create() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Drone\Mvc\ModuleFactory::create('App') of type null is incompatible with the type Drone\Mvc\AbstractModule expected by parameter $module of Drone\Mvc\AbstractController::setModule(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        $router->getController()->setModule(/** @scrutinizer ignore-type */ ModuleFactory::create("App"));
Loading history...
107
108
        $this->assertNotTrue($router->getController()->getModule()->executionIsAllowed());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $router->getController()...)->executionIsAllowed() targeting Drone\Mvc\AbstractModule::executionIsAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
110
        $errorObject = null;
111
112
        try {
113
            $router->run();
114
        }
115
        catch (\Exception $e)
116
        {
117
            $errorObject = ($e instanceof MethodExecutionNotAllowedException);
118
        }
119
        finally
120
        {
121
            $this->assertTrue($errorObject, $e->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
122
        }
123
    }
124
}
125
126
/*
127
|--------------------------------------------------------------------------
128
| Controller class
129
|--------------------------------------------------------------------------
130
|
131
| This is a simple controller implementing AbstractController.
132
|
133
*/
134
135
namespace App;
136
137
use Drone\Mvc\AbstractController;
138
139
class Index extends AbstractController
140
{
141
    public function home()
142
    {
143
        return [];
144
    }
145
}
146
147
/*
148
|--------------------------------------------------------------------------
149
| Another Controller class
150
|--------------------------------------------------------------------------
151
|
152
| This is a simple controller implementing AbstractController. In order to
153
| build a framework behavior, we need all classes inside Controller namespace.
154
|
155
*/
156
157
namespace App\Controller;
158
159
use Drone\Mvc\AbstractController;
160
161
class Index extends AbstractController
162
{
163
    public function about()
164
    {
165
        return [];
166
    }
167
}
168
169
/*
170
|--------------------------------------------------------------------------
171
| Module Class
172
|--------------------------------------------------------------------------
173
|
174
| Each module could have a Module class that handles method execution. It's
175
| useful for execute some code before method execution or for stop it.
176
|
177
*/
178
179
namespace App;
180
181
use Drone\Mvc\AbstractController;
182
use Drone\Mvc\AbstractModule;
183
184
class Module extends AbstractModule
185
{
186
    public function init()
187
    {
188
        # disallowing method execution
189
        $this->disallowExecution();
190
    }
191
}