summaryrefslogtreecommitdiff
path: root/tests/Unit/PathSegmentTest.php
blob: ffad95f125a7a66efbc070a49b842fa694c2979a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php

declare(strict_types=1);

use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Enums\PathSegmentType;
use Lightscale\Router\Exceptions\MissingParameterException;
use Lightscale\Router\PathSegment;
use Lightscale\Router\Route;

it('initializes')
    ->expect(fn () => new PathSegment('test'))
    ->toBeInstanceOf(PathSegment::class);

it('can get the content')
    ->expect(fn () => (new PathSegment('test'))->getContent())
    ->toBe('test');

it('can be root')
    ->expect((new PathSegment(type: PathSegmentType::Root))->getType())
    ->toBe(PathSegmentType::Root);

it('nulls value when type root')
    ->expect(fn () => (new PathSegment('testing', PathSegmentType::Root))->getValue())
    ->toBeNull();

it('gets value for type :dataset', function (PathSegmentType $type, ?string $res) {
    $seg = new PathSegment($v = 'testing', $type);
    expect($seg->getValue())->toBe($res);
})->with([
    ($t = PathSegmentType::Raw)->name => [$t, 'testing'],
    ($t = PathSegmentType::Root)->name => [$t, null],
    ($t = PathSegmentType::Parameter)->name => [$t, 'testing'],
]);

it('gets key for type :dataset', function (PathSegmentType $type, ?string $res) {
    $seg = new PathSegment($v = 'testing', $type);
    expect($seg->getKey())->toBe($res);
})->with([
    ($t = PathSegmentType::Raw)->name => [$t, 'testing'],
    ($t = PathSegmentType::Root)->name => [$t, '<root>'],
    ($t = PathSegmentType::Parameter)->name => [$t, '<parameter>'],
]);

it('gets raw keys lowercase', function() {
    $seg = new PathSegment('TesTiNg');
    expect($seg->getType())->toBe(PathSegmentType::Raw);
    expect($seg->getKey())->toBe('testing');
});

it('gets type :dataset', function (PathSegmentType $type) {
    $seg = new PathSegment($v = 'testing', $type);
    expect($seg->getType())->toBe($type);
})->with(function (): Generator {
    foreach (PathSegmentType::cases() as $t) {
        yield $t->name => $t;
    }
});

it('defaults type to Raw')
    ->expect(fn () => (new PathSegment('testing'))->getType())
    ->toBe(PathSegmentType::Raw);

$testChain = function () {
    $root = new PathSegment(type: PathSegmentType::Root);
    $seg1 = new PathSegment('test1');
    $seg2 = new PathSegment('test2');
    $seg3 = new PathSegment('test3');

    $seg3->setParent($seg2);
    $seg2->setParent($seg1);
    $seg1->setParent($root);

    return $seg3;
};

it('can have a parent')
    ->expect(fn () => $testChain()->getParent())
    ->toBeInstanceOf(PathSegment::class)
    ->getContent()->toBe('test2');

it('has null parent by default')
    ->expect(fn () => (new PathSegment('test'))->getParent())
    ->toBeNull();

it('can get all ancestors')
    ->expect(fn () => $testChain()->getAncestors())
    ->toBeArray()
    ->toContainOnlyInstancesOf(PathSegment::class)
    ->toHaveCount(3);

it('order all ancestors root first')
    ->expect(fn () => $testChain()->getAncestors())
    ->{0}->getContent()->toBe('')
    ->{0}->getType()->toBe(PathSegmentType::Root)
    ->{1}->getContent()->toBe('test1')
    ->{2}->getContent()->toBe('test2');

it('can get all ancestors and self')
    ->expect(fn () => $testChain()->getAncestorsAndSelf())
    ->toBeArray()
    ->toContainOnlyInstancesOf(PathSegment::class)
    ->toHaveCount(4);

it('order all ancestors and self root first')
    ->expect(fn () => $testChain()->getAncestorsAndSelf())
    ->{0}->getContent()->toBe('')
    ->{0}->getType()->toBe(PathSegmentType::Root)
    ->{1}->getContent()->toBe('test1')
    ->{2}->getContent()->toBe('test2')
    ->{3}->getContent()->toBe('test3');

$makeWithChildren = function () {
    $seg = new PathSegment('test1');
    $seg->addChild(new PathSegment('test2'));
    $seg->addChild(new PathSegment('test3'));

    return $seg;
};

it('can have children')
    ->expect(fn () => $makeWithChildren()->getChildren())
    ->toBeArray()
    ->toContainOnlyInstancesOf(PathSegment::class)
    ->{'test2'}->getContent()->toBe('test2')
    ->{'test3'}->getContent()->toBe('test3');

it('when adding child sets its parent')
    ->expect(fn () => $makeWithChildren()->getChildren())
    ->{'test2'}->getParent()->toBeInstanceOf(PathSegment::class);

it('gets the children count')
    ->expect(fn () => $makeWithChildren()->getChildrenCount())
    ->toBe(2);

it('creates new child and returns')
    ->expect(fn () => (new PathSegment('test1'))->child('test2'))
    ->toBeInstanceOf(PathSegment::class)
    ->getContent()->toBe('test2');

it('creates new child added to children')
    ->expect(function () {
        $seg = new PathSegment('test1');
        $seg->child('test2');

        return $seg->getChildren();
    })
    ->toHaveCount(1);

it('does not add same child twice', function () {
    $seg = new PathSegment('test1');
    $seg2 = $seg->child('test2');
    $seg3 = $seg->child('test2');

    expect($seg2)->toBe($seg3);
    expect($seg->getChildrenCount())->toBe(1);
});

it('can get a raw segment child', function () {
    $seg = new PathSegment('test1');
    $c1 = $seg->child('test2');
    $c2 = $seg->child('test3');

    expect($seg->findChild('test2'))->toBe($c1);
    expect($seg->findChild('test3'))->toBe($c2);
});

it('can get a parameter segment child', function () {
    $seg = new PathSegment('test1');
    $c1 = $seg->child('test2');
    $c2 = $seg->child('test3', PathSegmentType::Parameter);

    expect($seg->findChild('test2'))->toBe($c1);
    expect($seg->findChild('test4'))->toBe($c2);
});

it('can build full path')
    ->expect(fn () => $testChain()->getPath())
    ->toBe('/test1/test2/test3');

it('can build path with parameters', function() {
    $seg = new PathSegment(type: PathSegmentType::Root);
    $child = $seg->child('test1')
        ->child('test2', PathSegmentType::Parameter)
        ->child('test3', PathSegmentType::Parameter)
        ->child('test4');

    expect($child->getPath([
        'test2' => 'a',
        'test3' => 'b',
    ]))->toBe('/test1/a/b/test4');
});

it('throws when building path with missing parameters', function() {
    $seg = new PathSegment(type: PathSegmentType::Root);
    $child = $seg->child('test1')
        ->child('test2', PathSegmentType::Parameter)
        ->child('test3', PathSegmentType::Parameter)
        ->child('test4');

    expect($child->getPath([
        'test2' => 'a',
    ]))->toBe('/test1/a/b/test4');
})->throws(MissingParameterException::class);

it('sets route segment when added', function () {
    $seg = new PathSegment('test1');
    $seg->addRoute($r = new Route(
        HttpMethod::Get,
        fn () => null
    ));

    expect($r->getSegment())->toBe($seg);
});

it('can have routes', function () {
    $seg = new PathSegment('test');
    $seg->addRoute($r1 = new Route(
        HttpMethod::Get,
        fn () => null,
    ));
    $seg->addRoute($r2 = new Route(
        HttpMethod::Post,
        fn () => null,
    ));

    expect($seg->getRoutes())
        ->toHaveCount(2)
        ->{HttpMethod::Get->value}->toBe($r1)
        ->{HttpMethod::Post->value}->toBe($r2);
});

it('overwrites existing routes', function () {
    $seg = new PathSegment('test');
    $seg->addRoute($r1 = new Route(
        HttpMethod::Get,
        fn () => null,
    ));
    $seg->addRoute($r2 = new Route(
        HttpMethod::Get,
        fn () => null,
    ));

    expect($seg->getRoutes())
        ->toHaveCount(1)
        ->{HttpMethod::Get->value}->toBe($r2);
});

it('can get route by method', function () {
    $seg = new PathSegment('test');
    $seg->addRoute($r = new Route(
        HttpMethod::Get,
        fn () => null,
    ));

    expect($seg->getRoute(HttpMethod::Get))->toBe($r);
});

it('gets null with route method that doesn\'t exist')
    ->expect(fn () => (new PathSegment('test'))->getRoute(HttpMethod::Get))
    ->toBeNull();