Passed
Pull Request — master (#28)
by Fran
08:31
created

RouterTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testPreconditions() 0 12 1
A testCanAccess() 0 13 1
A testRouterBasics() 0 15 1
A testNotFound() 0 7 1
A testPreconditionsNonStrict() 0 12 1
A testGetRoute() 0 10 2
1
<?php
2
3
namespace PSFS\tests\base;
4
5
use PHPUnit\Framework\TestCase;
6
use PSFS\base\config\Config;
7
use PSFS\base\exception\RouterException;
8
use PSFS\base\Router;
9
use PSFS\base\Security;
10
use PSFS\base\types\helpers\AuthHelper;
11
use PSFS\base\types\helpers\SecurityHelper;
12
use PSFS\controller\base\Admin;
13
use PSFS\controller\ConfigController;
14
15
/**
16
 * Class RouterTest
17
 * @package PSFS\tests
18
 */
19
class RouterTest extends TestCase
20
{
21
22
    /**
23
     * @return void
24
     */
25
    public function testRouterBasics()
26
    {
27
        $router = Router::getInstance();
28
        $config = Config::getInstance()->dumpConfig();
29
        $config['debug'] = true;
30
        Config::save($config, []);
31
        $this->assertNotNull($router);
32
        $this->assertInstanceOf(Router::class, $router);
33
        $this->assertTrue(Router::exists(Router::class), "Can't check the namespace");
34
35
        $slugs = $router->getSlugs();
36
        $this->assertNotEmpty($slugs);
37
38
        $routes = $router->getAllRoutes();
39
        $this->assertNotEmpty($routes);
40
    }
41
42
    /**
43
     * @return void
44
     * @throws \Exception
45
     */
46
    public function testNotFound()
47
    {
48
        $this->expectExceptionCode(404);
49
        $this->expectException(\PSFS\base\exception\RouterException::class);
50
        Router::dropInstance();
51
        $router = Router::getInstance();
52
        $router->execute(uniqid(time(), true));
53
    }
54
55
    /**
56
     * @return void
57
     * @throws \PSFS\base\exception\GeneratorException
58
     */
59
    public function testCanAccess()
60
    {
61
        $this->expectExceptionCode(401);
62
        $this->expectException(\PSFS\base\exception\UserAuthException::class);
63
        // Creates a default user
64
        Security::getInstance()->saveUser([
65
            'username' => uniqid('test', true),
66
            'password' => uniqid('test', true),
67
            'profile' => AuthHelper::ADMIN_ID_TOKEN,
68
        ]);
69
        $router = Router::getInstance();
70
        Admin::setTest(true);
71
        $router->execute('/admin/config');
72
    }
73
74
    /**
75
     * @return void
76
     * @throws \Exception
77
     */
78
    public function testPreconditions()
79
    {
80
        $this->expectExceptionCode(404);
81
        $this->expectException(\PSFS\base\exception\RouterException::class);
82
        Router::dropInstance();
83
        $router = Router::getInstance();
84
        SecurityHelper::setTest(true);
85
        Security::setTest(true);
86
        $config = Config::getInstance()->dumpConfig();
87
        $config['allow.double.slashes'] = false;
88
        Config::save($config);
89
        $router->execute('/admin//config');
90
    }
91
92
    /**
93
     * @return void
94
     * @throws \Exception
95
     */
96
    public function testPreconditionsNonStrict()
97
    {
98
        $this->expectException(\PSFS\base\exception\UserAuthException::class);
99
        Router::dropInstance();
100
        $router = Router::getInstance();
101
        SecurityHelper::setTest(true);
102
        Security::setTest(false);
103
        ConfigController::setTest(true);
104
        $config = Config::getInstance()->dumpConfig();
105
        $config['allow.double.slashes'] = true;
106
        Config::save($config);
107
        $router->execute('/admin//config');
108
    }
109
110
    /**
111
     * @return void
112
     */
113
    public function testGetRoute()
114
    {
115
        $router = Router::getInstance();
116
        $this->assertNotNull($router->getRoute(), "Can't gather the homepage route");
117
        $this->assertNotNull($router->getRoute('admin'), "Can't gather the admin route");
118
        $this->assertNotEquals($router->getRoute('admin'), $router->getRoute('admin', true), 'Absolute route is equal than normal');
119
        try {
120
            $router->getRoute(uniqid('test', true));
121
        } catch (\Exception $e) {
122
            $this->assertInstanceOf(RouterException::class, $e, 'Exception is not the expected: ' . get_class($e));
123
        }
124
    }
125
126
}
127