1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Collector; |
6
|
|
|
|
7
|
|
|
use Nyholm\Psr7\Response; |
8
|
|
|
use Nyholm\Psr7\ServerRequest; |
9
|
|
|
use Yiisoft\Di\Container; |
10
|
|
|
use Yiisoft\Di\ContainerConfig; |
11
|
|
|
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware; |
12
|
|
|
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware; |
13
|
|
|
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory; |
14
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
15
|
|
|
use Yiisoft\Yii\Debug\Collector\MiddlewareCollector; |
16
|
|
|
use Yiisoft\Yii\Debug\Tests\Support\DummyMiddleware; |
17
|
|
|
|
18
|
|
|
final class MiddlewareCollectorTest extends CollectorTestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param \Yiisoft\Yii\Debug\Collector\CollectorInterface|\Yiisoft\Yii\Debug\Collector\MiddlewareCollector $collector |
22
|
|
|
*/ |
23
|
|
|
protected function collectTestData(CollectorInterface $collector): void |
24
|
|
|
{ |
25
|
|
|
$collector->collect(new BeforeMiddleware($this->createCallableMiddleware(static fn () => 1), new ServerRequest('GET', '/test'))); |
|
|
|
|
26
|
|
|
$collector->collect(new BeforeMiddleware($this->createCallableMiddleware([DummyMiddleware::class, 'process']), new ServerRequest('GET', '/test'))); |
27
|
|
|
$collector->collect(new BeforeMiddleware(new DummyMiddleware(), new ServerRequest('GET', '/test'))); |
28
|
|
|
$collector->collect(new AfterMiddleware(new DummyMiddleware(), new Response(200))); |
29
|
|
|
$collector->collect(new AfterMiddleware($this->createCallableMiddleware(static fn () => 1), new Response(200))); |
30
|
|
|
$collector->collect(new AfterMiddleware($this->createCallableMiddleware([DummyMiddleware::class, 'process']), new Response(200))); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function getCollector(): CollectorInterface |
34
|
|
|
{ |
35
|
|
|
return new MiddlewareCollector(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function checkCollectedData(CollectorInterface $collector): void |
39
|
|
|
{ |
40
|
|
|
parent::checkCollectedData($collector); |
41
|
|
|
|
42
|
|
|
$data = $collector->getCollected(); |
43
|
|
|
|
44
|
|
|
$this->assertNotEmpty($data['beforeStack']); |
45
|
|
|
$this->assertNotEmpty($data['afterStack']); |
46
|
|
|
$this->assertNotEmpty($data['actionHandler']); |
47
|
|
|
$this->assertEquals(DummyMiddleware::class, $data['actionHandler']['name']); |
48
|
|
|
$this->assertEquals('GET', $data['actionHandler']['request']->getMethod()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function createCallableMiddleware(callable|array $callable): \Psr\Http\Server\MiddlewareInterface |
52
|
|
|
{ |
53
|
|
|
$factory = new MiddlewareFactory(new Container(ContainerConfig::create())); |
54
|
|
|
return $factory->create($callable); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|