Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

TranslatorManagerSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 33.05 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 0
cbo 4
dl 39
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_translate_the_given_category_in_the_given_language() 0 21 1
A it__try_to_translate_the_given_category() 0 21 1
A it_get_a_language_from_the_translations() 19 19 1
A it__try_to_get_a_language_from_the_translations() 20 20 1
A it_get_the_translations_from_never_cached_config_file() 0 15 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 spec\Fenos\Notifynder\Translator;
4
5
use Fenos\Notifynder\Exceptions\NotificationLanguageNotFoundException;
6
use Fenos\Notifynder\Exceptions\NotificationTranslationNotFoundException;
7
use Fenos\Notifynder\Translator\Compiler;
8
use Illuminate\Contracts\Config\Repository;
9
use PhpSpec\ObjectBehavior;
10
11
class TranslatorManagerSpec extends ObjectBehavior
12
{
13
    public function let(Compiler $compiler, Repository $config)
14
    {
15
        $this->beConstructedWith($compiler, $config);
16
    }
17
18
    public function it_is_initializable()
19
    {
20
        $this->shouldHaveType('Fenos\Notifynder\Translator\TranslatorManager');
21
    }
22
23
    /** @test */
24
    public function it_translate_the_given_category_in_the_given_language(Compiler $compiler, Repository $config)
25
    {
26
        $filePath = 'cached/file';
27
        $categoryToTranslate = 'categoryName';
28
        $translations = [
29
            'it' => [
30
                $categoryToTranslate => 'translation',
31
            ],
32
        ];
33
34
        $compiler->getFilePath()->shouldBeCalled()
35
            ->willReturn($filePath);
36
37
        $config->get('notifynder.translations')->shouldBeCalled()
38
            ->willReturn($translations);
39
40
        $compiler->cacheFile($translations)->shouldBeCalled();
41
42
        $this->translate('it', $categoryToTranslate)
43
             ->shouldReturn($translations['it'][$categoryToTranslate]);
44
    }
45
46
    /** @test */
47
    public function it__try_to_translate_the_given_category(Compiler $compiler, Repository $config)
48
    {
49
        $filePath = 'cached/file';
50
        $categoryToTranslate = 'categoryName';
51
        $translations = [
52
            'it' => [
53
                $categoryToTranslate => 'translation',
54
            ],
55
        ];
56
57
        $compiler->getFilePath()->shouldBeCalled()
58
            ->willReturn($filePath);
59
60
        $config->get('notifynder.translations')->shouldBeCalled()
61
            ->willReturn($translations);
62
63
        $compiler->cacheFile($translations)->shouldBeCalled();
64
65
        $this->shouldThrow(NotificationTranslationNotFoundException::class)
66
             ->during('translate', ['it', 'not existing']);
67
    }
68
69
    /** @test */
70 View Code Duplication
    public function it_get_a_language_from_the_translations(Compiler $compiler, Repository $config)
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...
71
    {
72
        $filePath = 'cached/file';
73
        $translations = [
74
            'it' => [
75
                'categoryName' => 'translation',
76
            ],
77
        ];
78
79
        $compiler->getFilePath()->shouldBeCalled()
80
            ->willReturn($filePath);
81
82
        $config->get('notifynder.translations')->shouldBeCalled()
83
            ->willReturn($translations);
84
85
        $compiler->cacheFile($translations)->shouldBeCalled();
86
87
        $this->getLanguage('it')->shouldReturn($translations['it']);
88
    }
89
90
    /** @test */
91 View Code Duplication
    public function it__try_to_get_a_language_from_the_translations(Compiler $compiler, Repository $config)
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...
92
    {
93
        $filePath = 'cached/file';
94
        $translations = [
95
            'it' => [
96
                'categoryName' => 'translation',
97
            ],
98
        ];
99
100
        $compiler->getFilePath()->shouldBeCalled()
101
            ->willReturn($filePath);
102
103
        $config->get('notifynder.translations')->shouldBeCalled()
104
            ->willReturn($translations);
105
106
        $compiler->cacheFile($translations)->shouldBeCalled();
107
108
        $this->shouldThrow(NotificationLanguageNotFoundException::class)
109
             ->during('getLanguage', ['en']);
110
    }
111
112
    /** @test */
113
    public function it_get_the_translations_from_never_cached_config_file(Compiler $compiler, Repository $config)
114
    {
115
        $filePath = 'cached/file';
116
        $translations = [];
117
118
        $compiler->getFilePath()->shouldBeCalled()
119
                 ->willReturn($filePath);
120
121
        $config->get('notifynder.translations')->shouldBeCalled()
122
                ->willReturn($translations);
123
124
        $compiler->cacheFile($translations)->shouldBeCalled();
125
126
        $this->getTranslations()->shouldReturn($translations);
127
    }
128
}
129