1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Philae\Tests; |
4
|
|
|
|
5
|
|
|
use Middlewares\HttpErrorException; |
6
|
|
|
use Philae\Application; |
7
|
|
|
use Philae\DefaultServiceProvider; |
8
|
|
|
use Philae\Exceptions\InvalidCallableServiceException; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
|
11
|
|
|
class GeneralAppClientTest extends BaseTestCase |
12
|
|
|
{ |
13
|
|
|
public function testOk() |
14
|
|
|
{ |
15
|
|
|
$this->app->getRouter()->get('/foo', function () { |
16
|
|
|
return 'bar'; |
17
|
|
|
}); |
18
|
|
|
$response = $this->call($this->createRequest('GET', '/foo')); |
19
|
|
|
|
20
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
21
|
|
|
$this->assertEquals('bar', $response->getBody()->__toString()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testRouteParams() |
25
|
|
|
{ |
26
|
|
|
$this->app->getRouter()->get('/foo/{id}/{action}[/{action2}]', function ($request, $customArg, $params) { |
27
|
|
|
$this->assertInternalType('array', $params); |
28
|
|
|
$this->assertArrayHasKey('id', $params); |
29
|
|
|
$this->assertArrayHasKey('action', $params); |
30
|
|
|
$this->assertArrayNotHasKey('action2', $params); |
31
|
|
|
$this->assertEquals('123', $params['id']); |
32
|
|
|
$this->assertEquals('show', $params['action']); |
33
|
|
|
$this->assertInstanceOf(ServerRequestInterface::class, $request); |
34
|
|
|
$this->assertInstanceOf(Application::class, $customArg); |
35
|
|
|
}); |
36
|
|
|
$response = $this->call($this->createRequest('GET', '/foo/123/show')); |
37
|
|
|
|
38
|
|
|
$this->assertEquals(204, $response->getStatusCode()); |
39
|
|
|
$this->assertEquals('', $response->getBody()->__toString()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testInvalidMiddlewareDispatcherError() |
43
|
|
|
{ |
44
|
|
|
$this->app->getRouter()->get('/foo', function () { |
45
|
|
|
return 'bar'; |
46
|
|
|
}); |
47
|
|
|
$this->expectException(InvalidCallableServiceException::class); |
48
|
|
|
$this->app->share(DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER, new \stdClass()); |
49
|
|
|
$this->call($this->createRequest('GET', '/foo')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testInvalidResponseEmitterError() |
53
|
|
|
{ |
54
|
|
|
$this->app->getRouter()->get('/foo', function () { |
55
|
|
|
return 'bar'; |
56
|
|
|
}); |
57
|
|
|
$this->expectException(InvalidCallableServiceException::class); |
58
|
|
|
$this->app->share(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER, new \stdClass()); |
59
|
|
|
$this->app->execute($this->createRequest('GET', '/foo')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testInvalidBody() |
63
|
|
|
{ |
64
|
|
|
$this->app->getRouter()->get('/foo', function () { |
65
|
|
|
return ['bar']; |
66
|
|
|
}); |
67
|
|
|
$this->expectException(\UnexpectedValueException::class); |
68
|
|
|
$this->call($this->createRequest('GET', '/foo')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testRedirection() |
72
|
|
|
{ |
73
|
|
|
$this->app->getRouter()->get('/foo', function () { |
74
|
|
|
return $this->app->getResponse() |
75
|
|
|
->withStatus(302) |
76
|
|
|
->withHeader('location', $this->app->getUriResolver()->uri('home') . ''); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$response = $this->call($this->createRequest('GET', '/foo')); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(302, $response->getStatusCode()); |
82
|
|
|
$this->assertTrue($response->hasHeader('Location')); |
83
|
|
|
$this->assertEquals('http://localhost/home', $response->getHeaderLine('Location')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testHttp404() |
87
|
|
|
{ |
88
|
|
|
$this->expectException(HttpErrorException::class); |
89
|
|
|
$this->expectExceptionCode(404); |
90
|
|
|
$this->call($this->createRequest('GET', '/foobar')); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|