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
|
<?php
declare(strict_types=1);
use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Enums\PathSegmentType;
use Lightscale\Router\PathSegment;
use Lightscale\Router\Route;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\RequestInterface;
it('initializes')
->expect(fn () => new Route(
HttpMethod::Get,
fn () => null,
new PathSegment(type: PathSegmentType::Root),
))
->toBeInstanceOf(Route::class);
it('gets segment')
->expect(fn () => (new Route(
HttpMethod::Get,
fn () => null,
new PathSegment(type: PathSegmentType::Root),
))->getSegment())
->toBeInstanceOf(PathSegment::class);
it('throws getting segment without being set', function () {
$r = new Route(HttpMethod::Get, fn () => null);
$r->getSegment();
})->throws(Error::class);
it('can set segment')
->expect(function () {
$r = new Route(HttpMethod::Get, fn () => null);
$r->setSegment(new PathSegment('test'));
return $r->getSegment();
})
->toBeInstanceOf(PathSegment::class)
->getValue()->toBe('test');
it('gets method')
->expect(fn () => (new Route(
HttpMethod::Get,
fn () => null,
))->getMethod())
->toBe(HttpMethod::Get);
it('can call handler')
->expect(fn () => (new Route(
HttpMethod::Get,
fn (RequestInterface $req) => $req->getUri()->getPath()
))(
(new Psr17Factory())->createRequest(HttpMethod::Get->value, '/testing')
)
)
->toBe('/testing');
|