Completed
Pull Request — dev (#11)
by
unknown
04:51
created

SnappyRouterTest::testNoHandlerFoundException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Vectorface\SnappyRouterTests;
4
5
use Vectorface\SnappyRouter\SnappyRouter;
6
use Vectorface\SnappyRouter\Config\Config;
7
use Vectorface\SnappyRouter\Di\Di;
8
use Vectorface\SnappyRouter\Plugin\PluginInterface;
9
use Vectorface\SnappyRouter\Handler\AbstractHandler;
10
use Vectorface\SnappyRouter\Handler\ControllerHandler;
11
12
use \PHPUnit_Framework_TestCase;
13
14
/**
15
 * Tests the main SnappyRouter class.
16
 * @copyright Copyright (c) 2014, VectorFace, Inc.
17
 * @author Dan Bruce <[email protected]>
18
 */
19
class SnappyRouterTest extends PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * Returns a standard router config array.
23
     * @return array A standard router config.
24
     */
25
    private function getStandardConfig()
26
    {
27
        return array(
28
            Config::KEY_DI => 'Vectorface\SnappyRouter\Di\Di',
29
            Config::KEY_HANDLERS => array(
30
                'BogusCliHandler' => array(
31
                    Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\CliTaskHandler'
32
                ),
33
                'ControllerHandler' => array(
34
                    Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\ControllerHandler',
35
                    Config::KEY_OPTIONS => array(
36
                        ControllerHandler::KEY_BASE_PATH => '/',
37
                        Config::KEY_CONTROLLERS => array(
38
                            'TestController' => 'Vectorface\SnappyRouterTests\Controller\TestDummyController'
39
                        ),
40
                        Config::KEY_PLUGINS => array(
41
                            'TestPlugin'     => array(
42
                                Config::KEY_CLASS => 'Vectorface\SnappyRouterTests\Plugin\TestPlugin',
43
                                Config::KEY_OPTIONS => array()
44
                            ),
45
                            'AnotherPlugin'  => 'Vectorface\SnappyRouterTests\Plugin\TestPlugin'
46
                        )
47
                    )
48
                ),
49
                'CliHandler' => array(
50
                    Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\CliTaskHandler',
51
                    Config::KEY_OPTIONS => array(
52
                        'tasks' => array(
53
                            'TestTask' => 'Vectorface\SnappyRouterTests\Task\DummyTestTask'
54
                        )
55
                    )
56
                )
57
            )
58
        );
59
    }
60
61
    /**
62
     * An overview of how to use the SnappyRouter class.
63
     * @test
64
     */
65
    public function synopsis()
0 ignored issues
show
Coding Style introduced by
synopsis uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
synopsis uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
synopsis uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
66
    {
67
        // an example configuration of the router
68
        $config = $this->getStandardConfig();
69
        // instantiate the router
70
        $router = new SnappyRouter(new Config($config));
71
72
        // an example MVC request
73
        $_SERVER['REQUEST_URI'] = '/Test/test';
74
        $_SERVER['REQUEST_METHOD'] = 'GET';
75
        $_GET['param'] = 'value';
76
        $response = $router->handleRoute('apache2handler');
77
78
        $expectedResponse = 'This is a test service.';
79
        $this->assertEquals($expectedResponse, $response);
80
81
        unset($_SERVER['REQUEST_URI']);
82
        $_GET = array();
83
        $_POST = array();
84
    }
85
86
    /**
87
     * Tests that the router handles a generic exception.
88
     */
89
    public function testGenericException()
90
    {
91
        $config = $this->getStandardConfig();
92
        $router = new SnappyRouter(new Config($config));
93
94
        // an example MVC request
95
        $path = '/Test/genericException';
96
        $query = array('jsoncall' => 'testMethod');
97
        $response = $router->handleHttpRoute($path, $query, array(), 'get');
98
99
        $expectedResponse = 'A generic exception.';
100
        $this->assertEquals($expectedResponse, $response);
101
    }
102
103
    /**
104
     * Tests that an empty config array results in no handler being found.
105
     */
106
    public function testNoHandlerFoundException()
107
    {
108
        // turn on debug mode so we get a verbose description of the exception
109
        $router = new SnappyRouter(new Config(array(
110
            'debug' => true
111
        )));
112
113
        // an example MVC request
114
        $path = '/Test/test';
115
        $query = array('jsoncall' => 'testMethod');
116
        $response = $router->handleHttpRoute($path, $query, array(), 'get');
117
        $this->assertEquals('No handler responded to the request.', $response);
118
    }
119
120
    /**
121
     * Tests that an exception is thrown if a handler class does not exist.
122
     * @expectedException Exception
123
     * @expectedExceptionMessage Cannot instantiate instance of Vectorface\SnappyRouter\Handler\NonexistantHandler
124
     */
125
    public function testInvalidHandlerClass()
126
    {
127
        $config = $this->getStandardConfig();
128
        $config[Config::KEY_HANDLERS]['InvalidHandler'] = array(
129
            'class' => 'Vectorface\SnappyRouter\Handler\NonexistantHandler'
130
        );
131
        $router = new SnappyRouter(new Config($config));
132
133
        // an example MVC request
134
        $path = '/Test/test';
135
        $query = array('jsoncall' => 'testMethod');
136
        $response = $router->handleHttpRoute($path, $query, array(), 'get');
137
138
        $expectedResponse = 'No handler responded to request.';
139
        $this->assertEquals($expectedResponse, $response);
140
    }
141
142
    /**
143
     * Tests that the CLI routing functionality works.
144
     */
145 View Code Duplication
    public function testStandardCliRoute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testStandardCliRoute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
146
    {
147
        $config = $this->getStandardConfig();
148
        $router = new SnappyRouter(new Config($config));
149
150
        $_SERVER['argv'] = array(
151
            'dummyScript.php',
152
            '--task',
153
            'TestTask',
154
            '--action',
155
            'testMethod'
156
        );
157
        $_SERVER['argc'] = count($_SERVER['argv']);
158
        $response = $router->handleRoute();
159
160
        $expected = 'Hello World'.PHP_EOL;
161
        $this->assertEquals($expected, $response);
162
    }
163
164
    /**
165
     * Tests a CLI route that throws an exception.
166
     */
167 View Code Duplication
    public function testCliRouteWithException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testCliRouteWithException uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
168
    {
169
        $config = $this->getStandardConfig();
170
        $router = new SnappyRouter(new Config($config));
171
172
        $_SERVER['argv'] = array(
173
            'dummyScript.php',
174
            '--task',
175
            'TestTask',
176
            '--action',
177
            'throwsException'
178
        );
179
        $_SERVER['argc'] = count($_SERVER['argv']);
180
        $response = $router->handleRoute();
181
182
        $expected = 'An exception was thrown.'.PHP_EOL;
183
        $this->assertEquals($expected, $response);
184
    }
185
186
    /**
187
     * Tests that a CLI route with no appropriate handlers throws an
188
     * exception.
189
     */
190 View Code Duplication
    public function testCliRouteWithNoHandler()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testCliRouteWithNoHandler uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
191
    {
192
        $config = $this->getStandardConfig();
193
        $router = new SnappyRouter(new Config($config));
194
195
        $_SERVER['argv'] = array(
196
            'dummyScript.php',
197
            '--task',
198
            'NotDefinedTask',
199
            '--action',
200
            'anyAction'
201
        );
202
        $_SERVER['argc'] = count($_SERVER['argv']);
203
        $response = $router->handleRoute();
204
205
        $expected = 'No CLI handler registered.'.PHP_EOL;
206
        $this->assertEquals($expected, $response);
207
    }
208
}
209