|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.