/
SplashServiceProviderTest.php
63 lines (54 loc) · 2.06 KB
/
SplashServiceProviderTest.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
<?php
namespace TheCodingMachine\Splash\DI;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TheCodingMachine\Splash\DI\Fixtures\TestController2;
use TheCodingMachine\Splash\Routers\SplashRouter;
use Simplex\Container;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\ServerRequest;
class SplashServiceProviderTest extends TestCase
{
protected function setUp()
{
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
}
public function testDefaultRouter()
{
$simplex = new Container([new SplashServiceProvider()]);
$simplex[Reader::class] = function () {
return new AnnotationReader();
};
$simplex[TestController2::class] = function () {
return new TestController2();
};
$simplex['thecodingmachine.splash.controllers'] = [
TestController2::class,
];
$defaultRouter = $simplex->get(SplashRouter::class);
$this->assertInstanceOf(SplashRouter::class, $defaultRouter);
// Now, let's test the redirect
$request = new ServerRequest([], [], '/foo/var/bar/', 'GET', 'php://input',
[],
[],
['id' => 42]
);
$handler = new class implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new HtmlResponse("", 404);
}
};
$response = $defaultRouter->process($request, $handler);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals('/foo/var/bar', $response->getHeader('Location')[0]);
}
}