-
Notifications
You must be signed in to change notification settings - Fork 38
/
RoutingTest.php
141 lines (122 loc) · 4.36 KB
/
RoutingTest.php
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
<?php declare(strict_types=1);
namespace DI\Bridge\Slim\Test;
use DI\Bridge\Slim\Bridge;
use DI\Bridge\Slim\Test\Fixture\UserController;
use DI\Bridge\Slim\Test\Mock\RequestFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class RoutingTest extends TestCase
{
/**
* @test
*/
public function injects_request_and_response()
{
$app = Bridge::create();
// Response and request and inversed to check that they are correctly injected by name
$app->get('/', function (ResponseInterface $response, ServerRequestInterface $request) {
$response->getBody()->write('Hello ' . $request->getQueryParams()['foo']);
return $response;
});
$response = $app->handle(RequestFactory::create('/', 'foo=matt'));
$this->assertEquals('Hello matt', (string) $response->getBody());
}
/**
* @test
*/
public function injects_route_placeholder()
{
$app = Bridge::create();
$app->get('/{name}', function ($name, $response) {
$response->getBody()->write('Hello ' . $name);
return $response;
});
$response = $app->handle(RequestFactory::create('/matt'));
$this->assertEquals('Hello matt', (string) $response->getBody());
}
/**
* @test
*/
public function injects_route_placeholder_from_request()
{
$app = Bridge::create();
$app->get('/{name}', function ($request, $response) {
$response->getBody()->write('Hello ' . $request->getAttribute('name'));
return $response;
});
$response = $app->handle(RequestFactory::create('/matt'));
$this->assertEquals('Hello matt', (string) $response->getBody());
}
/**
* @test
*/
public function injects_optional_route_placeholder()
{
$app = Bridge::create();
$app->get('/[{name}]', function ($response, $name = null) {
$response->getBody()->write('Hello ' . $name);
return $response;
});
$response = $app->handle(RequestFactory::create('/matt'));
$this->assertEquals('Hello matt', (string) $response->getBody());
}
/**
* @test
*/
public function injects_default_value_in_optional_route_placeholder()
{
$app = Bridge::create();
$app->get('/[{name}]', function ($response, $name = 'john doe') {
$response->getBody()->write('Hello ' . $name);
return $response;
});
$response = $app->handle(RequestFactory::create());
$this->assertEquals('Hello john doe', (string) $response->getBody());
}
/**
* @test
*/
public function injects_request_attribute()
{
$app = Bridge::create();
// Let's add a middleware that adds a request attribute
$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $next) {
return $next->handle($request->withAttribute('name', 'Bob'));
});
$app->get('/', function ($name, $response) {
$response->getBody()->write('Hello ' . $name);
return $response;
});
$response = $app->handle(RequestFactory::create());
$this->assertEquals('Hello Bob', (string) $response->getBody());
}
/**
* @test
*/
public function injects_route_placeholder_over_request_attribute()
{
$app = Bridge::create();
$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $next) {
return $next->handle($request->withAttribute('name', 'Bob'));
});
$app->get('/{name}', function ($name, $response) {
$response->getBody()->write('Hello ' . $name);
return $response;
});
$response = $app->handle(RequestFactory::create('/matt'));
// The route placeholder has priority over the request attribute
$this->assertEquals('Hello matt', (string) $response->getBody());
}
/**
* @test
*/
public function resolve_controller_from_container()
{
$app = Bridge::create();
$app->get('/', [UserController::class, 'dashboard']);
$response = $app->handle(RequestFactory::create());
$this->assertEquals('Hello world!', (string) $response->getBody());
}
}