Passed
Pull Request — master (#156)
by Wilmer
06:05 queued 03:57
created

RouterCollectorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 34
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A collectTestData() 0 7 1
A createRoutes() 0 5 1
A getCollector() 0 15 1
A checkCollectedData() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Collector;
6
7
use Yiisoft\Di\Container;
8
use Yiisoft\Di\ContainerConfig;
9
use Yiisoft\Router\Group;
10
use Yiisoft\Router\Route;
11
use Yiisoft\Router\RouteCollection;
12
use Yiisoft\Router\RouteCollectionInterface;
13
use Yiisoft\Router\RouteCollector;
14
use Yiisoft\Router\RouteCollectorInterface;
15
use Yiisoft\Router\UrlMatcherInterface;
16
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
17
use Yiisoft\Yii\Debug\Collector\RouterCollector;
18
19
final class RouterCollectorTest extends CollectorTestCase
20
{
21
    /**
22
     * @var \PHPUnit\Framework\MockObject\MockObject|\Yiisoft\Router\RouteCollectorInterface
23
     */
24
    private $routeCollector;
25
26
    private ?Container $container = null;
27
28
    /**
29
     * @param \Yiisoft\Yii\Debug\Collector\CollectorInterface|\Yiisoft\Yii\Debug\Collector\RouterCollector $collector
30
     */
31
    protected function collectTestData(CollectorInterface $collector): void
32
    {
33
        $routes = $this->createRoutes();
34
        $this->routeCollector
35
            ->method('getItems')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Yiisoft\Router\RouteCollectorInterface. ( Ignorable by Annotation )

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

35
            ->/** @scrutinizer ignore-call */ 
36
              method('getItems')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
            ->willReturn($routes);
37
        $collector->collect(0.001);
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

37
        $collector->/** @scrutinizer ignore-call */ 
38
                    collect(0.001);
Loading history...
38
    }
39
40
    protected function getCollector(): CollectorInterface
41
    {
42
        $this->routeCollector = $this->createMock(RouteCollectorInterface::class);
43
        $routeCollector = new RouteCollector();
44
        $routeCollector->addGroup(Group::create()->routes(...$this->createRoutes()));
45
46
        $config = ContainerConfig::create()
47
            ->withDefinitions([
48
                UrlMatcherInterface::class => $this->routeCollector,
49
                RouteCollectionInterface::class => RouteCollection::class,
50
                RouteCollectorInterface::class => $routeCollector,
51
            ]);
52
        $this->container = new Container($config);
53
54
        return new RouterCollector($this->container);
55
    }
56
57
    protected function checkCollectedData(CollectorInterface $collector): void
58
    {
59
        parent::checkCollectedData($collector);
60
        $this->assertArrayHasKey('routes', $collector->getCollected());
61
        $this->assertArrayHasKey('routesTree', $collector->getCollected());
62
        $this->assertArrayHasKey('routeTime', $collector->getCollected());
63
        $this->assertEquals(
64
            $this->container->get(RouteCollectionInterface::class)->getRoutes(),
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

64
            $this->container->/** @scrutinizer ignore-call */ 
65
                              get(RouteCollectionInterface::class)->getRoutes(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            $collector->getCollected()['routes']
66
        );
67
        $this->assertEquals(
68
            $this->container->get(RouteCollectionInterface::class)->getRouteTree(),
69
            $collector->getCollected()['routesTree']
70
        );
71
        $this->assertEquals(
72
            0.001,
73
            $collector->getCollected()['routeTime']
74
        );
75
    }
76
77
    private function createRoutes(): array
78
    {
79
        return [
80
            Route::get('/'),
81
            Group::create('/api')->routes(Route::get('/v1')),
82
        ];
83
    }
84
}
85