Completed
Push — master ( c5c436...b453a3 )
by Ruud
15:58
created

ImportCommandHandlerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A testExecuteImportCommand() 0 11 1
A testdetermineLocalesToImport() 0 12 1
A testParseRequestedLocalesMulti() 0 7 1
A testParseRequestedLocalesSingle() 0 7 1
A testParseRequestedLocalesArray() 0 7 1
A testImportBundleTranslationFiles() 15 15 2
A testImportSf4TranslationFiles() 15 15 2

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 Kunstmaan\TranslatorBundle\Tests\Service\Importer;
4
5
use Kunstmaan\TranslatorBundle\Model\Import\ImportCommand;
6
use Kunstmaan\TranslatorBundle\Tests\unit\WebTestCase;
7
use Symfony\Component\HttpKernel\Kernel;
8
9
class ImportCommandHandlerTest extends WebTestCase
10
{
11
    private $importCommandHandler;
12
13 View Code Duplication
    public function setUp()
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...
14
    {
15
        static::bootKernel(['test_case' => 'TranslatorBundleTest', 'root_config' => 'config.yaml']);
16
        $container = static::$kernel->getContainer();
17
        static::loadFixtures($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by static::$kernel->getContainer() on line 16 can be null; however, Kunstmaan\TranslatorBund...estCase::loadFixtures() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
18
19
        $this->importCommandHandler = $container->get('kunstmaan_translator.service.importer.command_handler');
20
    }
21
22
    /**
23
     * @group handler
24
     */
25
    public function testExecuteImportCommand()
26
    {
27
        $importCommand = new ImportCommand();
28
        $importCommand
29
            ->setForce(false)
30
            ->setLocales(false)
31
            ->setGlobals(true)
32
            ->setDefaultBundle(false);
33
34
        $this->assertEquals(7, $this->importCommandHandler->executeImportCommand($importCommand));
35
    }
36
37
    public function testdetermineLocalesToImport()
38
    {
39
        $importCommand = new ImportCommand();
40
        $importCommand
41
            ->setForce(false)
42
            ->setLocales(false)
43
            ->setGlobals(true)
44
            ->setDefaultBundle(false);
45
46
        $locales = $this->importCommandHandler->determineLocalesToImport($importCommand);
47
        $this->assertEquals(array('nl', 'en', 'de'), $locales);
48
    }
49
50
    public function testParseRequestedLocalesMulti()
51
    {
52
        $locale = 'nl,De,   FR';
53
        $expectedArray = array('nl', 'de', 'fr');
54
        $locales = $this->importCommandHandler->parseRequestedLocales($locale);
55
        $this->assertEquals($expectedArray, $locales);
56
    }
57
58
    public function testParseRequestedLocalesSingle()
59
    {
60
        $locale = 'dE';
61
        $expectedArray = array('de');
62
        $locales = $this->importCommandHandler->parseRequestedLocales($locale);
63
        $this->assertEquals($expectedArray, $locales);
64
    }
65
66
    public function testParseRequestedLocalesArray()
67
    {
68
        $locale = array('dE', 'NL', 'es');
69
        $expectedArray = array('de', 'nl', 'es');
70
        $locales = $this->importCommandHandler->parseRequestedLocales($locale);
71
        $this->assertEquals($expectedArray, $locales);
72
    }
73
74
    /**
75
     * @group legacy
76
     */
77 View Code Duplication
    public function testImportBundleTranslationFiles()
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...
78
    {
79
        if (Kernel::VERSION_ID >= 40000) {
80
            $this->markTestSkipped('Skip symfony 3 test');
81
        }
82
83
        $importCommand = new ImportCommand();
84
        $importCommand
85
            ->setForce(false)
86
            ->setLocales(false)
87
            ->setGlobals(true)
88
            ->setDefaultBundle('own');
89
90
        $this->assertEquals(0, $this->importCommandHandler->importBundleTranslationFiles($importCommand));
91
    }
92
93 View Code Duplication
    public function testImportSf4TranslationFiles()
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...
94
    {
95
        if (Kernel::VERSION_ID < 40000) {
96
            $this->markTestSkipped('Skip symfony 4 test');
97
        }
98
99
        $importCommand = new ImportCommand();
100
        $importCommand
101
            ->setForce(false)
102
            ->setLocales(false)
103
            ->setGlobals(true)
104
            ->setDefaultBundle('own');
105
106
        $this->assertEquals(7, $this->importCommandHandler->importBundleTranslationFiles($importCommand));
107
    }
108
}
109