-
Notifications
You must be signed in to change notification settings - Fork 9
/
TranslationHistorySubscriberTest.php
176 lines (156 loc) · 6.23 KB
/
TranslationHistorySubscriberTest.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
<?php
/*
* This file is part of the AsmTranslationLoaderBundle package.
*
* (c) Marc Aschmann <maschmann@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm\TranslationLoaderBundle\Tests\EventListener;
use Asm\TranslationLoaderBundle\Event\TranslationEvent;
use Asm\TranslationLoaderBundle\EventListener\TranslationHistorySubscriber;
use Asm\TranslationLoaderBundle\Model\Translation;
use Asm\TranslationLoaderBundle\Model\TranslationHistory;
use Asm\TranslationLoaderBundle\Model\TranslationHistoryManagerInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class TranslationHistorySubscriberTest extends \PHPUnit_Framework_TestCase
{
/**
* @var TranslationHistoryManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $translationHistoryManager;
protected function setUp()
{
$this->translationHistoryManager = $this->createTranslationHistoryManager();
}
/**
* @dataProvider updateHistoryProvider
*/
public function testUpdateHistoryProvider($securityContext, $eventName, $expectedUsername, $expectedAction)
{
$dateBefore = new \DateTime();
$translation = $this->createRandomDummyTranslation();
$subscriber = new TranslationHistorySubscriber($this->translationHistoryManager, $securityContext);
$this
->translationHistoryManager
->expects($this->once())
->method('updateTranslationHistory')
->with($this->isInstanceOf('Asm\TranslationLoaderBundle\Model\TranslationHistoryInterface'), false);
$event = new TranslationEvent($translation);
if (method_exists($eventName, 'setName')) {
$event->setName($eventName);
}
$translationHistory = $subscriber->updateHistory($event, $eventName);
$this->assertGreaterThanOrEqual($dateBefore, $translationHistory->getDateOfChange());
$this->assertEquals($translation->getMessageDomain(), $translationHistory->getMessageDomain());
$this->assertEquals($translation->getTransKey(), $translationHistory->getTransKey());
$this->assertEquals($translation->getTransLocale(), $translationHistory->getTransLocale());
$this->assertEquals($translation->getTranslation(), $translationHistory->getTranslation());
$this->assertEquals($expectedUsername, $translationHistory->getUserName());
$this->assertEquals($expectedAction, $translationHistory->getUserAction());
}
public function updateHistoryProvider()
{
return array(
'persist-anonymous-user' => array(
$this->createAnonymousSecurityContext(),
TranslationEvent::POST_PERSIST,
'anonymous',
'persist',
),
'update-anonymous-user' => array(
$this->createAnonymousSecurityContext(),
TranslationEvent::POST_UPDATE,
'anonymous',
'update',
),
'remove-anonymous-user' => array(
$this->createAnonymousSecurityContext(),
TranslationEvent::POST_REMOVE,
'anonymous',
'remove',
),
'persist-authenticated-user' => array(
$this->createAuthenticatedSecurityContext(),
TranslationEvent::POST_PERSIST,
'the username',
'persist',
),
'update-authenticated-user' => array(
$this->createAuthenticatedSecurityContext(),
TranslationEvent::POST_UPDATE,
'the username',
'update',
),
'remove-authenticated-user' => array(
$this->createAuthenticatedSecurityContext(),
TranslationEvent::POST_REMOVE,
'the username',
'remove',
),
);
}
/**
* @return TranslationHistoryManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private function createTranslationHistoryManager()
{
$manager = $this->getMockBuilder('Asm\TranslationLoaderBundle\Model\TranslationHistoryManagerInterface')->getMock();
$manager
->expects($this->any())
->method('createTranslationHistory')
->will($this->returnValue(new DummyTranslationHistory()));
return $manager;
}
/**
* @return SecurityContextInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private function createAnonymousSecurityContext()
{
return $this->mockTokenStorage();
}
/**
* @return SecurityContextInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private function createAuthenticatedSecurityContext()
{
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
$token
->expects($this->any())
->method('getUsername')
->will($this->returnValue('the username'));
$tokenStorage = $this->mockTokenStorage();
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($token));
return $tokenStorage;
}
private function createRandomDummyTranslation()
{
$translation = new DummyTranslation();
$translation->setTransKey(md5(uniqid()));
$translation->setTransLocale(md5(uniqid()));
$translation->setMessageDomain(md5(uniqid()));
$translation->setTranslation(md5(uniqid()));
return $translation;
}
private function mockTokenStorage()
{
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
} else {
return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')->getMock();
}
}
}
class DummyTranslation extends Translation
{
}
class DummyTranslationHistory extends TranslationHistory
{
}