ConfigurationTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 98
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultConfiguration() 0 23 1
A testKeysAreNotNormalized() 0 22 1
A testWithYamlConfig() 0 18 1
A getDefaultConfiguration() 0 21 1
A processConfiguration() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\Tests\DependencyInjection;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\SeoBundle\DependencyInjection\Configuration;
18
use Symfony\Component\Config\Definition\Processor;
19
use Symfony\Component\Yaml\Yaml;
20
21
final class ConfigurationTest extends TestCase
22
{
23
    public function testDefaultConfiguration(): void
24
    {
25
        $expected = [
26
            'encoding' => 'UTF-8',
27
            'page' => [
28
                'default' => 'sonata.seo.page.default',
29
                'head' => [],
30
                'metas' => [],
31
                'separator' => ' - ',
32
                'title' => 'Project name',
33
            ],
34
            'sitemap' => [
35
                'doctrine_orm' => [],
36
                'services' => [],
37
            ],
38
            'http' => [
39
                'client' => null,
40
                'message_factory' => null,
41
            ],
42
        ];
43
44
        $this->assertSame($expected, $this->processConfiguration([[]]));
45
    }
46
47
    public function testKeysAreNotNormalized(): void
48
    {
49
        $values = [
50
            'page' => [
51
                'head' => ['data-example' => 'abc-123'],
52
                'metas' => [
53
                    'http-equiv' => [
54
                        'Content-Type' => 'text/html; charset=utf-8',
55
                    ],
56
                ],
57
            ],
58
        ];
59
60
        $config = $this->processConfiguration([$values]);
61
62
        $expected = array_merge_recursive(
63
            $this->getDefaultConfiguration(),
64
            $values
65
        );
66
67
        $this->assertSame($expected, $config);
68
    }
69
70
    public function testWithYamlConfig(): void
71
    {
72
        $values = Yaml::parse(
73
            file_get_contents(__DIR__.'/data/config.yml'),
74
            Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE
75
        );
76
77
        $config = $this->processConfiguration([$values]);
78
79
        $expected = array_merge_recursive(
80
            $this->getDefaultConfiguration(),
81
            $values
82
        );
83
84
        $this->assertSame($expected, $config);
85
86
        $this->assertSame('website', $config['page']['metas']['property']['og:type']);
87
    }
88
89
    private function getDefaultConfiguration()
90
    {
91
        return [
92
            'page' => [
93
                'head' => [],
94
                'metas' => [],
95
                'default' => 'sonata.seo.page.default',
96
                'separator' => ' - ',
97
                'title' => 'Project name',
98
            ],
99
            'encoding' => 'UTF-8',
100
            'sitemap' => [
101
                'doctrine_orm' => [],
102
                'services' => [],
103
            ],
104
            'http' => [
105
                'client' => null,
106
                'message_factory' => null,
107
            ],
108
        ];
109
    }
110
111
    private function processConfiguration(array $configs)
112
    {
113
        $configuration = new Configuration();
114
        $processor = new Processor();
115
116
        return $processor->processConfiguration($configuration, $configs);
117
    }
118
}
119