AnnotationServiceProviderTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerControllersByDirectoryTestProvider() 0 22 1
A testRegisterControllersByDirectory() 0 7 1
A testControllerCache() 0 19 1
1
<?php
2
/**
3
 * This file is part of the silex-annotation-provider package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license       MIT License
8
 * @copyright (c) 2018, Dana Desrosiers <[email protected]>
9
 */
10
11
namespace DDesrosiers\Test\SilexAnnotations;
12
13
use DDesrosiers\SilexAnnotations\AnnotationService;
14
use DDesrosiers\SilexAnnotations\ControllerFinder;
15
use DDesrosiers\Test\SilexAnnotations\Controller\AfterCollectionTestController;
16
use DDesrosiers\Test\SilexAnnotations\Controller\AssertCollectionTestController;
17
use DDesrosiers\Test\SilexAnnotations\Controller\BeforeCollectionTestController;
18
use DDesrosiers\Test\SilexAnnotations\Controller\ConvertCollectionTestController;
19
use DDesrosiers\Test\SilexAnnotations\Controller\HostCollectionTestController;
20
use DDesrosiers\Test\SilexAnnotations\Controller\RequireHttpCollectionTestController;
21
use DDesrosiers\Test\SilexAnnotations\Controller\RequireHttpsCollectionTestController;
22
use DDesrosiers\Test\SilexAnnotations\Controller\SecureCollectionTestController;
23
use DDesrosiers\Test\SilexAnnotations\Controller\SubDir\SubDirTestController;
24
use DDesrosiers\Test\SilexAnnotations\Controller\TestController;
25
use DDesrosiers\Test\SilexAnnotations\Controller\TestController2;
26
use DDesrosiers\Test\SilexAnnotations\Controller\ValueTestController;
27
28
class AnnotationServiceProviderTest extends AnnotationTestBase
29
{
30
    public function registerControllersByDirectoryTestProvider()
31
    {
32
        $allControllers = [
33
            AfterCollectionTestController::class,
34
            AssertCollectionTestController::class,
35
            BeforeCollectionTestController::class,
36
            ConvertCollectionTestController::class,
37
            HostCollectionTestController::class,
38
            RequireHttpCollectionTestController::class,
39
            RequireHttpsCollectionTestController::class,
40
            SecureCollectionTestController::class,
41
            SubDirTestController::class,
42
            TestController::class,
43
            TestController2::class,
44
            ValueTestController::class
45
        ];
46
47
        return array(
48
            array("/SubDir", [SubDirTestController::class]),
49
            array('', $allControllers)
50
        );
51
    }
52
53
    /**
54
     * @dataProvider registerControllersByDirectoryTestProvider
55
     * @param $dir
56
     * @param $result
57
     */
58
    public function testRegisterControllersByDirectory($dir, $result)
59
    {
60
        $controllerFinder = new ControllerFinder(self::$CONTROLLER_DIR . $dir, []);
61
        $files = $controllerFinder->getControllerClasses();
62
        sort($files);
63
        self::assertEquals($result, $files);
64
    }
65
66
    /**
67
     * @throws \Psr\SimpleCache\InvalidArgumentException
68
     */
69
    public function testControllerCache()
70
    {
71
        $_SERVER['REQUEST_URI'] = '';
72
        $cacheKey = AnnotationService::CONTROLLER_CACHE_INDEX;
73
        $cache = new TestArrayCache();
74
        $this->app['annot.cache'] = $cache;
75
        $this->app['debug'] = false;
76
        $service = $this->registerProviders();
77
        $service->registerControllers();
78
        $this->assertCount(12, $this->flattenControllerArray($cache->get($cacheKey)));
0 ignored issues
show
Documentation introduced by
$this->flattenController...$cache->get($cacheKey)) is of type array<integer,string>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
80
        $cache->clearWasFetched();
81
        $service->registerControllers();
82
        $this->assertTrue($cache->wasFetched($cacheKey));
83
        $controllers = $cache->get($cacheKey);
84
        $this->assertContains(SubDirTestController::class, $controllers['/']);
85
        $this->assertContains(TestController::class, $controllers['/test']);
86
        $this->assertCount(12, $this->flattenControllerArray($controllers));
0 ignored issues
show
Documentation introduced by
$this->flattenControllerArray($controllers) is of type array<integer,string>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
    }
88
}
89