Passed
Push — master ( 035a69...ef3bdc )
by Kirill
03:22
created

ControllerTargetTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 26
c 1
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testMatch() 0 38 1
A testDefaultAction() 0 6 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Router\Targets;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Router\Route;
16
use Spiral\Router\Target\Controller;
17
use Spiral\Tests\Router\Diactoros\UriFactory;
18
use Spiral\Tests\Router\Fixtures\TestController;
19
use Spiral\Router\UriHandler;
20
use Laminas\Diactoros\ServerRequest;
21
use Laminas\Diactoros\Uri;
22
23
class ControllerTargetTest extends TestCase
24
{
25
    public function testDefaultAction(): void
26
    {
27
        $route = new Route('/home[/<action>]', new Controller(TestController::class));
28
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
29
30
        $this->assertSame(['action' => null], $route->getDefaults());
31
    }
32
33
    public function testMatch(): void
34
    {
35
        $route = new Route(
36
            '/test[/<action>]',
37
            new Controller(TestController::class)
38
        );
39
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
40
41
        $this->assertNull($route->match(new ServerRequest()));
42
        $this->assertNotNull($route->match(new ServerRequest([], [], new Uri('/test/something'))));
43
        $this->assertNotNull($route->match(new ServerRequest([], [], new Uri('/test/tester'))));
44
45
        $this->assertNotNull(
46
            $match = $route->match(new ServerRequest([], [], new Uri('/test')))
47
        );
48
49
        $this->assertSame(['action' => null], $match->getMatches());
50
51
        $this->assertNotNull(
52
            $match = $route->match(new ServerRequest([], [], new Uri('/test/')))
53
        );
54
        $this->assertSame(['action' => null], $match->getMatches());
55
56
        $this->assertNotNull(
57
            $match = $route->match(new ServerRequest([], [], new Uri('/test/test')))
58
        );
59
        $this->assertSame(['action' => 'test'], $match->getMatches());
60
61
        $this->assertNotNull(
62
            $match = $route->match(new ServerRequest([], [], new Uri('/test/test/')))
63
        );
64
        $this->assertSame(['action' => 'test'], $match->getMatches());
65
66
        $this->assertNotNull(
67
            $match = $route->match(new ServerRequest([], [], new Uri('/test/other')))
68
        );
69
70
        $this->assertSame(['action' => 'other'], $match->getMatches());
71
    }
72
}
73