Completed
Push — master ( d41cf1...36fce9 )
by Javi
23:31
created

GeneralAppClientTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 78
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testOk() 0 10 1
A testRouteParams() 0 13 1
A testInvalidMiddlewareDispatcherError() 0 9 1
A testInvalidResponseEmitterError() 0 9 1
A testInvalidBody() 0 8 1
A testRedirection() 0 14 1
A testHttp404() 0 6 1
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}', function ($id, $action, $request, $customArg) {
27
            $this->assertEquals('123', $id);
28
            $this->assertEquals('show', $action);
29
            $this->assertInstanceOf(ServerRequestInterface::class, $request);
30
            $this->assertInstanceOf(Application::class, $customArg);
31
        });
32
        $response = $this->call($this->createRequest('GET', '/foo/123/show'));
33
34
        $this->assertEquals(204, $response->getStatusCode());
35
        $this->assertEquals('', $response->getBody()->__toString());
36
    }
37
38
    public function testInvalidMiddlewareDispatcherError()
39
    {
40
        $this->app->getRouter()->get('/foo', function () {
41
            return 'bar';
42
        });
43
        $this->expectException(InvalidCallableServiceException::class);
44
        $this->app->share(DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER, new \stdClass());
45
        $this->call($this->createRequest('GET', '/foo'));
46
    }
47
48
    public function testInvalidResponseEmitterError()
49
    {
50
        $this->app->getRouter()->get('/foo', function () {
51
            return 'bar';
52
        });
53
        $this->expectException(InvalidCallableServiceException::class);
54
        $this->app->share(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER, new \stdClass());
55
        $this->app->execute($this->createRequest('GET', '/foo'));
56
    }
57
58
    public function testInvalidBody()
59
    {
60
        $this->app->getRouter()->get('/foo', function () {
61
            return ['bar'];
62
        });
63
        $this->expectException(\UnexpectedValueException::class);
64
        $this->call($this->createRequest('GET', '/foo'));
65
    }
66
67
    public function testRedirection()
68
    {
69
        $this->app->getRouter()->get('/foo', function () {
70
            return $this->app->getResponse()
71
                             ->withStatus(302)
72
                             ->withHeader('location', $this->app->getUriResolver()->uri('home') . '');
73
        });
74
75
        $response = $this->call($this->createRequest('GET', '/foo'));
76
77
        $this->assertEquals(302, $response->getStatusCode());
78
        $this->assertTrue($response->hasHeader('Location'));
79
        $this->assertEquals('http://localhost/home', $response->getHeaderLine('Location'));
80
    }
81
82
    public function testHttp404()
83
    {
84
        $this->expectException(HttpErrorException::class);
85
        $this->expectExceptionCode(404);
86
        $this->call($this->createRequest('GET', '/foobar'));
87
    }
88
}
89