Completed
Push — master ( e26eba...178ca9 )
by Eric
37:26 queued 30:42
created

TranslatableTest::createFactoryMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Translation\Tests\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Lug\Component\Resource\Factory\FactoryInterface;
16
use Lug\Component\Translation\Model\TranslatableInterface;
17
use Lug\Component\Translation\Model\TranslatableTrait;
18
use Lug\Component\Translation\Model\TranslationInterface;
19
use Lug\Component\Translation\Model\TranslationTrait;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class TranslatableTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var ConcreteTranslatable
28
     */
29
    private $translatable;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function setUp()
35
    {
36
        $this->translatable = new ConcreteTranslatable();
37
    }
38
39
    public function testInheritance()
40
    {
41
        $this->assertInstanceOf(TranslatableInterface::class, $this->translatable);
42
        $this->assertTrue(in_array(TranslatableTrait::class, class_uses($this->translatable), true));
43
    }
44
45
    public function testDefaultState()
46
    {
47
        $this->assertEmpty($this->translatable->getLocales());
48
        $this->assertFalse($this->translatable->hasFallbackLocale());
49
        $this->assertNull($this->translatable->getFallbackLocale());
50
        $this->assertNull($this->translatable->getTranslationFactory());
51
        $this->assertInstanceOf(ArrayCollection::class, $this->translatable->getTranslations());
52
        $this->assertTrue($this->translatable->getTranslations()->isEmpty());
53
    }
54
55
    public function testLocales()
56
    {
57
        $this->translatable->setLocales($locales = ['fr_FR']);
58
59
        $this->assertSame($locales, $this->translatable->getLocales());
60
        $this->assertNull($this->translatable->getLocale());
61
    }
62
63
    public function testFallbackLocale()
64
    {
65
        $this->translatable->setFallbackLocale($fallbackLocale = 'fr_FR');
66
67
        $this->assertTrue($this->translatable->hasFallbackLocale());
68
        $this->assertSame($fallbackLocale, $this->translatable->getFallbackLocale());
69
    }
70
71
    public function testTranslationFactory()
72
    {
73
        $this->translatable->setTranslationFactory($translationFactory = $this->createFactoryMock());
74
75
        $this->assertSame($translationFactory, $this->translatable->getTranslationFactory());
76
    }
77
78
    public function testAddTranslation()
79
    {
80
        $translation = $this->createTranslationMock();
81
        $translation
82
            ->expects($this->any())
83
            ->method('getLocale')
84
            ->will($this->returnValue($locale = 'fr_FR'));
85
86
        $translation
87
            ->expects($this->once())
88
            ->method('setTranslatable')
89
            ->with($this->identicalTo($this->translatable));
90
91
        $this->translatable->addTranslation($translation);
92
93
        $this->assertSame([$locale => $translation], $this->translatable->getTranslations()->toArray());
94
    }
95
96
    public function testAddExistentTranslation()
97
    {
98
        $translation = $this->createTranslationMock();
99
        $translation
100
            ->expects($this->any())
101
            ->method('getLocale')
102
            ->will($this->returnValue($locale = 'fr_FR'));
103
104
        $translation
105
            ->expects($this->once())
106
            ->method('setTranslatable')
107
            ->with($this->identicalTo($this->translatable));
108
109
        $duplicatedTranslation = $this->createTranslationMock();
110
        $duplicatedTranslation
111
            ->expects($this->any())
112
            ->method('getLocale')
113
            ->will($this->returnValue($locale));
114
115
        $duplicatedTranslation
116
            ->expects($this->never())
117
            ->method('setTranslatable');
118
119
        $this->translatable->addTranslation($translation);
120
        $this->translatable->addTranslation($duplicatedTranslation);
121
122
        $this->assertSame([$locale => $translation], $this->translatable->getTranslations()->toArray());
123
    }
124
125
    public function testRemoveTranslation()
126
    {
127
        $translation = $this->createTranslationMock();
128
        $translation
129
            ->expects($this->any())
130
            ->method('getLocale')
131
            ->will($this->returnValue($locale = 'fr_FR'));
132
133
        $translation
134
            ->expects($this->exactly(2))
135
            ->method('setTranslatable')
136
            ->withConsecutive(
137
                [$this->identicalTo($this->translatable)],
138
                [$this->isNull()]
139
            );
140
141
        $this->translatable->addTranslation($translation);
142
        $this->translatable->removeTranslation($translation);
143
144
        $this->assertTrue($this->translatable->getTranslations()->isEmpty());
145
    }
146
147
    public function testRemoveNonexistentTranslation()
148
    {
149
        $translation = $this->createTranslationMock();
150
        $translation
151
            ->expects($this->any())
152
            ->method('getLocale')
153
            ->will($this->returnValue($locale = 'fr_FR'));
154
155
        $translation
156
            ->expects($this->never())
157
            ->method('setTranslatable');
158
159
        $this->translatable->removeTranslation($translation);
160
    }
161
162 View Code Duplication
    public function testTranslationWithSingleLocale()
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...
163
    {
164
        $this->translatable->setLocales([$locale = 'fr_FR']);
165
166
        $translation = $this->createTranslationMock();
167
        $translation
168
            ->expects($this->any())
169
            ->method('getLocale')
170
            ->will($this->returnValue($locale));
171
172
        $this->translatable->addTranslation($translation);
173
174
        $this->assertSame($translation, $this->translatable->getTranslation());
175
    }
176
177 View Code Duplication
    public function testTranslationWithMultipleLocales()
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...
178
    {
179
        $this->translatable->setLocales(['fr_FR', $locale = 'en_EN']);
180
181
        $translation = $this->createTranslationMock();
182
        $translation
183
            ->expects($this->any())
184
            ->method('getLocale')
185
            ->will($this->returnValue($locale));
186
187
        $this->translatable->addTranslation($translation);
188
189
        $this->assertSame($translation, $this->translatable->getTranslation());
190
    }
191
192 View Code Duplication
    public function testTranslationWithFallbackLocale()
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...
193
    {
194
        $this->translatable->setLocales(['en_EN']);
195
        $this->translatable->setFallbackLocale($fallbackLocale = 'fr_FR');
196
197
        $translation = $this->createTranslationMock();
198
        $translation
199
            ->expects($this->any())
200
            ->method('getLocale')
201
            ->will($this->returnValue($fallbackLocale));
202
203
        $this->translatable->addTranslation($translation);
204
205
        $this->assertSame($translation, $this->translatable->getTranslation());
206
    }
207
208
    public function testTranslationCreated()
209
    {
210
        $this->translatable->setLocales([$locale = 'fr_FR']);
211
        $this->translatable->setFallbackLocale('en_EN');
212
        $this->translatable->setTranslationFactory($translationFactory = $this->createFactoryMock());
213
214
        $translationFactory
215
            ->expects($this->once())
216
            ->method('create')
217
            ->with($this->identicalTo(['locale' => $locale]))
218
            ->will($this->returnValue($translation = $this->createTranslationMock()));
219
220
        $this->assertSame($translation, $this->translatable->getTranslation(true));
221
    }
222
223
    public function testLocale()
224
    {
225
        $this->translatable->setLocales([$locale = 'fr_FR']);
226
        $this->translatable->setFallbackLocale('en_EN');
227
        $this->translatable->setTranslationFactory($translationFactory = $this->createFactoryMock());
228
229
        $translationFactory
230
            ->expects($this->once())
231
            ->method('create')
232
            ->with($this->identicalTo(['locale' => $locale]))
233
            ->will($this->returnValue($translation = $this->createTranslationMock()));
234
235
        $translation
236
            ->expects($this->exactly(3))
237
            ->method('getLocale')
238
            ->will($this->returnValue($locale));
239
240
        $this->translatable->getTranslation(true);
241
242
        $this->assertSame($locale, $this->translatable->getLocale());
243
    }
244
245
    public function testLocaleWithoutTranslation()
246
    {
247
        $this->translatable->setLocales(['fr_FR']);
248
        $this->translatable->setFallbackLocale('en_EN');
249
250
        $this->assertNull($this->translatable->getLocale());
251
    }
252
253
    /**
254
     * @expectedException \Lug\Component\Translation\Exception\LocaleNotFoundException
255
     * @expectedExceptionMessage The locale could not be found.
256
     */
257
    public function testTranslationWithMissingLocales()
258
    {
259
        $this->translatable->getTranslation();
260
    }
261
262
    /**
263
     * @expectedException \Lug\Component\Translation\Exception\TranslationNotFoundException
264
     * @expectedExceptionMessage The translation could not be found.
265
     */
266
    public function testTranslationWithMissingFallbackLocale()
267
    {
268
        $this->translatable->setLocales(['fr_FR']);
269
270
        $this->translatable->getTranslation();
271
    }
272
273
    /**
274
     * @expectedException \Lug\Component\Translation\Exception\TranslationNotFoundException
275
     * @expectedExceptionMessage The translation could not be found.
276
     */
277
    public function testMissingTranslation()
278
    {
279
        $this->translatable->setLocales(['fr_FR', 'en_EN']);
280
        $this->translatable->setFallbackLocale('en');
281
282
        $this->translatable->getTranslation();
283
    }
284
285
    /**
286
     * @return \PHPUnit_Framework_MockObject_MockObject|TranslationInterface
287
     */
288
    private function createTranslationMock()
289
    {
290
        return $this->createMock(TranslationInterface::class);
291
    }
292
293
    /**
294
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
295
     */
296
    private function createFactoryMock()
297
    {
298
        return $this->createMock(FactoryInterface::class);
299
    }
300
}
301
302
/**
303
 * @author GeLo <[email protected]>
304
 */
305
class ConcreteTranslatable implements TranslatableInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
306
{
307
    use TranslatableTrait;
308
309
    public function __construct()
310
    {
311
        $this->initTranslatable();
312
    }
313
}
314
315
/**
316
 * @author GeLo <[email protected]>
317
 */
318
class ConcreteTranslatableTranslation implements TranslationInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
319
{
320
    use TranslationTrait;
321
}
322