1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sleepness\UberTranslationAdminBundle\Tests\Frontend; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Test MemcachedMessagesFrontendCatalogue methods and cases |
9
|
|
|
* |
10
|
|
|
* @author Viktor Novikov <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class MemcachedMessagesFrontendCatalogueTest extends WebTestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Sleepness\UberTranslationAdminBundle\Frontend\MemcachedMessagesFrontendCatalogue; |
16
|
|
|
*/ |
17
|
|
|
private $messagesFrontend; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Sleepness\UberTranslationBundle\Storage\UberMemcached; |
21
|
|
|
*/ |
22
|
|
|
private $uberMemcached; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Test building catalogue by locale |
26
|
|
|
*/ |
27
|
|
|
public function testBuildByLocale() |
28
|
|
|
{ |
29
|
|
|
$preparedTranslations = $this->messagesFrontend->buildByLocale('en_US'); |
30
|
|
|
|
31
|
|
|
$this->assertEquals('key.hello', $preparedTranslations[0]['keyYml']); |
32
|
|
|
$this->assertEquals('value.Hello', $preparedTranslations[0]['messageProps']['messageText']); |
33
|
|
|
$this->assertEquals('en_US', $preparedTranslations[0]['messageProps']['locale']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test building catalogue by domain name |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function testBuildByDomain() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$preparedTranslations = $this->messagesFrontend->buildByDomain('messages'); |
42
|
|
|
$this->assertEquals('key.hello', $preparedTranslations[0]['keyYml']); |
43
|
|
|
$this->assertEquals('value.Hello', $preparedTranslations[0]['messageProps']['messageText']); |
44
|
|
|
$this->assertArrayNotHasKey('validators', $preparedTranslations); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test building catalogue by translation key |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function testBuildByKey() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$preparedTranslations = $this->messagesFrontend->buildByKey('key.not.blank'); |
53
|
|
|
$this->assertEquals('key.not.blank', $preparedTranslations[0]['keyYml']); |
54
|
|
|
$this->assertEquals('value.NotBlank', $preparedTranslations[0]['messageProps']['messageText']); |
55
|
|
|
$this->assertArrayNotHasKey('messages', $preparedTranslations); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test building catalogue by given text |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testBuildByText() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$preparedTranslations = $this->messagesFrontend->buildByText('value.MaxLength'); |
64
|
|
|
$this->assertEquals('key.max.length', $preparedTranslations[0]['keyYml']); |
65
|
|
|
$this->assertEquals('value.MaxLength', $preparedTranslations[0]['messageProps']['messageText']); |
66
|
|
|
$this->assertArrayNotHasKey('messages', $preparedTranslations); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Test catalogue to get all messages from memcached |
71
|
|
|
*/ |
72
|
|
|
public function testGetAll() |
73
|
|
|
{ |
74
|
|
|
$preparedTranslations = $this->messagesFrontend->getAll(); |
75
|
|
|
$this->assertEquals('key.max.length', $preparedTranslations[3]['keyYml']); |
76
|
|
|
$this->assertEquals('value.MaxLength', $preparedTranslations[3]['messageProps']['messageText']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test replace translation |
81
|
|
|
*/ |
82
|
|
|
public function testReplace() |
83
|
|
|
{ |
84
|
|
|
$this->messagesFrontend->replace('key.foo', 'en_US', 'messages', 'en_XX', 'newmessages', 'new.value.Foo'); |
85
|
|
|
$definedTransaltions = $this->uberMemcached->getItem('en_US'); |
86
|
|
|
$newTransaltions = $this->uberMemcached->getItem('en_XX'); |
87
|
|
|
$this->assertEquals('new.value.Foo', $newTransaltions['newmessages']['key.foo']); |
88
|
|
|
$this->assertArrayNotHasKey('key.foo', $definedTransaltions['messages']); |
89
|
|
|
$this->assertArrayNotHasKey('newmessages', $definedTransaltions); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set up fixtures for testing |
94
|
|
|
*/ |
95
|
|
|
public function setUp() |
96
|
|
|
{ |
97
|
|
|
static::bootKernel(array()); |
98
|
|
|
$container = static::$kernel->getContainer(); |
99
|
|
|
$this->uberMemcached = $container->get('uber.memcached'); |
100
|
|
|
$this->messagesFrontend = $container->get('memcached.messages.frontend.catalogue'); |
101
|
|
|
$this->uberMemcached->addItem('en_US', $this->getMessagesArray()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Tear down fixtures after testing |
106
|
|
|
*/ |
107
|
|
|
public function tearDown() |
108
|
|
|
{ |
109
|
|
|
$this->uberMemcached->deleteItem('en_US'); |
110
|
|
|
$this->uberMemcached->deleteItem('en_XX'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get messages for testing |
115
|
|
|
* |
116
|
|
|
* @return array - messages |
117
|
|
|
*/ |
118
|
|
|
private function getMessagesArray() |
119
|
|
|
{ |
120
|
|
|
return array( |
121
|
|
|
'messages' => array( |
122
|
|
|
'key.hello' => 'value.Hello', |
123
|
|
|
'key.foo' => 'value.Foo', |
124
|
|
|
), |
125
|
|
|
'validators' => array( |
126
|
|
|
'key.not.blank' => 'value.NotBlank', |
127
|
|
|
'key.max.length' => 'value.MaxLength', |
128
|
|
|
), |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.