1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Bindings; |
8
|
|
|
use Gacela\Router\Entities\Request; |
9
|
|
|
use Gacela\Router\Router; |
10
|
|
|
use Gacela\Router\Routes; |
11
|
|
|
use GacelaTest\Feature\Router\Fake\Name; |
12
|
|
|
use GacelaTest\Feature\Router\Fake\NameInterface; |
13
|
|
|
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithDependencies; |
14
|
|
|
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithRequest; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
final class RouterBindingTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function test_inject_dependencies_in_controllers(): void |
20
|
|
|
{ |
21
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
22
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
23
|
|
|
|
24
|
|
|
$this->expectOutputString('default-Expected!'); |
25
|
|
|
|
26
|
|
|
Router::configure(static function (Bindings $binding, Routes $routes): void { |
27
|
|
|
$routes->get('expected/uri', FakeControllerWithDependencies::class); |
28
|
|
|
$binding->bind(NameInterface::class, new Name('Expected!')); |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function test_inject_controller_with_request_dependency(): void |
33
|
|
|
{ |
34
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected'; |
35
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
36
|
|
|
$_GET['name'] = 'Katarn'; |
37
|
|
|
|
38
|
|
|
$this->expectOutputString('Katarn'); |
39
|
|
|
|
40
|
|
|
Router::configure(static function (Routes $routes): void { |
41
|
|
|
$routes->get('expected', FakeControllerWithRequest::class); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|