ControllerRegistryTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 92
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ getControllerIdentifiers() 0 4 1
A hp$0 ➔ getExpirationTag() 0 4 1
B testControllerRegistryWithDetector() 0 32 1
A setUp() 0 5 1
A testControllerRegistry() 0 19 1
B testControllerRegistryThisParam() 0 30 1
1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Mouf\Mvc\Splash\Fixtures\TestController;
8
use Mouf\Mvc\Splash\Fixtures\TestController2;
9
use Mouf\Picotainer\Picotainer;
10
11
class ControllerRegistryTest extends \PHPUnit_Framework_TestCase
12
{
13
    protected function setUp()
14
    {
15
        $loader = require __DIR__.'../../../../../../vendor/autoload.php';
16
        AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
17
    }
18
19
    public function testControllerRegistry()
20
    {
21
        $container = new Picotainer([
22
           'controller' => function () {
23
               return new TestController();
24
           },
25
        ]);
26
27
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
28
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
29
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer);
30
        $controllerRegistry->addController('controller');
31
32
        $urlsList = $controllerRegistry->getUrlsList('foo');
33
34
        $this->assertCount(1, $urlsList);
35
        $this->assertInstanceOf(SplashRoute::class, $urlsList[0]);
36
        $this->assertEquals('myurl', $urlsList[0]->getUrl());
37
    }
38
39
    public function testControllerRegistryThisParam()
40
    {
41
        $container = new Picotainer([
42
            'controller' => function () {
43
                return new TestController2();
44
            },
45
        ]);
46
47
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
48
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
49
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
50
51
        $urlsList = $controllerRegistry->getUrlsList('foo');
52
53
        $this->assertCount(5, $urlsList);
54
        $this->assertInstanceOf(SplashRoute::class, $urlsList[0]);
55
        $this->assertEquals('url/42/foo/52', $urlsList[0]->getUrl());
56
57
        $this->assertInstanceOf(SplashRoute::class, $urlsList[1]);
58
        $this->assertEquals('controller/actionAnnotation', $urlsList[1]->getUrl());
59
60
        $this->assertInstanceOf(SplashRoute::class, $urlsList[2]);
61
        $this->assertEquals('controller/', $urlsList[2]->getUrl());
62
        $this->assertInstanceOf(SplashRoute::class, $urlsList[2]);
63
        $this->assertEquals('Main page', $urlsList[2]->getTitle());
64
        $this->assertContains('GET', $urlsList[2]->getHttpMethods());
65
        $this->assertContains('POST', $urlsList[2]->getHttpMethods());
66
        $this->assertContains('PUT', $urlsList[2]->getHttpMethods());
67
        $this->assertContains('DELETE', $urlsList[2]->getHttpMethods());
68
    }
69
70
    public function testControllerRegistryWithDetector()
71
    {
72
        $container = new Picotainer([
73
            'controller' => function () {
74
                return new TestController();
75
            },
76
        ]);
77
78
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
79
80
        $detector = new class implements ControllerDetector
81
 {
82
     public function getControllerIdentifiers(ControllerAnalyzer $controllerAnalyzer) : array
83
     {
84
         return ['controller'];
85
     }
86
87
     public function getExpirationTag() : string
88
     {
89
         return '';
90
     }
91
 };
92
93
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
94
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, [], $detector);
95
96
        $urlsList = $controllerRegistry->getUrlsList('foo');
97
98
        $this->assertCount(1, $urlsList);
99
        $this->assertInstanceOf(SplashRoute::class, $urlsList[0]);
100
        $this->assertEquals('myurl', $urlsList[0]->getUrl());
101
    }
102
}
103