Test Setup Failed
Push — master ( b9b7e5...f50e96 )
by Gabriel
02:03 queued 10s
created

HasMatcherTraitTest::dataRouteDynamic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
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
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 View Code Duplication
    public function testNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $router = new Router();
23
        self::expectException(ResourceNotFoundException::class);
24
25
        $request = Request::create('/404');
26
        $router->matchRequest($request);
27
    }
28
29 View Code Duplication
    public function testRouteLiteral()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    /**
51
     * @dataProvider dataRouteDynamic
52
     */
53
    public function testRouteDynamic($uri, $routeName, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $routeName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        $router = new Router();
56
        $collection = $router->getRoutes();
57
58
        RouteFactory::generateStandardRoute(
59
            $collection,
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 56 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...
60
            "admin.standard",
61
            StandardRoute::class,
62
            "/admin",
63
            '/:controller/:action', ['module' => 'admin']);
64
        RouteFactory::generateStandardRoute(
65
            $collection, "api.standard",
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 56 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...
66
            StandardRoute::class,
67
            "/api",
68
            '/:controller/:action', ['module' => 'api']);
69
70
        RouteFactory::generateStandardRoute(
71
            $collection,
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $router->getRoutes() on line 56 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...
72
            "frontend.standard",
73
            StandardRoute::class,
74
            "/");
75
76
        $request = Request::create($uri);
77
        $matches = $router->matchRequest($request);
78
79
        self::assertArrayHasKey('_route', $matches);
80
81
        foreach ($params as $param=>$value) {
82
            self::assertArrayHasKey($param, $matches);
83
            self::assertSame($matches[$param], $value);
84
        }
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function dataRouteDynamic()
91
    {
92
        return [
93
            ['/api/pages', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'index']],
94
            ['/api/pages/', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'index']],
95
            ['/api/pages/delete', 'api.standard', ['module' => 'api', 'controller' => 'pages', 'action' => 'delete']],
96
            ['/admin/pages/delete', 'admin.standard', ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete']],
97
            ['/users/delete', 'frontend.standard', ['controller' => 'users', 'action' => 'delete']],
98
            ['/users/', 'frontend.standard', ['controller' => 'users', 'action' => 'index']],
99
            ['/users', 'frontend.standard', ['controller' => 'users', 'action' => 'index']],
100
        ];
101
    }
102
}
103