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