Completed
Push — master ( 8cf0aa...9a91d5 )
by Mehmet
02:56
created

myRouterClass::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace tests;
4
5
use Selami;
6
use Zend\Diactoros\ServerRequestFactory;
7
use ReflectionObject;
8
9
class myRouterClass extends \PHPUnit_Framework_TestCase
10
{
11
    private $config = [
12
        'folder'                => '',
13
        'default_return_type'   =>'html'
14
    ];
15
16
    private $routes = [
17
        'home' => ['get', '/', 'app/main'],
18
        ['get', '/json', 'app/json', 'json'],
19
        ['post', '/json', 'app/redirect', 'redirect'],
20
        'alias' => ['get', '/alias', 'app/alias'],
21
    ];
22
    private $request = null;
23
24
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp 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
setUp uses the super-global variable $_FILES 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
setUp 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
setUp 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...
Coding Style introduced by
setUp uses the super-global variable $_COOKIE 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...
25
    {
26
        $basedir = dirname(__DIR__) . '/app';
27
        $this->config['base_dir']   = $basedir;
28
        $this->config['app_dir']    = $basedir;
29
        $_SERVER                    = [];
30
        $_FILES                     = [];
31
        $_GET                       = [];
32
        $_POST                      = [];
33
        $_SERVER                    = [];
34
        $_COOKIE                    = [];
35
        $_SERVER['DOCUMENT_ROOT']   = $basedir."/htdocs";
36
        $_SERVER['SCRIPT_NAME']     = '/index.php';
37
        $_SERVER['REQUEST_URI']     = '/alias';
38
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
39
        $_SERVER['SERVER_NAME']     = 'Selami';
40
        $_SERVER['SERVER_PORT']     = '8080';
41
        $_SERVER['REQUEST_METHOD']  = 'GET';
42
        $_SERVER['QUERY_STRING']    = 'p1=1&p2=2';
43
        $_SERVER['HTTPS']           = '';
44
        $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
45
        $_SERVER['REQUEST_TIME']    = time();
46
        $this->request              = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
47
    }
48
49
    /**
50
     * @test
51
     * @dataProvider extractFolderDataProvider
52
     * @param $requestedPath string
53
     * @param $folder string
54
     * @param $expected string
55
     */
56
    public function shouldExtractRouteFromURLSuccessfully($requestedPath, $folder, $expected)
57
    {
58
        $router = new Selami\Router(
59
            $this->routes,
60
            $this->config['default_return_type'],
61
            $this->request->getMethod(),
62
            $this->request->getUri()->getPath(),
63
            $this->config['folder']
64
        );
65
        $reflector = new ReflectionObject($router);
66
        $method = $reflector->getMethod('extractFolder');
67
        $method->setAccessible(true);
68
        $result = $method->invoke($router, $requestedPath, $folder );
69
        $this->assertEquals(
70
            $expected,
71
            $result,
72
            "extractFolder did not correctly extract the requested path for sub folder"
73
         );
74
    }
75
76
    public function extractFolderDataProvider()
77
    {
78
        return [
79
            ['/', '', '/'],
80
            ['', '', '/'],
81
            ['/admin/dashboard', 'admin', '/dashboard']
82
        ];
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function shouldCorrectlyInstantiateRouter()
89
    {
90
        $router = new Selami\Router(
91
            $this->routes,
92
            $this->config['default_return_type'],
93
            $this->request->getMethod(),
94
            $this->request->getUri()->getPath(),
95
            $this->config['folder']
96
        );
97
        $this->assertInstanceOf('Selami\Router', $router);
98
        $this->assertAttributeContains('GET', 'method', $router, "Router didn't correctly return method as GET.");
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function shouldCorrectlyReturnAliases()
105
    {
106
        $router = new Selami\Router(
107
            $this->routes,
108
            $this->config['default_return_type'],
109
            $this->request->getMethod(),
110
            $this->request->getUri()->getPath(),
111
            $this->config['folder']
112
        );
113
        $reflector = new ReflectionObject($router);
114
        $method = $reflector->getMethod('getAliases');
115
        $method->setAccessible(true);
116
        $aliases = $method->invoke($router);
117
        $this->assertArrayHasKey('home', $aliases, "Router didn't correctly return aliases");
118
        $this->assertArrayHasKey('alias', $aliases, "Router didn't correctly return aliases");
119
        $this->assertEquals('/', $aliases['home'], "Router didn't correctly return aliases");
120
        $this->assertEquals('/alias', $aliases['alias'], "Router didn't correctly return aliases");
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function shouldCorrectlyReturnRouteAndRouteAliases()
127
    {
128
        $router = new Selami\Router(
129
            $this->routes,
130
            $this->config['default_return_type'],
131
            $this->request->getMethod(),
132
            $this->request->getUri()->getPath(),
133
            $this->config['folder']
134
        );
135
        $routeInfo = $router->getRoute();
136
        $this->assertArrayHasKey('aliases', $routeInfo, "Router didn't correctly return route data");
137
        $this->assertArrayHasKey('controller', $routeInfo['route'], "Router didn't correctly return route data");
138
        $this->assertArrayHasKey('action', $routeInfo['route'], "Router didn't correctly return router data");
139
        $this->assertEquals('app', $routeInfo['route']['controller'], "Router didn't correctly return router data");
140
        $this->assertEquals('alias', $routeInfo['route']['action'], "Router didn't correctly return router data");
141
    }
142
143
    /**
144
     * @test
145
     */
146 View Code Duplication
    public function shouldCorrectlyReturnMethodNotAllowed()
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
shouldCorrectlyReturnMethodNotAllowed 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
shouldCorrectlyReturnMethodNotAllowed 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
shouldCorrectlyReturnMethodNotAllowed 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...
Coding Style introduced by
shouldCorrectlyReturnMethodNotAllowed uses the super-global variable $_COOKIE 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
shouldCorrectlyReturnMethodNotAllowed uses the super-global variable $_FILES 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...
147
    {
148
        $_SERVER['REQUEST_METHOD'] = 'POST';
149
        $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
150
        $router = new Selami\Router(
151
            $this->routes,
152
            $this->config['default_return_type'],
153
            $this->request->getMethod(),
154
            $this->request->getUri()->getPath(),
155
            $this->config['folder']
156
        );
157
        $routeInfo = $router->getRoute();
158
        $this->assertEquals('405', $routeInfo['route']['status'], "Router didn't correctly return Method Not Allowed");
159
    }
160
161
    /**
162
     * @test
163
     */
164 View Code Duplication
    public function shouldCorrectlyReturnNotFound()
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
shouldCorrectlyReturnNotFound 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
shouldCorrectlyReturnNotFound 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
shouldCorrectlyReturnNotFound 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...
Coding Style introduced by
shouldCorrectlyReturnNotFound uses the super-global variable $_COOKIE 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
shouldCorrectlyReturnNotFound uses the super-global variable $_FILES 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...
165
    {
166
        $_SERVER['REQUEST_URI'] = '/notexists';
167
        $_SERVER['REQUEST_METHOD'] = 'POST';
168
        $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
169
        $router = new Selami\Router(
170
            $this->routes,
171
            $this->config['default_return_type'],
172
            $this->request->getMethod(),
173
            $this->request->getUri()->getPath(),
174
            $this->config['folder']
175
        );
176
        $routeInfo = $router->getRoute();
177
        $this->assertEquals('404', $routeInfo['route']['status'], "Router didn't correctly returnNot FOund");
178
    }
179
}
180