Test Setup Failed
Push — master ( 7450ca...e4bb3e )
by Gabriel
05:50 queued 15s
created

HasMatcherTraitTest::testRouteLiteral()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Tests\Legacy\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\Legacy\Router\Traits
16
 */
17 View Code Duplication
class HasMatcherTraitTest extends AbstractTest
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
20
    public function testNotFound()
21
    {
22
        $router = new Router();
23
        self::expectException(ResourceNotFoundException::class);
24
25
        $request = Request::create('/404');
26
        $router->route($request);
0 ignored issues
show
Deprecated Code introduced by
The method Nip\Router\Legacy\Router...asMatcherTrait::route() has been deprecated with message: Use matchRequest($request)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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->route($request);
0 ignored issues
show
Deprecated Code introduced by
The method Nip\Router\Legacy\Router...asMatcherTrait::route() has been deprecated with message: Use matchRequest($request)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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->route($request);
0 ignored issues
show
Deprecated Code introduced by
The method Nip\Router\Legacy\Router...asMatcherTrait::route() has been deprecated with message: Use matchRequest($request)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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($collection, "admin.standard", StandardRoute::class, "/admin",
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...
56
            '/:controller/:action', ['module' => 'admin']);
57
        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 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...
58
            '/:controller/:action', ['module' => 'api']);
59
60
        $request = Request::create('/api/pages/delete');
61
        self::assertEquals(
62
            ['module' => 'api', 'controller' => 'pages', 'action' => 'delete','_route' => 'api.standard'],
63
            $router->route($request)
0 ignored issues
show
Deprecated Code introduced by
The method Nip\Router\Legacy\Router...asMatcherTrait::route() has been deprecated with message: Use matchRequest($request)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
64
        );
65
66
        $request = Request::create('/admin/pages/delete');
67
        self::assertEquals(
68
            ['module' => 'admin', 'controller' => 'pages', 'action' => 'delete', '_route' => 'admin.standard'],
69
            $router->route($request)
0 ignored issues
show
Deprecated Code introduced by
The method Nip\Router\Legacy\Router...asMatcherTrait::route() has been deprecated with message: Use matchRequest($request)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
70
        );
71
    }
72
}
73