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

ActionTargetTest::testMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 40
rs 9.552
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\Autofill;
16
use Spiral\Router\Exception\ConstrainException;
17
use Spiral\Router\Exception\InvalidArgumentException;
18
use Spiral\Router\Route;
19
use Spiral\Router\Target\Action;
20
use Spiral\Tests\Router\Diactoros\UriFactory;
21
use Spiral\Tests\Router\Fixtures\TestController;
22
use Spiral\Router\UriHandler;
23
use Laminas\Diactoros\ServerRequest;
24
use Laminas\Diactoros\Uri;
25
26
class ActionTargetTest extends TestCase
27
{
28
    public function testDefaultAction(): void
29
    {
30
        $route = new Route('/home', new Action(TestController::class, 'test'));
31
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
32
33
        $this->assertSame(['action' => 'test'], $route->getDefaults());
34
    }
35
36
    public function testConstrains(): void
37
    {
38
        $route = new Route('/home', new Action(TestController::class, 'test'));
39
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
40
41
        $this->assertEquals(['action' => new Autofill('test')], $route->getUriHandler()->getConstrains());
42
43
        $route = new Route('/<action>', new Action(TestController::class, ['test', 'other']));
44
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
45
46
        $this->assertSame(['action' => ['test', 'other']], $route->getUriHandler()->getConstrains());
47
    }
48
49
    public function testConstrainedAction(): void
50
    {
51
        $this->expectException(ConstrainException::class);
52
53
        $route = new Route('/home', new Action(TestController::class, ['test', 'other']));
54
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
55
56
        $route->match(new ServerRequest());
57
    }
58
59
    public function testMatch(): void
60
    {
61
        $route = new Route(
62
            '/test[/<action>]',
63
            new Action(TestController::class, ['test', 'other'])
64
        );
65
        $route = $route->withUriHandler(new UriHandler(new UriFactory()));
66
67
        $route = $route->withDefaults(['action' => 'test']);
68
69
        $this->assertNull($route->match(new ServerRequest()));
70
        $this->assertNull($route->match(new ServerRequest([], [], new Uri('/test/something'))));
71
        $this->assertNull($route->match(new ServerRequest([], [], new Uri('/test/tester'))));
72
73
        $this->assertNotNull(
74
            $match = $route->match(new ServerRequest([], [], new Uri('/test')))
75
        );
76
77
        $this->assertSame(['action' => 'test'], $match->getMatches());
78
79
        $this->assertNotNull(
80
            $match = $route->match(new ServerRequest([], [], new Uri('/test/')))
81
        );
82
        $this->assertSame(['action' => 'test'], $match->getMatches());
83
84
        $this->assertNotNull(
85
            $match = $route->match(new ServerRequest([], [], new Uri('/test/test')))
86
        );
87
        $this->assertSame(['action' => 'test'], $match->getMatches());
88
89
        $this->assertNotNull(
90
            $match = $route->match(new ServerRequest([], [], new Uri('/test/test/')))
91
        );
92
        $this->assertSame(['action' => 'test'], $match->getMatches());
93
94
        $this->assertNotNull(
95
            $match = $route->match(new ServerRequest([], [], new Uri('/test/other')))
96
        );
97
98
        $this->assertSame(['action' => 'other'], $match->getMatches());
99
    }
100
101
    public function testActionException(): void
102
    {
103
        $this->expectException(InvalidArgumentException::class);
104
105
        new Action(TestController::class, $this);
0 ignored issues
show
Bug introduced by
$this of type Spiral\Tests\Router\Targets\ActionTargetTest is incompatible with the type array|string expected by parameter $action of Spiral\Router\Target\Action::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        new Action(TestController::class, /** @scrutinizer ignore-type */ $this);
Loading history...
106
    }
107
}
108