ConfigurationTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testItShouldBeAConfiguration() 0 4 1
A testItShouldBecomeTheRootNodeAsParameter() 0 8 1
A testItShouldForbidInvalidConfigurations() 0 7 1
A provideInvalidConfigurations() 0 8 1
A processConfiguration() 0 5 1
A testItShouldAcceptValidConfigurations() 0 5 1
A provideValidConfigurations() 0 12 1
1
<?php
2
namespace DkplusTest\CsrfApiUnprotectionBundle\DependencyInjection;
3
4
use Dkplus\CsrfApiUnprotectionBundle\DependencyInjection\Configuration;
5
use PHPUnit_Framework_TestCase as TestCase;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8
9
/**
10
 * @covers Dkplus\CsrfApiUnprotectionBundle\DependencyInjection\Configuration
11
 */
12
class ConfigurationTest extends TestCase
13
{
14
    public function testItShouldBeAConfiguration()
15
    {
16
        $this->assertInstanceOf(ConfigurationInterface::class, new Configuration('dkplus_csrf_api_unprotection'));
17
    }
18
19
    public function testItShouldBecomeTheRootNodeAsParameter()
20
    {
21
        $configuration = new Configuration('dkplus_csrf_api_unprotection');
22
        $this->assertSame(
23
            'dkplus_csrf_api_unprotection',
24
            $configuration->getConfigTreeBuilder()->buildTree()->getPath()
25
        );
26
    }
27
28
    /**
29
     * @dataProvider provideInvalidConfigurations
30
     *
31
     * @param array $input
32
     */
33
    public function testItShouldForbidInvalidConfigurations(array $input)
34
    {
35
        $configuration = new Configuration('dkplus_csrf_api_unprotection');
36
37
        $this->setExpectedException(InvalidConfigurationException::class);
38
        $this->processConfiguration($configuration, $input);
39
    }
40
41
    public static function provideInvalidConfigurations()
42
    {
43
        return [
44
            [['rules' => null]],
45
            [['rules' => []]],
46
            [['rules' => ['match_uri' => [null]]]]
47
        ];
48
    }
49
50
    /**
51
     * @param Configuration $configuration
52
     * @param array         $input
53
     * @return array The processed configuration
54
     */
55
    private function processConfiguration(Configuration $configuration, array $input = null)
56
    {
57
        $tree = $configuration->getConfigTreeBuilder()->buildTree();
58
        return $tree->finalize($tree->normalize($input));
59
    }
60
61
    /**
62
     * @dataProvider provideValidConfigurations
63
     *
64
     * @param array|null $input
65
     * @param array      $processedConfig
66
     */
67
    public function testItShouldAcceptValidConfigurations($input, array $processedConfig)
68
    {
69
        $configuration = new Configuration('dkplus_csrf_api_unprotection');
70
        $this->assertSame($processedConfig, $this->processConfiguration($configuration, $input));
71
    }
72
73
    public static function provideValidConfigurations()
74
    {
75
        $defaultProcessedConfiguration = ['rules' => ['match_uri' => ['#^(/app(_[a-zA-Z]*)?.php)?/api/#']]];
76
        return [
77
            [null,                                          $defaultProcessedConfiguration],
78
            [[],                                            $defaultProcessedConfiguration],
79
            [['rules' => ['match_uri' => null]],            ['rules' => ['match_uri' => []]]],
80
            [['rules' => ['match_uri' => []]],              ['rules' => ['match_uri' => []]]],
81
            [['rules' => ['match_uri' => ['/^\/api/.*/']]], ['rules' => ['match_uri' => ['/^\/api/.*/']]]],
82
            [['rules' => ['match_uri' => '/^\/api/.*/']],   ['rules' => ['match_uri' => ['/^\/api/.*/']]]],
83
        ];
84
    }
85
}
86