Test Failed
Branch master (4153a4)
by Divine Niiquaye
13:02
created

RealExampleBench::provideOtherScenarios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.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 Flight\Routing\Tests\Benchmarks;
19
20
use Flight\Routing\Exceptions\MethodNotAllowedException;
21
use Flight\Routing\Interfaces\RouteMatcherInterface;
22
use Flight\Routing\{RouteCollection, Router};
23
24
/**
25
 * @Groups({"real"})
26
 */
27
final class RealExampleBench extends RouteBench
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function createDispatcher(string $cache = null): RouteMatcherInterface
33
    {
34
        $router = new Router(null, $cache);
35
        $router->setCollection(static function (RouteCollection $routes): void {
36
            $routes->add('/', ['GET'])->bind('home');
37
            $routes->add('/page/{page_slug:[a-zA-Z0-9\-]+}', ['GET'])->bind('page.show');
38
            $routes->add('/about-us', ['GET'])->bind('about-us');
39
            $routes->add('/contact-us', ['GET'])->bind('contact-us');
40
            $routes->add('/contact-us', ['POST'])->bind('contact-us.submit');
41
            $routes->add('/blog', ['GET'])->bind('blog.index');
42
            $routes->add('/blog/recent', ['GET'])->bind('blog.recent');
43
            $routes->add('/blog/post/{post_slug:[a-zA-Z0-9\-]+}', ['GET'])->bind('blog.post.show');
44
            $routes->add('/blog/post/{post_slug:[a-zA-Z0-9\-]+}/comment', ['POST'])->bind('blog.post.comment');
45
            $routes->add('/shop', ['GET'])->bind('shop.index');
46
            $routes->add('/shop/category', ['GET'])->bind('shop.category.index');
47
            $routes->add('/shop/category/search/{filter_by:[a-zA-Z]+}:{filter_value}', ['GET'])->bind('shop.category.search');
48
            $routes->add('/shop/category/{category_id:\d+}', ['GET'])->bind('shop.category.show');
49
            $routes->add('/shop/category/{category_id:\d+}/product', ['GET'])->bind('shop.category.product.index');
50
            $routes->add('/shop/category/{category_id:\d+}/product/search/{filter_by:[a-zA-Z]+}:{filter_value}', ['GET'])->bind('shop.category.product.search');
51
            $routes->add('/shop/product', ['GET'])->bind('shop.product.index');
52
            $routes->any('/shop[/{type:\w+}[:{filter_by:\@.*}]')->bind('shop.api')->domain('api.shop.com');
53
            $routes->add('/shop/product/search/{filter_by:[a-zA-Z]+}:{filter_value}', ['GET'])->bind('shop.product.search');
54
            $routes->add('/shop/product/{product_id:\d+}', ['GET'])->bind('shop.product.show');
55
            $routes->add('/shop/cart', ['GET'])->bind('shop.cart.show');
56
            $routes->add('/shop/cart', ['PUT', 'DELETE'])->bind('shop.cart.add_remove');
57
            $routes->add('/shop/cart/checkout', ['GET', 'POST'])->bind('shop.cart.checkout');
58
            $routes->add('/admin/login', ['GET', 'POST'])->bind('admin.login');
59
            $routes->add('/admin/logout', ['GET'])->bind('admin.logout');
60
            $routes->add('/admin', ['GET'])->bind('admin.index');
61
            $routes->add('/admin/product', ['GET'])->bind('admin.product.index');
62
            $routes->add('/admin/product/create', ['GET'])->bind('admin.product.create');
63
            $routes->add('/admin/product', ['POST'])->bind('admin.product.store');
64
            $routes->add('/admin/product/{product_id:\d+}', ['GET', 'PUT', 'PATCH', 'DELETE'])->bind('admin.product');
65
            $routes->add('/admin/product/{product_id:\d+}/edit', ['GET'])->bind('admin.product.edit');
66
            $routes->add('/admin/category', ['GET', 'POST'])->bind('admin.category.index_store');
67
            $routes->add('/admin/category/create', ['GET'])->bind('admin.category.create');
68
            $routes->add('/admin/category/{category_id:\d+}', ['GET', 'PUT', 'PATCH', 'DELETE'])->bind('admin.category');
69
            $routes->add('/admin/category/{category_id:\d+}/edit', ['GET'])->bind('admin.category.edit');
70
            $routes->sort(); // Sort routes by giving more priority to static like routes
71
        });
72
73
        return $router;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function provideStaticRoutes(): iterable
80
    {
81
        yield 'first' => [
82
            'method' => 'GET',
83
            'route' => '/',
84
            'result' => ['handler' => null, 'prefix' => '/', 'path' => '/', 'methods' => ['GET' => true], 'name' => 'home'],
85
        ];
86
87
        yield 'middle' => [
88
            'method' => 'GET',
89
            'route' => '/shop/product',
90
            'result' => ['handler' => null, 'prefix' => '/shop/product', 'path' => '/shop/product', 'methods' => ['GET' => true], 'name' => 'shop.product.index'],
91
        ];
92
93
        yield 'last' => [
94
            'method' => 'GET',
95
            'route' => '/admin/category',
96
            'result' => ['handler' => null, 'prefix' => '/admin/category', 'path' => '/admin/category', 'methods' => ['GET' => true, 'POST' => true], 'name' => 'admin.category.index_store'],
97
        ];
98
99
        yield 'invalid-method' => [
100
            'method' => 'PUT',
101
            'route' => '/about-us',
102
            'result' => MethodNotAllowedException::class,
103
        ];
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function provideDynamicRoutes(): iterable
110
    {
111
        yield 'first' => [
112
            'method' => 'GET',
113
            'route' => '/page/hello-word',
114
            'result' => [
115
                'handler' => null,
116
                'prefix' => '/page',
117
                'path' => '/page/{page_slug:[a-zA-Z0-9\-]+}',
118
                'methods' => ['GET' => true],
119
                'name' => 'page.show',
120
                'arguments' => ['page_slug' => 'hello-word'],
121
            ],
122
        ];
123
124
        yield 'middle' => [
125
            'method' => 'GET',
126
            'route' => '//api.shop.com/shop/category_search:filter_by@furniture?value=chair',
127
            'result' => [
128
                'handler' => null,
129
                'hosts' => ['api.shop.com' => true],
130
                'prefix' => '/shop',
131
                'path' => '/shop[/{type:\w+}[:{filter_by:\@.*}]',
132
                'methods' => \array_fill_keys(Router::HTTP_METHODS_STANDARD, true),
133
                'name' => 'shop.api',
134
                'arguments' => ['type' => 'category_search', 'filter_by' => 'furniture'],
135
            ],
136
        ];
137
138
        yield 'last' => [
139
            'method' => 'GET',
140
            'route' => '/admin/category/123/edit',
141
            'result' => [
142
                'handler' => null,
143
                'prefix' => '/admin/category',
144
                'path' => '/admin/category/{category_id:\d+}/edit',
145
                'methods' => ['GET' => true],
146
                'name' => 'admin.category.edit',
147
                'arguments' => ['category_id' => '123'],
148
            ],
149
        ];
150
151
        yield 'invalid-method' => [
152
            'method' => 'PATCH',
153
            'route' => '/shop/category/123',
154
            'result' => MethodNotAllowedException::class,
155
        ];
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function provideOtherScenarios(): iterable
162
    {
163
        yield 'non-existent' => [
164
            'method' => 'GET',
165
            'route' => '/shop/product/awesome',
166
            'result' => null,
167
        ];
168
169
        yield 'longest-route' => [
170
            'method' => 'GET',
171
            'route' => '/shop/category/123/product/search/status:sale',
172
            'result' => [
173
                'handler' => null,
174
                'prefix' => '/admin/category/123/edit',
175
                'path' => '/shop/category/{category_id:\d+}/product/search/{filter_by:[a-zA-Z]+}:{filter_value}',
176
                'methods' => ['GET' => true],
177
                'name' => 'shop.category.product.search',
178
                'arguments' => ['category_id' => '123', 'filter_by' => 'status', 'filter_value' => 'sale'],
179
            ],
180
        ];
181
    }
182
}
183