Passed
Push — master ( c86ace...13d1a4 )
by Akmal
01:06
created

RouteTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 10
1
<?php declare(strict_types=1);
2
3
namespace OpenEngine\Mika\Core\Components\Route\Tests;
4
5
use OpenEngine\Mika\Core\Components\Di\DiConfig;
6
use OpenEngine\Mika\Core\Components\Di\Exceptions\MethodNotFoundException;
7
use OpenEngine\Mika\Core\Components\Di\Exceptions\MissingMethodArgumentException;
8
use OpenEngine\Mika\Core\Components\Http\Exceptions\NotFoundHttpException;
9
use OpenEngine\Mika\Core\Components\Http\Message\Stream\StreamFactory;
10
use OpenEngine\Mika\Core\Components\Route\Interfaces\RouteConfigInterface;
11
use OpenEngine\Mika\Core\Components\Route\Interfaces\RouteInterface;
12
use OpenEngine\Mika\Core\Components\Route\Route;
13
use OpenEngine\Mika\Core\Components\Route\RouteConfig;
14
use OpenEngine\Mika\Core\Helpers\Path;
15
use OpenEngine\Mika\Core\Mika;
16
use PHPUnit\Framework\TestCase;
17
use Psr\Http\Message\StreamFactoryInterface;
18
19
class RouteTest extends TestCase
20
{
21
    public function testAddingRoute(): void
22
    {
23
        $_SERVER['REQUEST_URI'] = '/foo/bar/baz?key=val&foo2';
24
25
        Mika::run($this->getDiConfig());
26
        $this->expectOutputString('I am an answer from BarController::bazAction');
27
    }
28
29
    public function testDefaultControllerOfRoute(): void
30
    {
31
        $_SERVER['REQUEST_URI'] = '/foo';
32
33
        Mika::run($this->getDiConfig());
34
        $this->expectOutputString('I am default method of the DefaultController');
35
    }
36
37
    public function testSolvingMethodDepends(): void
38
    {
39
        $_SERVER['REQUEST_URI'] = '/bar/bar';
40
41
        Mika::run($this->getDiConfig());
42
        $this->expectOutputString('called method is: GET');
43
    }
44
45
    public function testSolvingMethodDependsWithParams(): void
46
    {
47
        $_SERVER['REQUEST_URI'] = '/bar/bar/frameworkName';
48
        $_GET['name'] = 'Mika';
49
        $_GET['type'] = 'Framework';
50
        $_GET['age'] = '31';
51
52
        Mika::run($this->getDiConfig());
53
        $this->expectOutputString('method: GET; name: Mika; type: Framework; age: 31');
54
    }
55
56
    public function testExceptionMissingArgument(): void
57
    {
58
        $_SERVER['REQUEST_URI'] = '/bar/bar/frameworkName';
59
        $_GET['name'] = 'Mika';
60
        $_GET['type'] = 'Framework';
61
        $_GET['age'] = '31';
62
63
        unset($_GET['name']);
64
65
        $this->expectException(MissingMethodArgumentException::class);
66
        Mika::run($this->getDiConfig());
67
    }
68
69
    public function testExceptionMethodNotFound(): void
70
    {
71
        $_SERVER['REQUEST_URI'] = '/bar/bar/unknown';
72
73
        $this->expectException(MethodNotFoundException::class);
74
        Mika::run($this->getDiConfig());
75
    }
76
77
    public function testExceptionNotFound(): void
78
    {
79
        $_SERVER['REQUEST_URI'] = '/bar/unknown';
80
81
        $this->expectException(NotFoundHttpException::class);
82
        Mika::run($this->getDiConfig());
83
    }
84
85
    protected function setUp()
86
    {
87
        Path::setRoot(getenv('MIKA_ROOT_DIR'));
88
89
        $this->setUpGlobals();
90
    }
91
92
    private function setUpGlobals(): void
93
    {
94
        $_SERVER['REQUEST_SCHEME'] = 'http';
95
        $_SERVER['HTTP_HOST'] = 'kadirov.org';
96
    }
97
98
    private function getDiConfig(): DiConfig
99
    {
100
        $routeConfig = new RouteConfig();
101
        $routeConfig->register(
102
            'foo',
103
            'OpenEngine\Mika\Core\Components\Route\Tests\Dummy\Foo\Controllers'
104
        );
105
106
        $routeConfig->register(
107
            'bar',
108
            'OpenEngine\Mika\Core\Components\Route\Tests\Dummy\Bar\Controllers'
109
        );
110
111
        $diConfig = new DiConfig();
112
        $diConfig->register(StreamFactoryInterface::class, StreamFactory::class);
113
        $diConfig->register(RouteInterface::class, Route::class);
114
        $diConfig->registerObject(RouteConfigInterface::class, $routeConfig);
115
116
        return $diConfig;
117
    }
118
}
119