Passed
Push — master ( 70c68f...c76078 )
by Alexander
06:58
created

RouterFactoryTest.php$0 ➔ getUriPrefix()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
1
<?php
2
3
4
namespace Yiisoft\Router\Tests;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yiisoft\Router\Group;
10
use Yiisoft\Router\MatchingResult;
11
use Yiisoft\Router\Route;
12
use Yiisoft\Router\RouterFactory;
13
use Yiisoft\Router\RouterInterface;
14
use Yiisoft\Router\Tests\Support\Container;
15
16
final class RouterFactoryTest extends TestCase
17
{
18
    public function testContainerInjected(): void
19
    {
20
        $container = $this->getContainer();
21
22
        $routes = [
23
            Route::get('/info')->name('api-info'),
24
            Group::create(
25
                '/v2',
26
                [
27
                    Route::get('/user')->name('api-v2-user/index'),
28
                    Route::get('/user/{id}')->name('api-v2-user/view'),
29
                    Group::create(
30
                        '/news',
31
                        [
32
                            Route::get('/post')->name('api-v2-news-post/index'),
33
                            Route::get('/post/{id}')->name('api-v2-news-post/view'),
34
                        ]
35
                    ),
36
                    Group::create(
37
                        '/blog',
38
                        [
39
                            Route::get('/post')->name('api-v2-blog-post/index'),
40
                            Route::get('/post/{id}')->name('api-v2-blog-post/view'),
41
                        ]
42
                    ),
43
                    Route::get('/note')->name('api-v2-note/index'),
44
                    Route::get('/note/{id}')->name('api-v2-note/view'),
45
                ]
46
            ),
47
            Group::create(
48
                '/v2',
49
                [
50
                    Route::get('/user')->name('api-v2-user/index'),
51
                    Route::get('/user/{id}')->name('api-v2-user/view'),
52
                    Group::create(
53
                        '/news',
54
                        [
55
                            Route::get('/post')->name('api-v2-news-post/index'),
56
                            Route::get('/post/{id}')->name('api-v2-news-post/view'),
57
                        ]
58
                    ),
59
                    Group::create(
60
                        '/blog',
61
                        [
62
                            Route::get('/post')->name('api-v2-blog-post/index'),
63
                            Route::get('/post/{id}')->name('api-v2-blog-post/view'),
64
                        ]
65
                    ),
66
                    Route::get('/note')->name('api-v2-note/index'),
67
                    Route::get('/note/{id}')->name('api-v2-note/view'),
68
                ]
69
            )
70
        ];
71
72
        $factory = new RouterFactory($this->getEngineFactory(), $routes);
73
        $router = $factory($container);
74
        $items = $router->getItems();
75
76
        $this->assertAllRoutesAndGroupsHaveContainer($items);
77
    }
78
79
    private function assertAllRoutesAndGroupsHaveContainer(array $items): void
80
    {
81
        $func = function ($item) use (&$func) {
82
            $this->assertTrue($item->hasContainer());
83
            if ($item instanceof Group) {
84
                $items = $item->getItems();
85
                array_walk($items, $func);
86
            }
87
        };
88
        array_walk($items, $func);
89
    }
90
91
    private function getContainer(array $instances = []): ContainerInterface
92
    {
93
        return new Container($instances);
94
    }
95
96
    private function getEngineFactory(): callable
97
    {
98
        return new class() {
99
            public function __invoke(): RouterInterface
100
            {
101
                return new class() extends Group implements RouterInterface {
102
                    public function match(ServerRequestInterface $request): MatchingResult
103
                    {
104
                    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Yiisoft\Router\MatchingResult. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
105
106
                    public function generateAbsolute(string $name, array $parameters = [], string $scheme = null, string $host = null): string
107
                    {
108
                    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
109
110
                    public function generate(string $name, array $parameters = []): string
111
                    {
112
                    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
113
114
                    public function getUriPrefix(): string
115
                    {
116
                    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
117
118
                    public function setUriPrefix(string $name): void
119
                    {
120
                    }
121
122
                    public function getCurrentRoute(): ?Route
123
                    {
124
                    }
125
                };
126
            }
127
        };
128
    }
129
}
130