Completed
Push — ezp-31079-twig-variables ( 1c4141 )
by
unknown
14:24
created

VariableProviderRegistryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 69.23 %

Importance

Changes 0
Metric Value
dl 54
loc 78
rs 10
c 0
b 0
f 0
wmc 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 4 4 1
A hp$0 ➔ getIdentifier() 4 4 1
A hp$0 ➔ getTwigVariables() 6 6 1
A getRegistry() 0 6 1
A getProvider() 23 23 1
A testParameterProviderGetter() 15 15 1
A testParameterProviderSetter() 16 16 1
A testParameterProviderChecker() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace EzSystems\Tests\EzPlatformTemplating\View;
10
11
use ArrayIterator;
12
use eZ\Publish\Core\MVC\Symfony\View\GenericVariableProviderRegistry;
13
use eZ\Publish\Core\MVC\Symfony\View\VariableProvider;
14
use eZ\Publish\Core\MVC\Symfony\View\View;
15
use PHPUnit\Framework\TestCase;
16
17
final class VariableProviderRegistryTest extends TestCase
18
{
19
    private function getRegistry(array $providers): GenericVariableProviderRegistry
20
    {
21
        return new GenericVariableProviderRegistry(
22
            new ArrayIterator($providers)
23
        );
24
    }
25
26 View Code Duplication
    private function getProvider(string $identifier): VariableProvider
27
    {
28
        return new class($identifier) implements VariableProvider {
29
            private $identifier;
30
31
            public function __construct(string $identifier)
32
            {
33
                $this->identifier = $identifier;
34
            }
35
36
            public function getIdentifier(): string
37
            {
38
                return $this->identifier;
39
            }
40
41
            public function getTwigVariables(View $view, array $options = []): object
42
            {
43
                return (object)[
44
                    $this->identifier . '_parameter' => $this->identifier . '_value',
45
                ];
46
            }
47
        };
48
    }
49
50 View Code Duplication
    public function testParameterProviderGetter(): void
51
    {
52
        $registry = $this->getRegistry([
53
            $this->getProvider('provider_a'),
54
            $this->getProvider('provider_b'),
55
        ]);
56
57
        $providerA = $registry->getTwigVariableProvider('provider_a');
58
        $providerB = $registry->getTwigVariableProvider('provider_b');
59
        $providerC = $registry->getTwigVariableProvider('provider_c');
60
61
        $this->assertEquals($providerA->getIdentifier(), 'provider_a');
62
        $this->assertEquals($providerB->getIdentifier(), 'provider_b');
63
        $this->assertNull($providerC);
64
    }
65
66 View Code Duplication
    public function testParameterProviderSetter(): void
67
    {
68
        $registry = $this->getRegistry([
69
            $this->getProvider('provider_a'),
70
            $this->getProvider('provider_b'),
71
        ]);
72
73
        $providerC = $registry->getTwigVariableProvider('provider_c');
74
75
        $this->assertNull($providerC);
76
77
        $registry->setTwigVariableProvider($this->getProvider('provider_c'));
78
79
        $providerC = $registry->getTwigVariableProvider('provider_c');
80
        $this->assertEquals($providerC->getIdentifier(), 'provider_c');
81
    }
82
83
    public function testParameterProviderChecker(): void
84
    {
85
        $registry = $this->getRegistry([
86
            $this->getProvider('provider_a'),
87
            $this->getProvider('provider_b'),
88
        ]);
89
90
        $this->assertTrue($registry->hasTwigVariableProvider('provider_a'));
91
        $this->assertTrue($registry->hasTwigVariableProvider('provider_b'));
92
        $this->assertFalse($registry->hasTwigVariableProvider('provider_c'));
93
    }
94
}
95