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\Factory; |
13
|
|
|
|
14
|
|
|
use Lug\Component\Resource\Factory\Factory; |
15
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
16
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
17
|
|
|
use Lug\Component\Translation\Factory\TranslatableFactory; |
18
|
|
|
use Lug\Component\Translation\Model\TranslatableInterface; |
19
|
|
|
use Lug\Component\Translation\Model\TranslatableTrait; |
20
|
|
|
use Lug\Component\Translation\Model\TranslationInterface; |
21
|
|
|
use Lug\Component\Translation\Model\TranslationTrait; |
22
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
23
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class TranslatableFactoryTest extends \PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var TranslatableFactory |
32
|
|
|
*/ |
33
|
|
|
private $factory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
37
|
|
|
*/ |
38
|
|
|
private $resource; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface |
42
|
|
|
*/ |
43
|
|
|
private $propertyAccessor; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
47
|
|
|
*/ |
48
|
|
|
private $localeContext; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function setUp() |
54
|
|
|
{ |
55
|
|
|
$this->resource = $this->createResourceMock(); |
56
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
57
|
|
|
$this->localeContext = $this->createLocaleContextMock(); |
58
|
|
|
|
59
|
|
|
$this->factory = new TranslatableFactory($this->resource, $this->propertyAccessor, $this->localeContext); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testInheritance() |
63
|
|
|
{ |
64
|
|
|
$this->assertInstanceOf(Factory::class, $this->factory); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCreateWithOptions() |
68
|
|
|
{ |
69
|
|
|
$this->resource |
70
|
|
|
->expects($this->once()) |
71
|
|
|
->method('getModel') |
72
|
|
|
->will($this->returnValue($translatableClass = TranslatableTest::class)); |
73
|
|
|
|
74
|
|
|
$this->resource |
75
|
|
|
->expects($this->once()) |
76
|
|
|
->method('getTranslation') |
77
|
|
|
->will($this->returnValue($translationResource = $this->createResourceMock())); |
78
|
|
|
|
79
|
|
|
$translationResource |
80
|
|
|
->expects($this->once()) |
81
|
|
|
->method('getModel') |
82
|
|
|
->will($this->returnValue($translationClass = TranslationTest::class)); |
83
|
|
|
|
84
|
|
|
$this->localeContext |
85
|
|
|
->expects($this->once()) |
86
|
|
|
->method('getLocales') |
87
|
|
|
->will($this->returnValue($locales = ['fr', 'es'])); |
88
|
|
|
|
89
|
|
|
$this->localeContext |
90
|
|
|
->expects($this->once()) |
91
|
|
|
->method('getFallbackLocale') |
92
|
|
|
->will($this->returnValue($fallbackLocale = 'en')); |
93
|
|
|
|
94
|
|
|
$translatable = $this->factory->create(['name' => $name = 'foo']); |
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf($translatableClass, $translatable); |
97
|
|
|
$this->assertSame($name, $translatable->getName()); |
98
|
|
|
$this->assertSame($locales, $translatable->getLocales()); |
99
|
|
|
$this->assertSame($fallbackLocale, $translatable->getFallbackLocale()); |
100
|
|
|
$this->assertSame($translationClass, $translatable->getTranslationClass()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testCreateWithoutOptions() |
104
|
|
|
{ |
105
|
|
|
$this->resource |
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('getModel') |
108
|
|
|
->will($this->returnValue($translatableClass = TranslatableTest::class)); |
109
|
|
|
|
110
|
|
|
$this->resource |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('getTranslation') |
113
|
|
|
->will($this->returnValue($translationResource = $this->createResourceMock())); |
114
|
|
|
|
115
|
|
|
$translationResource |
116
|
|
|
->expects($this->once()) |
117
|
|
|
->method('getModel') |
118
|
|
|
->will($this->returnValue($translationClass = TranslationTest::class)); |
119
|
|
|
|
120
|
|
|
$this->localeContext |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('getLocales') |
123
|
|
|
->will($this->returnValue($locales = ['fr', 'es'])); |
124
|
|
|
|
125
|
|
|
$this->localeContext |
126
|
|
|
->expects($this->once()) |
127
|
|
|
->method('getFallbackLocale') |
128
|
|
|
->will($this->returnValue($fallbackLocale = 'en')); |
129
|
|
|
|
130
|
|
|
$translatable = $this->factory->create(); |
131
|
|
|
|
132
|
|
|
$this->assertInstanceOf($translatableClass, $translatable); |
133
|
|
|
$this->assertSame($locales, $translatable->getLocales()); |
134
|
|
|
$this->assertSame($fallbackLocale, $translatable->getFallbackLocale()); |
135
|
|
|
$this->assertSame($translationClass, $translatable->getTranslationClass()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
140
|
|
|
*/ |
141
|
|
|
private function createResourceMock() |
142
|
|
|
{ |
143
|
|
|
return $this->createMock(ResourceInterface::class); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
148
|
|
|
*/ |
149
|
|
|
private function createLocaleContextMock() |
150
|
|
|
{ |
151
|
|
|
return $this->createMock(LocaleContextInterface::class); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @author GeLo <[email protected]> |
157
|
|
|
*/ |
158
|
|
|
class TranslatableTest implements TranslatableInterface |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
use TranslatableTrait; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @var string |
164
|
|
|
*/ |
165
|
|
|
private $name; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getName() |
171
|
|
|
{ |
172
|
|
|
return $this->name; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $name |
177
|
|
|
*/ |
178
|
|
|
public function setName($name) |
179
|
|
|
{ |
180
|
|
|
$this->name = $name; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @author GeLo <[email protected]> |
186
|
|
|
*/ |
187
|
|
|
class TranslationTest implements TranslationInterface |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
use TranslationTrait; |
190
|
|
|
} |
191
|
|
|
|
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.