PerformanceBench::provideDynamicRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 9.7333
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of Flight Routing.
5
 *
6
 * PHP version 8.0 and above required
7
 *
8
 * @author    Divine Niiquaye Ibok <[email protected]>
9
 * @copyright 2019 Divine Niiquaye Ibok (https://divinenii.com/)
10
 * @license   https://opensource.org/licenses/BSD-3-Clause License
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
namespace Flight\Routing\Tests\Benchmarks;
17
18
use Flight\Routing\Exceptions\MethodNotAllowedException;
19
use Flight\Routing\Interfaces\RouteMatcherInterface;
20
use Flight\Routing\{RouteCollection, Router};
21
22
/**
23
 * @Groups({"performance"})
24
 */
25
final class PerformanceBench extends RouteBench
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createDispatcher(string $cache = null): RouteMatcherInterface
31
    {
32
        $router = new Router(null, $cache);
33
        $router->setCollection(static function (RouteCollection $routes): void {
34
            for ($i = 0; $i < self::MAX_ROUTES; ++$i) {
35
                if (199 === $i) {
36
                    $routes->add('//localhost.com/route'.$i, ['GET'])->bind('static-'.$i);
37
                    $routes->add('//{host}/route{foo}/'.$i, ['GET'])->bind('no-static-'.$i);
38
                    continue;
39
                }
40
41
                $routes->add('/route'.$i, ['GET'])->bind('static-'.$i);
42
                $routes->add('/route{foo}/'.$i, ['GET'])->bind('no-static-'.$i);
43
            }
44
        });
45
46
        return $router;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function provideStaticRoutes(): iterable
53
    {
54
        yield 'first' => [
55
            'method' => 'GET',
56
            'route' => '/route0',
57
            'result' => ['handler' => null, 'prefix' => '/route0', 'path' => '/route0', 'methods' => ['GET' => true], 'name' => 'static-0'],
58
        ];
59
60
        yield 'middle' => [
61
            'method' => 'GET',
62
            'route' => '//localhost/route199',
63
            'result' => ['handler' => null, 'hosts' => ['localhost' => true], 'prefix' => '/route199', 'path' => '/route199', 'methods' => ['GET' => true], 'name' => 'static-199'],
64
        ];
65
66
        yield 'last' => [
67
            'method' => 'GET',
68
            'route' => '/route399',
69
            'result' => ['handler' => null, 'prefix' => '/route399', 'path' => '/route399', 'methods' => ['GET' => true], 'name' => 'static-399'],
70
        ];
71
72
        yield 'invalid-method' => [
73
            'method' => 'PUT',
74
            'route' => '/route399',
75
            'result' => MethodNotAllowedException::class,
76
        ];
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function provideDynamicRoutes(): iterable
83
    {
84
        yield 'first' => [
85
            'method' => 'GET',
86
            'route' => '/routebar/0',
87
            'result' => ['handler' => null, 'prefix' => '/route', 'path' => '/route{foo}/0', 'methods' => ['GET' => true], 'name' => 'not-static-0', ['arguments' => ['foo' => 'bar']]],
88
        ];
89
90
        yield 'middle' => [
91
            'method' => 'GET',
92
            'route' => '//localhost/routebar/199',
93
            'result' => ['handler' => null, 'hosts' => ['{host}' => true], 'prefix' => '/route', 'path' => '/route{foo}/199', 'methods' => ['GET' => true], 'name' => 'not-static-199', ['arguments' => ['foo' => 'bar']]],
94
        ];
95
96
        yield 'last' => [
97
            'method' => 'GET',
98
            'route' => '/routebar/399',
99
            'result' => ['handler' => null, 'prefix' => '/route', 'path' => '/route{foo}/399', 'methods' => ['GET' => true], 'name' => 'not-static-399', ['arguments' => ['foo' => 'bar']]],
100
        ];
101
102
        yield 'invalid-method' => [
103
            'method' => 'PUT',
104
            'route' => '/routebar/399',
105
            'result' => MethodNotAllowedException::class,
106
        ];
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function provideOtherScenarios(): iterable
113
    {
114
        yield 'non-existent' => [
115
            'method' => 'GET',
116
            'route' => '/testing',
117
            'result' => null,
118
        ];
119
    }
120
}
121