Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

HasMatcherTraitTest::testRouteDynamic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Tests\Router\Traits;
4
5
use Nip\Request;
6
use Nip\Router\Route\Route;
7
use Nip\Router\RouteFactory;
8
use Nip\Router\Router;
9
use Nip\Router\Tests\AbstractTest;
10
use Nip\Router\Tests\Fixtures\Application\Library\Router\Route\StandardRoute;
11
12
/**
13
 * Class HasMatcherTraitTest
14
 * @package Nip\Router\Tests\Router\Traits
15
 */
16
class HasMatcherTraitTest extends AbstractTest
17
{
18
19
    public function testRouteLiteral()
20
    {
21
        $router = new Router();
22
        $collection = $router->getRoutes();
23
24
        RouteFactory::generateLiteralRoute($collection, "admin.index", Route::class, "/admin", "/index");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 22 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateLiteralRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
25
        RouteFactory::generateLiteralRoute($collection, "api.index", Route::class, "/api", "/index");
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 22 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateLiteralRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
27
        $request = Request::create('/api/404');
28
        $router->route($request);
29
        self::assertNull($router->getCurrent());
30
31
        $request = Request::create('/api/index');
32
        $params = $router->route($request);
33
        self::assertSame([], $params);
34
35
        $currentRoute = $router->getCurrent();
36
        self::assertInstanceOf(Route::class, $currentRoute);
37
        self::assertSame('api.index', $currentRoute->getName());
38
39
        $request = Request::create('/admin/index');
40
        $router->route($request);
41
        self::assertSame('admin.index', $router->getCurrent()->getName());
42
    }
43
44
    public function testRouteDynamic()
45
    {
46
        $router = new Router();
47
        $collection = $router->getRoutes();
48
49
        RouteFactory::generateStandardRoute($collection, "admin.standard", StandardRoute::class, "/admin",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 47 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateStandardRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
            '/:controller/:action', ['module' => 'admin']);
51
        RouteFactory::generateStandardRoute($collection, "api.standard", StandardRoute::class, "/api",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 47 can also be of type array<integer,object<Nip\Router\Route\Route>>; however, Nip\Router\RouteFactory::generateStandardRoute() does only seem to accept object<Nip\Router\RouteCollection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
            '/:controller/:action', ['module' => 'api']);
53
54
        $request = Request::create('/api/pages/delete');
55
        self::assertSame(
56
            ['module' => 'api', 'controller' => 'pages', 'action' => 'delete'],
57
            $router->route($request)
58
        );
59
60
        $request = Request::create('/admin/pages/delete');
61
        self::assertSame(
62
            ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete'],
63
            $router->route($request)
64
        );
65
    }
66
}
67