Passed
Push — master ( 8ace39...71280b )
by Wilmer
16:17 queued 02:55
created

MiddlewareCollectorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 2
b 0
f 0
dl 0
loc 37
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollector() 0 3 1
A createCallableMiddleware() 0 4 1
A checkCollectedData() 0 11 1
A collectTestData() 0 8 1
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')));
0 ignored issues
show
Bug introduced by
The method collect() does not exist on Yiisoft\Yii\Debug\Collector\CollectorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Yii\Debug\Collector\QueueCollector. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $collector->/** @scrutinizer ignore-call */ 
26
                    collect(new BeforeMiddleware($this->createCallableMiddleware(static fn () => 1), new ServerRequest('GET', '/test')));
Loading history...
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