Issues (101)

spec/Suite/Host.spec.php (1 issue)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Lead\Router\Spec\Suite;
5
6
use Lead\Router\Exception\RouterException;
7
use Lead\Router\Host;
8
9
describe("Host", function() {
10
11
    describe("->scheme()", function() {
12
13
        it("gets/sets the scheme", function() {
14
15
            $host = new Host();
16
            expect($host->getScheme())->toBe('*');
17
            expect($host->setScheme('https'))->toBe($host);
18
            expect($host->getScheme())->toBe('https');
19
20
        });
21
22
    });
23
24
    describe("->pattern()", function() {
25
26
        it("gets/sets the pattern", function() {
27
28
            $host = new Host();
29
            expect($host->getPattern())->toBe('*');
30
            expect($host->setPattern('foo.{domain}.bar'))->toBe($host);
31
            expect($host->getPattern())->toBe('foo.{domain}.bar');
32
            expect($host->getRegex())->toBe('foo\\.([^.]+)\\.bar');
33
            expect($host->getVariables())->toBe([
34
                'domain'    => false
35
            ]);
36
37
        });
38
39
        it("updates the regex", function() {
40
41
            $host = new Host();
42
            expect($host->setPattern('foo.{domain}.bar'))->toBe($host);
43
            expect($host->getRegex())->toBe('foo\\.([^.]+)\\.bar');
44
45
            expect($host->setPattern('foo.{domain}.baz'))->toBe($host);
46
            expect($host->getRegex())->toBe('foo\\.([^.]+)\\.baz');
47
        });
48
49
        it("updates the variables", function() {
50
51
            $host = new Host();
52
            expect($host->setPattern('foo.{domain}.bar'))->toBe($host);
53
            expect($host->getVariables())->toBe([
54
                'domain'    => false
55
            ]);
56
57
            expect($host->setPattern('foo.{baz}.bar'))->toBe($host);
58
            expect($host->getVariables())->toBe([
59
                'baz'    => false
60
            ]);
61
        });
62
63
    });
64
65
    describe("->match()", function() {
66
67
        it("returns `true` when host & scheme matches", function() {
68
69
            $host = new Host(['pattern' => 'foo.{domain}.bar', 'scheme' => 'https']);
70
71
            expect($host->match(['scheme' => 'https', 'host' => 'foo.baz.bar'], $variables))->toBe(true);
72
            expect($variables)->toBe(['domain' => 'baz']);
73
74
            expect($host->match(['scheme' => 'https', 'host' => 'biz.bar.baz'], $variables))->toBe(false);
75
76
            expect($host->match(['scheme' => 'http', 'host' => 'foo.baz.bar'], $variables))->toBe(false);
77
78
        });
79
80
        it("returns `true` when host matches with a wildcard as host's scheme", function() {
81
82
            $host = new Host(['pattern' => 'foo.{domain}.bar', 'scheme' => 'https']);
83
84
            expect($host->match(['scheme' => '*', 'host' => 'foo.baz.bar'], $variables))->toBe(true);
85
            expect($variables)->toBe(['domain' => 'baz']);
86
87
            expect($host->match(['scheme' => '*', 'host' => 'biz.baz.bar'], $variables))->toBe(false);
88
89
        });
90
91
        it("returns `true` when host matches with a wildcard as request's scheme", function() {
92
93
            $host = new Host(['pattern' => 'foo.{domain}.bar', 'scheme' => '*']);
94
95
            expect($host->match(['scheme' => 'https', 'host' => 'foo.baz.bar'], $variables))->toBe(true);
96
            expect($variables)->toBe(['domain' => 'baz']);
97
98
            expect($host->match(['scheme' => 'https', 'host' => 'biz.baz.bar'], $variables))->toBe(false);
99
100
        });
101
102
        it("returns `true` when scheme matches with a wildcard as host's pattern", function() {
103
104
            $host = new Host(['pattern' => '*', 'scheme' => 'http']);
105
106
            expect($host->match(['scheme' => 'http', 'host' => 'foo.baz.bar'], $variables))->toBe(true);
107
            expect($variables)->toBe([]);
108
109
            expect($host->match(['scheme' => 'https', 'host' => 'foo.baz.bar'], $variables))->toBe(false);
110
111
        });
112
113
        it("returns `true` when scheme matches with a wildcard as request's pattern", function() {
114
115
            $host = new Host(['pattern' => 'foo.{domain}.bar', 'scheme' => 'http']);
116
117
            expect($host->match(['scheme' => 'http', 'host' => '*'], $variables))->toBe(true);
118
            expect($variables)->toBe(['domain' => null]);
119
120
            expect($host->match(['scheme' => 'https', 'host' => '*'], $variables))->toBe(false);
121
122
        });
123
124
    });
125
126
127
    describe("->link()", function() {
128
129
        it("builds an host link", function() {
130
131
            $host = new Host(['pattern' => 'www.{domain}.com', 'scheme' => 'https']);
132
133
            $link = $host->link(['domain' => 'example']);
134
            expect($link)->toBe('https://www.example.com');
135
136
        });
137
138
        it("builds a scheme less host link", function() {
139
140
            $host = new Host(['pattern' => 'www.{domain}.com']);
141
142
            $link = $host->link(['domain' => 'example']);
143
            expect($link)->toBe('//www.example.com');
144
145
        });
146
147
        it("overrides scheme when passed as parameter", function() {
148
149
            $host = new Host(['pattern' => 'www.{domain}.com']);
150
151
            $link = $host->link(['domain' => 'example'], [
152
                'scheme' => 'http'
153
            ]);
154
            expect($link)->toBe('http://www.example.com');
155
156
        });
157
158
        it("processes complex pattern", function() {
159
160
            $host = new Host(['pattern' => '[{subdomains}.]*{domain}.com', 'scheme' => 'https']);
161
162
            expect($host->link(['domain' => 'example']))->toBe('https://example.com');
163
            expect($host->link([
164
                'subdomains' => ['a'],
165
                'domain' => 'example'
166
            ]))->toBe('https://a.example.com');
167
168
            expect($host->link([
169
                'subdomains' => ['a', 'b', 'c'],
170
                'domain' => 'example'
171
            ]))->toBe('https://a.b.c.example.com');
172
173
        });
174
175
        it("throws an exception when variables are missing", function() {
176
177
            $closure = function() {
178
                $host = new Host(['pattern' => 'www.{domain}.com']);
179
180
                $link = $host->link();
0 ignored issues
show
The assignment to $link is dead and can be removed.
Loading history...
181
            };
182
            expect($closure)->toThrow(new RouterException("Missing parameters `'domain'` for host: `'www.{domain}.com'`."));
183
184
        });
185
186
    });
187
188
});
189