-
Notifications
You must be signed in to change notification settings - Fork 283
/
TranslatableTest.php
198 lines (163 loc) · 5.53 KB
/
TranslatableTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Tests\Knp\DoctrineBehaviors\ORM;
use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;
use Doctrine\Common\EventManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
require_once 'EntityManagerProvider.php';
class TranslatableTest extends \PHPUnit_Framework_TestCase
{
use EntityManagerProvider;
protected function getUsedEntityFixtures()
{
return [
'BehaviorFixtures\\ORM\\TranslatableEntity',
'BehaviorFixtures\\ORM\\TranslatableEntityTranslation'
];
}
protected function getEventManager()
{
$em = new EventManager;
$em->addEventSubscriber(new \Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber(
new ClassAnalyzer(),
false,
function()
{
return 'en';
},
'Knp\DoctrineBehaviors\Model\Translatable\Translatable',
'Knp\DoctrineBehaviors\Model\Translatable\Translation',
'LAZY',
'LAZY'
));
return $em;
}
/**
* @test
*/
public function should_persist_translations()
{
$em = $this->getEntityManager();
$entity = new \BehaviorFixtures\ORM\TranslatableEntity();
$entity->translate('fr')->setTitle('fabuleux');
$entity->translate('en')->setTitle('awesome');
$entity->translate('ru')->setTitle('удивительный');
$entity->mergeNewTranslations();
$em->persist($entity);
$em->flush();
$id = $entity->getId();
$em->clear();
$entity = $em
->getRepository('BehaviorFixtures\ORM\TranslatableEntity')
->find($id)
;
$this->assertEquals(
'fabuleux',
$entity->translate('fr')->getTitle()
);
$this->assertEquals(
'awesome',
$entity->translate('en')->getTitle()
);
$this->assertEquals(
'удивительный',
$entity->translate('ru')->getTitle()
);
}
/**
* @test
*/
public function translate_method_should_always_return_translation_object()
{
$em = $this->getEntityManager();
$entity = new \BehaviorFixtures\ORM\TranslatableEntity();
$this->assertInstanceOf(
'BehaviorFixtures\ORM\TranslatableEntityTranslation',
$entity->translate('fr')
);
}
/**
* @test
*/
public function subscriber_should_configure_entity_with_current_locale()
{
$em = $this->getEntityManager();
$entity = new \BehaviorFixtures\ORM\TranslatableEntity();
$entity->setTitle('test'); // magic method
$entity->mergeNewTranslations();
$em->persist($entity);
$em->flush();
$id = $entity->getId();
$em->clear();
$entity = $em->getRepository('BehaviorFixtures\ORM\TranslatableEntity')->find($id);
$this->assertEquals('en', $entity->getCurrentLocale());
$this->assertEquals('test', $entity->getTitle());
$this->assertEquals('test', $entity->translate($entity->getCurrentLocale())->getTitle());
}
/**
* @test
*/
public function should_have_ontToMany_relation()
{
$em = $this->getEntityManager();
$meta = $em->getClassMetadata('BehaviorFixtures\ORM\TranslatableEntityTranslation');
$this->assertEquals(
'BehaviorFixtures\ORM\TranslatableEntity',
$meta->getAssociationTargetClass('translatable')
);
$meta = $em->getClassMetadata('BehaviorFixtures\ORM\TranslatableEntity');
$this->assertEquals(
'BehaviorFixtures\ORM\TranslatableEntityTranslation',
$meta->getAssociationTargetClass('translations')
);
$this->assertTrue($meta->isAssociationInverseSide('translations'));
$this->assertEquals(
ClassMetadataInfo::ONE_TO_MANY,
$meta->getAssociationMapping('translations')['type']
);
}
/**
* @test
*/
public function should_create_only_one_time_the_same_translation()
{
$em = $this->getEntityManager();
$entity = new \BehaviorFixtures\ORM\TranslatableEntity();
$translation = $entity->translate('fr');
$translation->setTitle('fabuleux');
$entity->translate('fr')->setTitle('fabuleux2');
$entity->translate('fr')->setTitle('fabuleux3');
$this->assertEquals('fabuleux3', $entity->translate('fr')->getTitle());
$this->assertEquals(spl_object_hash($entity->translate('fr')), spl_object_hash($translation));
}
/**
* @test
*/
public function should_remove_translation()
{
$em = $this->getEntityManager();
$entity = new \BehaviorFixtures\ORM\TranslatableEntity();
$entity->translate('en')->setTitle('Hello');
$entity->translate('nl')->setTitle('Hallo');
$entity->mergeNewTranslations();
$em->persist($entity);
$em->flush();
$nlTranslation = $entity->translate('nl');
$entity->removeTranslation($nlTranslation);
$em->flush();
$em->refresh($entity);
$this->assertNotEquals('Hallo', $entity->translate('nl')->getTitle());
}
/**
* @test
* @expectedException Exception
*/
public function should_notice_deprecation()
{
set_error_handler(function() {throw new \Exception; }, E_USER_DEPRECATED);
new \Knp\DoctrineBehaviors\ORM\Translatable\TranslatableListener;
}
public function tearDown()
{
restore_error_handler();
}
}