LaravelRouter::testStatic()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace App\BenchMark\Routers;
19
20
use App\BenchMark\AbstractRouter;
21
use Illuminate\Container\Container;
22
use Illuminate\Events\Dispatcher;
23
use Illuminate\Http\Request;
24
use Illuminate\Routing\Router;
25
26
class LaravelRouter extends AbstractRouter
27
{
28
    protected Router $router;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function testStatic(): bool
34
    {
35
        $methods = $this->generator->getMethods();
36
37
        foreach ($methods as $method) {
38
            $path = ($this->strategy)($method);
39
40
            $request = Request::create($path, $method);
41
            $result  = $this->router->dispatch($request)->getContent();
42
43
            if ($result !== 'Hello') {
44
                return false;
45
            }
46
        }
47
48
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function testPath(): bool
55
    {
56
        $methods = $this->generator->getMethods();
57
58
        foreach ($methods as $method) {
59
            $path = ($this->strategy)($method);
60
61
            $request = Request::create($path . 'laravel', $method);
62
            $result  = $this->router->dispatch($request)->getContent();
63
64
            if ($result !== 'laravel') {
65
                return false;
66
            }
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * Test Sub Domain with route path
74
     *
75
     * @return bool
76
     */
77
    public function testSubDomain(): bool
78
    {
79
        $hosts = $this->generator->getHosts();
80
81
        foreach ($hosts as $host) {
82
            $methods = $this->generator->getMethods();
83
84
            foreach ($methods as $method) {
85
                $path = ($this->strategy)($method, $host);
86
                $server   = [];
87
88
                if ($host !== '*') {
89
                    $server['HTTP_HOST'] = $host . 'laravel';
90
                }
91
92
                $request = Request::create($path . 'laravel', $method, [], [], [], $server);
93
                $result  = $this->router->dispatch($request)->getContent();
94
95
                if ($result !== 'laravel') {
96
                    return false;
97
                }
98
            }
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function buildRoutes(array $routes): void
108
    {
109
        $router = new Router(new Dispatcher(), new Container());
110
111
        foreach ($routes as $route) {
112
            $action = [
113
                'uses' => function ($id = 'Hello') {
114
                    return $id;
115
                },
116
            ];
117
118
            if ($route['host'] !== '*') {
119
                $action['domain'] = $route['host'];
120
            }
121
122
            $router->addRoute($route['methods'], $route['pattern'], $action)
123
                ->setWheres($route['constraints']);
124
        }
125
126
        $this->router = $router;
127
    }
128
}
129