Completed
Push — master ( 17598f...abee1f )
by Kirill
13s queued 11s
created

SapiTest::testDispatch()

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
nc 1
nop 0
dl 0
loc 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Framework\Http;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Spiral\Boot\DirectoriesInterface;
16
use Spiral\Files\FilesInterface;
17
use Spiral\Http\EmitterInterface;
18
use Spiral\Http\SapiDispatcher;
19
use Spiral\Tests\Framework\ConsoleTest;
20
21
class SapiTest extends ConsoleTest
22
{
23
    /** @var EmitterInterface */
24
    private $bufferEmitter;
25
26
    public function setUp(): void
27
    {
28
        $this->bufferEmitter = new class() implements EmitterInterface {
29
            public $response;
30
31
            public function emit(ResponseInterface $response): bool
32
            {
33
                $this->response = $response;
34
                return true;
35
            }
36
        };
37
        parent::setUp();
38
    }
39
40
    public function testCantServe(): void
41
    {
42
        $this->assertFalse($this->app->get(SapiDispatcher::class)->canServe());
43
    }
44
45
    public function testDispatch(): void
46
    {
47
        $e = $this->bufferEmitter;
48
49
        $app = $this->makeApp();
50
51
        $_SERVER['REQUEST_URI'] = '/index/dave';
52
        $app->get(SapiDispatcher::class)->serve($e);
53
54
        $this->assertSame('Hello, dave.', (string)$e->response->getBody());
0 ignored issues
show
Bug introduced by
Accessing response on the interface Spiral\Http\EmitterInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
    }
56
57
    public function testDispatchError(): void
58
    {
59
        $e = $this->bufferEmitter;
60
61
        $files = $this->app->get(FilesInterface::class)->getFiles(
62
            $this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/'
63
        );
64
65
        $this->assertCount(0, $files);
66
67
        $_SERVER['REQUEST_URI'] = '/error';
68
        $this->app->get(SapiDispatcher::class)->serve($e);
69
70
        $files = $this->app->get(FilesInterface::class)->getFiles(
71
            $this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/'
72
        );
73
74
        $this->assertCount(1, $files);
75
76
        $this->assertStringContainsString('500', (string)$e->response->getBody());
0 ignored issues
show
Bug introduced by
Accessing response on the interface Spiral\Http\EmitterInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
77
    }
78
79
    public function testDispatchNativeError(): void
80
    {
81
        $e = $this->bufferEmitter;
82
83
        $app = $this->makeApp([
84
            'DEBUG' => true
85
        ]);
86
87
        $files = $app->get(FilesInterface::class)->getFiles(
88
            $app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/'
89
        );
90
91
        $this->assertCount(0, $files);
92
93
        $_SERVER['REQUEST_URI'] = '/error';
94
        $app->get(SapiDispatcher::class)->serve($e);
95
96
        $files = $app->get(FilesInterface::class)->getFiles(
97
            $app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/'
98
        );
99
100
        $this->assertCount(1, $files);
101
102
        $this->assertStringContainsString('undefined', (string)$e->response->getBody());
0 ignored issues
show
Bug introduced by
Accessing response on the interface Spiral\Http\EmitterInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
103
    }
104
}
105