Completed
Push — master ( 1595c9...264e47 )
by Alejandro
01:56
created

SlimRouterTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 124
rs 10
c 2
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testAddRoute() 0 14 1
A testAddRouteWithOptions() 0 18 1
A testAddRouteWithAnyMethod() 0 15 1
A testDummyCallable() 0 4 1
A testGenerateUrl() 0 9 1
A testGenerateUrlWithInvalidName() 0 7 1
A testMatchInvalidRequest() 0 5 1
A testMatchValidRequest() 0 10 1
A injectRoutes() 0 7 1
1
<?php
2
namespace AcelayaTest\Expressive\Router;
3
4
use Acelaya\Expressive\Router\SlimRouter;
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Slim\Router;
9
use Zend\Diactoros\ServerRequest;
10
use Zend\Diactoros\ServerRequestFactory;
11
use Zend\Expressive\Router\Route;
12
13
class SlimRouterTest extends TestCase
14
{
15
    /**
16
     * @var SlimRouter
17
     */
18
    protected $router;
19
    /**
20
     * @var Router
21
     */
22
    protected $slimRouter;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    protected $middleware;
27
28
    public function setUp()
29
    {
30
        new SlimRouter();
31
        $this->slimRouter = new Router();
32
        $this->router = new SlimRouter($this->slimRouter);
33
        $this->middleware = $this->prophesize(MiddlewareInterface::class);
34
    }
35
36
    public function testAddRoute()
37
    {
38
        $middleware = $this->middleware->reveal();
39
        $this->router->addRoute(new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home'));
40
        $this->injectRoutes();
41
42
        $this->assertCount(1, $this->slimRouter->getNamedRoutes());
43
44
        /** @var \Slim\Route $route */
45
        $route = $this->slimRouter->getMatchedRoutes('GET', '/foo/baz')[0];
46
        $this->assertEquals('/foo(/:bar)', $route->getPattern());
47
        $this->assertEquals('home', $route->getName());
48
        $this->assertEquals($middleware, $route->getParams()['middleware']);
49
    }
50
51
    public function testAddRouteWithOptions()
52
    {
53
        $middleware = $this->middleware->reveal();
54
        $route = new Route('/foo/:bar', $middleware, ['GET', 'POST'], 'home');
55
        $route->setOptions([
56
            'conditions' => [
57
                'bar' => 'es|en'
58
            ],
59
            'defaults' => [
60
                'bar' => 'en'
61
            ]
62
        ]);
63
        $this->router->addRoute($route);
64
        $this->injectRoutes();
65
66
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/es'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...outes('GET', '/foo/es') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
        $this->assertCount(0, $this->slimRouter->getMatchedRoutes('GET', '/foo/baz', true));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...GET', '/foo/baz', true) is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    public function testAddRouteWithAnyMethod()
71
    {
72
        $middleware = $this->middleware->reveal();
73
        $route = new Route('/foo/bar', $middleware, Route::HTTP_METHOD_ANY, 'home');
74
        $this->router->addRoute($route);
75
        $this->injectRoutes();
76
77
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...utes('GET', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('POST', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...tes('POST', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('PUT', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...utes('PUT', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('DELETE', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...s('DELETE', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('PATCH', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...es('PATCH', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('OPTIONS', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...('OPTIONS', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
        $this->assertCount(1, $this->slimRouter->getMatchedRoutes('HEAD', '/foo/bar'));
0 ignored issues
show
Documentation introduced by
$this->slimRouter->getMa...tes('HEAD', '/foo/bar') is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
    }
85
86
    public function testDummyCallable()
87
    {
88
        $this->assertNull($this->router->dummyCallable());
89
    }
90
91
    public function testGenerateUrl()
92
    {
93
        $middleware = $this->middleware->reveal();
94
        $route = new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home');
95
        $this->router->addRoute($route);
96
97
        $this->assertEquals('/foo', $this->router->generateUri('home'));
98
        $this->assertEquals('/foo/baz', $this->router->generateUri('home', ['bar' => 'baz']));
99
    }
100
101
    /**
102
     * @expectedException \Zend\Expressive\Router\Exception\RuntimeException
103
     */
104
    public function testGenerateUrlWithInvalidName()
105
    {
106
        $middleware = $this->middleware->reveal();
107
        $route = new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home');
108
        $this->router->addRoute($route);
109
        $this->router->generateUri('invalidName');
110
    }
111
112
    public function testMatchInvalidRequest()
113
    {
114
        $result = $this->router->match(ServerRequestFactory::fromGlobals());
115
        $this->assertTrue($result->isFailure());
116
    }
117
118
    public function testMatchValidRequest()
119
    {
120
        $middleware = $this->middleware->reveal();
121
        $this->router->addRoute(new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home'));
122
        $this->injectRoutes();
123
124
        $this->assertCount(1, $this->slimRouter->getNamedRoutes());
125
        $result = $this->router->match(new ServerRequest([], [], '/foo/bar', 'POST'));
126
        $this->assertTrue($result->isSuccess());
127
    }
128
129
    private function injectRoutes()
130
    {
131
        $ref = new \ReflectionObject($this->router);
132
        $method = $ref->getMethod('injectRoutes');
133
        $method->setAccessible(true);
134
        $method->invoke($this->router);
135
    }
136
}
137