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

GroupTargetTest::testConstrainedController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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\Exception\ConstrainException;
16
use Spiral\Router\Route;
17
use Spiral\Router\Target\Group;
18
use Spiral\Tests\Router\Diactoros\UriFactory;
19
use Spiral\Tests\Router\Fixtures\TestController;
20
use Spiral\Router\UriHandler;
21
use Laminas\Diactoros\ServerRequest;
22
use Laminas\Diactoros\Uri;
23
24
class GroupTargetTest extends TestCase
25
{
26
    public function testDefaultAction(): void
27
    {
28
        $route = new Route('/<controller>/<action>', new Group(['test' => TestController::class]));
29
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
30
31
        $this->assertSame(['controller' => null, 'action' => null], $route->getDefaults());
32
    }
33
34
    public function testConstrainedController(): void
35
    {
36
        $this->expectException(ConstrainException::class);
37
38
        $route = new Route('/<action>', new Group(['test' => TestController::class]));
39
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
40
41
        $route->match(new ServerRequest());
42
    }
43
44
    public function testConstrainedAction(): void
45
    {
46
        $this->expectException(ConstrainException::class);
47
48
        $route = new Route('/<controller>', new Group(['test' => TestController::class]));
49
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
50
        $route->match(new ServerRequest());
51
    }
52
53
    public function testMatch(): void
54
    {
55
        $route = new Route(
56
            '/<controller>[/<action>]',
57
            new Group(['test' => TestController::class])
58
        );
59
60
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
61
62
        $route = $route->withDefaults(['controller' => 'test']);
63
64
        $this->assertNull($route->match(new ServerRequest()));
65
66
        $this->assertNotNull(
67
            $match = $route->match(new ServerRequest([], [], new Uri('/test')))
68
        );
69
70
        $this->assertSame(['controller' => 'test', 'action' => null], $match->getMatches());
71
72
        $this->assertNotNull(
73
            $match = $route->match(new ServerRequest([], [], new Uri('/test/action/')))
74
        );
75
76
        $this->assertSame(['controller' => 'test', 'action' => 'action'], $match->getMatches());
77
78
        $this->assertNull(
79
            $match = $route->match(new ServerRequest([], [], new Uri('/other/action/')))
80
        );
81
82
        $this->assertNull(
83
            $match = $route->match(new ServerRequest([], [], new Uri('/other')))
84
        );
85
    }
86
}
87