Test Setup Failed
Push — master ( e4bb3e...ec0b1d )
by Gabriel
02:21 queued 10s
created

HasMatcherTraitTest::testRouteDynamic()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 65
rs 8.7636
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
12
13
/**
14
 * Class HasMatcherTraitTest
15
 * @package Nip\Router\Tests\Router\Traits
16
 */
17
class HasMatcherTraitTest extends AbstractTest
18
{
19
20
    public function testNotFound()
21
    {
22
        $router = new Router();
23
        self::expectException(ResourceNotFoundException::class);
24
25
        $request = Request::create('/404');
26
        $router->matchRequest($request);
27
    }
28
29
    public function testRouteLiteral()
30
    {
31
        $router = new Router();
32
        $collection = $router->getRoutes();
33
34
        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 32 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...
35
        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 32 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...
36
37
        $request = Request::create('/api/index');
38
        $params = $router->matchRequest($request);
39
        self::assertEquals(['_route' => 'api.index'], $params);
40
41
        $currentRoute = $router->getCurrent();
42
        self::assertInstanceOf(Route::class, $currentRoute);
43
        self::assertEquals('api.index', $currentRoute->getName());
44
45
        $request = Request::create('/admin/index');
46
        $router->matchRequest($request);
47
        self::assertEquals('admin.index', $router->getCurrent()->getName());
48
    }
49
50
    public function testRouteDynamic()
51
    {
52
        $router = new Router();
53
        $collection = $router->getRoutes();
54
55
        RouteFactory::generateStandardRoute(
56
            $collection,
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 53 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...
57
            "admin.standard",
58
            StandardRoute::class,
59
            "/admin",
60
            '/:controller/:action', ['module' => 'admin']);
61
        RouteFactory::generateStandardRoute(
62
            $collection, "api.standard",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 53 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...
63
            StandardRoute::class,
64
            "/api",
65
            '/:controller/:action', ['module' => 'api']);
66
67
        RouteFactory::generateStandardRoute(
68
            $collection,
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 53 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...
69
            "frontend.standard",
70
            StandardRoute::class,
71
            "/");
72
73
        $request = Request::create('/api/pages/delete');
74
        self::assertEquals(
75
            ['module' => 'api', 'controller' => 'pages', 'action' => 'delete', '_route' => 'api.standard'],
76
            $router->matchRequest($request)
77
        );
78
79
        $request = Request::create('/api/pages/');
80
        self::assertEquals(
81
            ['module' => 'api', 'controller' => 'pages', 'action' => 'index', '_route' => 'api.standard'],
82
            $router->matchRequest($request)
83
        );
84
85
        $request = Request::create('/api/pages');
86
        self::assertEquals(
87
            ['module' => 'api', 'controller' => 'pages', 'action' => 'index', '_route' => 'api.standard'],
88
            $router->matchRequest($request)
89
        );
90
91
        $request = Request::create('/admin/pages/delete');
92
        self::assertEquals(
93
            ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete', '_route' => 'admin.standard'],
94
            $router->matchRequest($request)
95
        );
96
97
        $request = Request::create('/users/delete');
98
        self::assertEquals(
99
            ['controller' => 'users', 'action' => 'delete', '_route' => 'frontend.standard'],
100
            $router->matchRequest($request)
101
        );
102
103
        $request = Request::create('/users/');
104
        self::assertEquals(
105
            ['controller' => 'users', 'action' => 'index', '_route' => 'frontend.standard'],
106
            $router->matchRequest($request)
107
        );
108
109
        $request = Request::create('/users');
110
        self::assertEquals(
111
            ['controller' => 'users', 'action' => 'index', '_route' => 'frontend.standard'],
112
            $router->matchRequest($request)
113
        );
114
    }
115
}
116