FrameworkExtensionTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 144
Duplicated Lines 9.72 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 14
loc 144
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getContainer() 0 14 1
A tearDown() 0 4 1
A testGetFunctions() 0 14 3
A testBundleExists() 0 5 1
A testConvertToTranslationTrim() 0 6 1
A testConvertToTranslationToLowerCase() 7 7 1
A testConvertToTranslationReplaceFakeSpaces() 0 9 1
A testTranslationWithNumbersAtTheEnd() 0 6 1
A testTranslationWithSumoCodersInTheString() 7 7 1
A testTranslationWithBundleInTheString() 0 7 1
A testTranslationWithDotsInTheString() 0 4 1
A testTranslationsWithNumbersAsSingleItems() 0 4 1
A testIfFrameworkIsRemoved() 0 6 1
A testIfBundleIsCleaned() 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
namespace SumoCoders\FrameworkCoreBundle\Tests\Twig;
4
5
use SumoCoders\FrameworkCoreBundle\Twig\FrameworkExtension;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
8
class FrameworkExtensionTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var FrameworkExtension
12
     */
13
    protected $frameworkExtension;
14
15
    /**
16
     * @inherit
17
     */
18
    protected function setUp()
19
    {
20
        $this->frameworkExtension = new FrameworkExtension(
21
            $this->getContainer()
22
        );
23
    }
24
25
    /**
26
     * @return \PHPUnit_Framework_MockObject_MockObject
27
     */
28
    protected function getContainer()
29
    {
30
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
31
        $container->method('getParameter')
32
            ->will(
33
                $this->returnValue(
34
                    [
35
                        'existing' => null,
36
                    ]
37
                )
38
            );
39
40
        return $container;
41
    }
42
43
    /**
44
     * @inherit
45
     */
46
    protected function tearDown()
47
    {
48
        $this->frameworkExtension = null;
49
    }
50
51
    /**
52
     * Test FrameworkExtension->getFunctions()
53
     */
54
    public function testGetFunctions()
55
    {
56
        $var = $this->frameworkExtension->getFunctions();
57
        $this->assertInternalType('array', $var);
58
59
        $containsBundleExists = false;
60
        foreach ($var as $function) {
61
            /** @var \Twig_SimpleFunction $function */
62
            if ($function->getName() == 'bundleExists') {
63
                $containsBundleExists = true;
64
            }
65
        }
66
        $this->assertTrue($containsBundleExists, 'bundleExists-function not found');
67
    }
68
69
    public function testBundleExists()
70
    {
71
        $this->assertTrue($this->frameworkExtension->bundleExists('existing'), 'existing bundle not found');
72
        $this->assertFalse($this->frameworkExtension->bundleExists('non-existing'), 'non-existing bundle found');
73
    }
74
75
    public function testConvertToTranslationTrim()
76
    {
77
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation(' foo'));
78
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo '));
79
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation(' foo '));
80
    }
81
82 View Code Duplication
    public function testConvertToTranslationToLowerCase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo'));
85
        $this->assertEquals('FOO', $this->frameworkExtension->convertToTranslation('FOO'));
86
        $this->assertEquals('FoO', $this->frameworkExtension->convertToTranslation('FoO'));
87
        $this->assertEquals('fOo', $this->frameworkExtension->convertToTranslation('fOo'));
88
    }
89
90
    public function testConvertToTranslationReplaceFakeSpaces()
91
    {
92
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('f_o_o'));
93
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('f-o-o'));
94
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('f o o'));
95
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('f_o_o_'));
96
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('_f_o_o'));
97
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('_f_o_o_'));
98
    }
99
100
    public function testTranslationWithNumbersAtTheEnd()
101
    {
102
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo1'));
103
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo123'));
104
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo 123'));
105
    }
106
107 View Code Duplication
    public function testTranslationWithSumoCodersInTheString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('SumoCoders.foo'));
110
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('sumocoders.foo'));
111
        $this->assertEquals('foo.sumocoders', $this->frameworkExtension->convertToTranslation('foo.sumocoders'));
112
        $this->assertEquals('foo.bar', $this->frameworkExtension->convertToTranslation('SumoCoders.foo.bar'));
113
    }
114
115
    public function testTranslationWithBundleInTheString()
116
    {
117
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('fooBundle'));
118
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo.frameworkBundle'));
119
        $this->assertEquals('Foo.bar', $this->frameworkExtension->convertToTranslation('FooBundle.bar'));
120
        $this->assertEquals('Foo.bar', $this->frameworkExtension->convertToTranslation('Foo.barBundle'));
121
    }
122
123
    public function testTranslationWithDotsInTheString()
124
    {
125
        $this->assertEquals('foo.bar', $this->frameworkExtension->convertToTranslation('foo.......bar'));
126
    }
127
128
    public function testTranslationsWithNumbersAsSingleItems()
129
    {
130
        $this->assertEquals('f.o.o', $this->frameworkExtension->convertToTranslation('f_1_o_o'));
131
    }
132
133
    public function testIfFrameworkIsRemoved()
134
    {
135
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('framework_foo'));
136
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('framework_foo_framework'));
137
        $this->assertEquals('foo', $this->frameworkExtension->convertToTranslation('foo_framework'));
138
    }
139
140
    public function testIfBundleIsCleaned()
141
    {
142
        $this->assertEquals(
143
            'Core.foo',
144
            $this->frameworkExtension->convertToTranslation('SumoCoders_FrameworkCoreBundle_foo')
145
        );
146
        $this->assertEquals(
147
            'Namespace.NameOfThe.foo',
148
            $this->frameworkExtension->convertToTranslation('Namespace_NameOfTheBundle_foo')
149
        );
150
    }
151
}
152