Test Failed
Branch improve-scrutinizer (33a448)
by Ayan
02:51
created

RouteTest::testUntil()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Phprest\Annotation;
3
4
use InvalidArgumentException;
5
use LogicException;
6
use PHPUnit\Framework\TestCase;
7
8
class RouteTest extends TestCase
9
{
10
    /**
11
     * @param string $method
12
     *
13
     * @dataProvider methodProvider
14
     */
15
    public function testSuccessValidation($method): void
16
    {
17
        $route = new Route([
18
            'method' => $method,
19
            'path' => '/root',
20
        ]);
21
22
        $this->assertEquals($method, $route->method);
23
        $this->assertEquals('/root', $route->path);
24
    }
25
26
    public function methodProvider(): array
27
    {
28
        return [
29
            ['GET'], ['POST'], ['PUT'], ['PATCH'], ['OPTIONS'], ['DELETE'], ['HEAD']
30
        ];
31
    }
32
33
    public function testSince(): void
34
    {
35
        $route = new Route([
36
            'method' => 'GET',
37
            'path' => '/root',
38
            'since' => '2.3'
39
        ]);
40
41
        $this->assertEquals('{version:(?:[2-9]\.[3-9])|(?:[3-9]\.\d)}', $route->version);
42
    }
43
44
    public function testUntil(): void
45
    {
46
        $route = new Route([
47
            'method' => 'GET',
48
            'path' => '/root',
49
            'until' => '3.2'
50
        ]);
51
52
        $this->assertEquals('{version:(?:[0-3]\.[0-2])|(?:[0-2]\.\d)}', $route->version);
53
    }
54
55
    public function testSinceAndUntilWithOneVersionNumDiff(): void
56
    {
57
        $route = new Route([
58
            'method' => 'GET',
59
            'path' => '/root',
60
            'since' => '2.3',
61
            'until' => '3.2'
62
        ]);
63
64
        $this->assertEquals('{version:(?:2\.[3-9])|(?:3\.[0-2])}', $route->version);
65
    }
66
67
    public function testSinceAndUntilWithMoreThanOneVersionNumDiff(): void
68
    {
69
        $route = new Route([
70
            'method' => 'GET',
71
            'path' => '/root',
72
            'since' => '2.3',
73
            'until' => '5.2'
74
        ]);
75
76
        $this->assertEquals('{version:(?:2\.[3-9])|(?:5\.[0-2])|(?:[3-4]\.\d)}', $route->version);
77
    }
78
79
    public function testSinceAndUntilWithEqualFirstNum(): void
80
    {
81
        $route = new Route([
82
            'method' => 'GET',
83
            'path' => '/root',
84
            'since' => '2.3',
85
            'until' => '2.7'
86
        ]);
87
88
        $this->assertEquals('{version:(?:2\.[3-7])}', $route->version);
89
    }
90
91
    /**
92
     * @expectedException LogicException
93
     */
94
    public function testInvalidSinceAndUntil(): void
95
    {
96
        new Route([
97
            'method' => 'GET',
98
            'path' => '/root',
99
            'since' => '2.3',
100
            'until' => '1.7'
101
        ]);
102
    }
103
104
    /**
105
     * @expectedException InvalidArgumentException
106
     */
107
    public function testMethodMissingOnValidation(): void
108
    {
109
        new Route(['path' => '/root']);
110
    }
111
112
    /**
113
     * @expectedException InvalidArgumentException
114
     */
115
    public function testMethodIsNotCorrectOnValidation(): void
116
    {
117
        new Route(['method' => 'IronMan']);
118
    }
119
120
    /**
121
     * @expectedException InvalidArgumentException
122
     */
123
    public function testPathMissingOnValidation(): void
124
    {
125
        new Route(['method' => 'POST']);
126
    }
127
128
    /**
129
     * @expectedException InvalidArgumentException
130
     */
131
    public function testInvalidSinceVersionOnValidation(): void
132
    {
133
        new Route(['method' => 'DELETE', 'path' => '/', 'since' => '1.0.1.0']);
134
    }
135
136
    /**
137
     * @expectedException InvalidArgumentException
138
     */
139
    public function testInvalidUntilVersionOnValidation(): void
140
    {
141
        new Route(['method' => 'DELETE', 'path' => '/', 'until' => '-5']);
142
    }
143
}
144