Passed
Push — master ( 17d331...42ece1 )
by Alex
02:01
created

FetchActionsUnitTest::testFetchActionsWithMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 45
rs 9.536
c 1
b 0
f 0
1
<?php
2
namespace Mezon\Router\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Router\Router;
6
7
class FetchActionsUnitTest extends TestCase
8
{
9
10
    /**
11
     * Testing action #1.
12
     */
13
    public function actionA1(): string
14
    {
15
        return 'action #1';
16
    }
17
18
    /**
19
     * Testing action #2.
20
     */
21
    public function actionA2(): string
22
    {
23
        return 'action #2';
24
    }
25
26
    /**
27
     * Two word action name
28
     *
29
     * @return string
30
     */
31
    public function actionDoubleWord(): string
32
    {
33
        return 'action double word';
34
    }
35
36
    /**
37
     * Data provider for the test testClassAction
38
     *
39
     * @return array test data
40
     */
41
    public function classActionDataProvider(): array
42
    {
43
        $testData = [];
44
45
        foreach ([
46
            'GET',
47
            'POST'
48
        ] as $requestMethod) {
49
            $testData[] = [
50
                $requestMethod,
51
                '/a1/',
52
                'action #1'
53
            ];
54
55
            $testData[] = [
56
                $requestMethod,
57
                '/a2/',
58
                'action #2'
59
            ];
60
61
            $testData[] = [
62
                $requestMethod,
63
                '/double-word/',
64
                'action double word'
65
            ];
66
        }
67
68
        return $testData;
69
    }
70
71
    /**
72
     * Method tests class actions
73
     *
74
     * @param string $requestMethod
75
     *            request method
76
     * @param string $route
77
     *            requesting route
78
     * @param string $expectedResult
79
     *            expected result of route processing
80
     * @dataProvider classActionDataProvider
81
     */
82
    public function testClassAction(string $requestMethod, string $route, string $expectedResult): void
83
    {
84
        // setup
85
        RouterUnitTest::setRequestMethod($requestMethod);
86
        $router = new Router();
87
88
        // test body
89
        $router->fetchActions($this);
90
91
        // assertions
92
        $this->assertEquals($expectedResult, $router->callRoute($route));
93
    }
94
95
    /**
96
     * Testing map for the fetchActions method
97
     */
98
    public function testFetchActionsWithMap(): void
99
    {
100
        // setup
101
        $router = new Router();
102
103
        // test body
104
        $router->fetchActions($this, [
105
            'A1' => 'GET',
106
            'A2' => 'POST',
107
            'DoubleWord' => [
108
                'GET',
109
                'POST'
110
            ]
111
        ]);
112
113
        // assertions #1
114
        RouterUnitTest::setRequestMethod('GET');
115
        $this->assertEquals('action #1', $router->callRoute('a1'));
116
117
        // assertions #2
118
        RouterUnitTest::setRequestMethod('POST');
119
        $this->assertEquals('action #2', $router->callRoute('a2'));
120
121
        // assertions #3
122
        RouterUnitTest::setRequestMethod('GET');
123
        $this->assertEquals('action double word', $router->callRoute('double-word'));
124
125
        // assertions #4
126
        RouterUnitTest::setRequestMethod('POST');
127
        $this->assertEquals('action double word', $router->callRoute('double-word'));
128
129
        // assertions #5
130
        RouterUnitTest::setRequestMethod('POST');
131
        $this->expectException(\Exception::class);
132
        $router->callRoute('a1');
133
134
        // assertions #6
135
        RouterUnitTest::setRequestMethod('GET');
136
        $this->expectException(\Exception::class);
137
        $router->callRoute('a2');
138
139
        // assertions #7
140
        RouterUnitTest::setRequestMethod('DELETE');
141
        $this->expectException(\Exception::class);
142
        $router->callRoute('double-word');
143
    }
144
}
145