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()); |
|
|
|
|
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()); |
|
|
|
|
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()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|