Completed
Pull Request — 3.x (#871)
by
unknown
02:03
created

ConfigurationTest::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Tests;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Configuration;
16
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Symfony\Component\Config\Definition\Processor;
19
20
class ConfigurationTest extends TestCase
21
{
22
    public function testDefaultOptions()
23
    {
24
        $config = $this->process([]);
25
26
        $this->assertNull($config['entity_manager']);
27
        $this->assertTrue($config['audit']['force']);
28
        $this->assertArraySubset(['@SonataDoctrineORMAdmin/Form/form_admin_fields.html.twig'], $config['templates']['form']);
29
        $this->assertArraySubset(['@SonataDoctrineORMAdmin/Form/filter_admin_fields.html.twig'], $config['templates']['filter']);
30
        $this->assertArrayNotHasKey('types', $config['templates']);
31
    }
32
33
    public function testAuditForceWithInvalidFormat()
34
    {
35
        $this->expectException(InvalidTypeException::class);
36
37
        $config = $this->process([[
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
            'audit' => [
39
                'force' => '1',
40
            ],
41
        ]]);
42
    }
43
44
    public function testCustomTemplates()
45
    {
46
        $config = $this->process([[
47
            'templates' => [
48
                'form' => ['form.twig.html', 'form_extra.twig.html'],
49
                'filter' => ['filter.twig.html'],
50
                'types' => [
51
                    'list' => [
52
                        'array' => 'list_array.twig.html',
53
                    ],
54
                    'show' => [
55
                        'array' => 'show_array.twig.html',
56
                    ],
57
                ],
58
            ],
59
        ]]);
60
61
        $this->assertSame(['form.twig.html', 'form_extra.twig.html'], $config['templates']['form']);
62
        $this->assertSame(['filter.twig.html'], $config['templates']['filter']);
63
        $this->assertSame([
64
            'list' => [
65
                'array' => 'list_array.twig.html',
66
            ],
67
            'show' => [
68
                'array' => 'show_array.twig.html',
69
            ],
70
        ], $config['templates']['types']);
71
    }
72
73
    public function testTemplateTypesWithInvalidValues()
74
    {
75
        $this->expectException(InvalidConfigurationException::class);
76
77
        $config = $this->process([[
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
            'templates' => [
79
                'types' => [
80
                    'edit' => [],
81
                ],
82
            ],
83
        ]]);
84
    }
85
86
    /**
87
     * Processes an array of configurations and returns a compiled version.
88
     *
89
     * @param array $configs An array of raw configurations
90
     *
91
     * @return array A normalized array
92
     */
93
    protected function process($configs)
94
    {
95
        $processor = new Processor();
96
97
        return $processor->processConfiguration(new Configuration(), $configs);
98
    }
99
}
100