-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathFlightTest.php
More file actions
128 lines (102 loc) · 2.99 KB
/
FlightTest.php
File metadata and controls
128 lines (102 loc) · 2.99 KB
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
<?php
declare(strict_types=1);
namespace Tests\PHP8;
use DateTimeImmutable;
use ExampleClass;
use Flight;
use flight\Container;
use flight\Engine;
use flight\net\Route;
use PHPUnit\Framework\TestCase;
use stdClass;
final class FlightTest extends TestCase
{
private Engine $engine;
protected function setUp(): void
{
$this->engine = new Engine();
Flight::init();
Flight::setEngine($this->engine);
}
//////////////////
// CORE METHODS //
//////////////////
public function testPath(): void
{
Flight::path(dir: __DIR__); // phpcs:ignore
$exampleObject = new ExampleClass();
self::assertInstanceOf(ExampleClass::class, $exampleObject);
}
public function testStopWithCode(): void
{
Flight::stop(code: 500);
self::expectOutputString('');
self::assertSame(500, Flight::response()->status());
}
public function testHalt(): void
{
Flight::halt(actuallyExit: false, code: 500, message: 'Test');
self::expectOutputString('Test');
self::assertSame(500, Flight::response()->status());
}
public function testRegister(): void
{
Flight::register(
class: stdClass::class,
name: 'customClass',
callback: static function (stdClass $object): void {
$object->property = 'value';
},
params: []
);
$object = Flight::customClass();
self::assertInstanceOf(stdClass::class, $object);
self::assertObjectHasProperty('property', $object);
self::assertSame('value', $object->property);
Flight::unregister(methodName: 'customClass');
}
public function testRegisterContainer(): void
{
$dateTime = new DateTimeImmutable();
$controller = new class ($dateTime) {
public function __construct(private DateTimeImmutable $dateTime)
{
//
}
public function test(): void
{
echo $this->dateTime->format('Y-m-d');
}
};
Flight::registerContainerHandler(
containerHandler: new Container()
);
Flight::request()->url = '/test';
Flight::route(
pass_route: true,
alias: 'testRoute',
callback: [$controller::class, 'test'],
pattern: '/test'
);
self::expectOutputString($dateTime->format('Y-m-d'));
Flight::start();
}
/////////////////////
// ROUTING METHODS //
/////////////////////
public function testStaticRoute(): void
{
Flight::request()->url = '/test';
$route = Flight::route(
pass_route: true,
alias: 'testRoute',
callback: function () {
echo 'test';
},
pattern: '/test'
);
self::assertInstanceOf(Route::class, $route);
self::expectOutputString('test');
Flight::start();
}
}