1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2018 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\CoreBundle\Test\Service; |
||
13 | |||
14 | use Mautic\CoreBundle\Model\NotificationModel; |
||
15 | use Mautic\CoreBundle\Service\FlashBag; |
||
16 | use PHPUnit\Framework\MockObject\MockObject; |
||
17 | use PHPUnit\Framework\TestCase; |
||
18 | use Symfony\Component\HttpFoundation\Request; |
||
19 | use Symfony\Component\HttpFoundation\RequestStack; |
||
20 | use Symfony\Component\HttpFoundation\Session\Flash\FlashBag as SymfonyFlashBag; |
||
21 | use Symfony\Component\HttpFoundation\Session\Session; |
||
22 | use Symfony\Component\Translation\TranslatorInterface; |
||
23 | |||
24 | class FlashBagTest extends TestCase |
||
25 | { |
||
26 | /** |
||
27 | * @var MockObject|SymfonyFlashBag |
||
28 | */ |
||
29 | private $symfonyFlashBag; |
||
30 | |||
31 | /** |
||
32 | * @var MockObject|Session |
||
33 | */ |
||
34 | private $session; |
||
35 | |||
36 | /** |
||
37 | * @var MockObject|TranslatorInterface |
||
38 | */ |
||
39 | private $translator; |
||
40 | |||
41 | /** |
||
42 | * @var MockObject|RequestStack |
||
43 | */ |
||
44 | private $requestStack; |
||
45 | |||
46 | /** |
||
47 | * @var NotificationModel|MockObject |
||
48 | */ |
||
49 | private $notificationModel; |
||
50 | |||
51 | /** |
||
52 | * @var FlashBag |
||
53 | */ |
||
54 | private $flashBag; |
||
55 | |||
56 | protected function setUp(): void |
||
57 | { |
||
58 | $this->symfonyFlashBag = $this->createMock(SymfonyFlashBag::class); |
||
59 | |||
60 | $this->session = $this->createMock(Session::class); |
||
61 | $this->session |
||
62 | ->expects($this->once()) |
||
63 | ->method('getFlashBag') |
||
64 | ->willReturn($this->symfonyFlashBag); |
||
65 | $this->translator = $this->createMock(TranslatorInterface::class); |
||
66 | $this->requestStack = $this->createMock(RequestStack::class); |
||
67 | $this->notificationModel = $this->createMock(NotificationModel::class); |
||
68 | $this->flashBag = new FlashBag($this->session, $this->translator, $this->requestStack, $this->notificationModel); |
||
69 | |||
70 | parent::setUp(); |
||
71 | } |
||
72 | |||
73 | public function testAddWithoutVars(): void |
||
74 | { |
||
75 | $message = 'message'; |
||
76 | $messageVars = []; |
||
77 | $level = FlashBag::LEVEL_NOTICE; |
||
78 | $domain = false; |
||
79 | $addNotification = false; |
||
80 | |||
81 | $this->symfonyFlashBag |
||
82 | ->expects($this->once()) |
||
83 | ->method('add') |
||
84 | ->with($level, $message); |
||
85 | |||
86 | $this->flashBag->add($message, $messageVars, $level, $domain, $addNotification); |
||
87 | } |
||
88 | |||
89 | public function testAddWithChoices(): void |
||
90 | { |
||
91 | $message = 'message'; |
||
92 | $messageVars['pluralCount'] = 2; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
93 | $translatedMessage = 'translatedMessage'; |
||
94 | $level = FlashBag::LEVEL_NOTICE; |
||
95 | $domain = 'flashes'; |
||
96 | $addNotification = false; |
||
97 | |||
98 | $this->symfonyFlashBag |
||
99 | ->expects($this->once()) |
||
100 | ->method('add') |
||
101 | ->with($level, $translatedMessage); |
||
102 | |||
103 | $this->session |
||
104 | ->expects($this->once()) |
||
105 | ->method('getFlashBag') |
||
106 | ->willReturn($this->symfonyFlashBag); |
||
107 | |||
108 | $this->translator |
||
109 | ->expects($this->once()) |
||
110 | ->method('transChoice') |
||
111 | ->with($message, $messageVars['pluralCount'], $messageVars, $domain) |
||
112 | ->willReturn($translatedMessage); |
||
113 | |||
114 | $this->flashBag->add($message, $messageVars, $level, $domain, $addNotification); |
||
115 | } |
||
116 | |||
117 | public function testAddWithTranslation(): void |
||
118 | { |
||
119 | $message = 'message'; |
||
120 | $messageVars = []; |
||
121 | $translatedMessage = 'translatedMessage'; |
||
122 | $level = FlashBag::LEVEL_NOTICE; |
||
123 | $domain = 'flashes'; |
||
124 | $addNotification = false; |
||
125 | |||
126 | $this->symfonyFlashBag |
||
127 | ->expects($this->once()) |
||
128 | ->method('add') |
||
129 | ->with($level, $translatedMessage); |
||
130 | |||
131 | $this->session |
||
132 | ->expects($this->once()) |
||
133 | ->method('getFlashBag') |
||
134 | ->willReturn($this->symfonyFlashBag); |
||
135 | |||
136 | $this->translator |
||
137 | ->expects($this->once()) |
||
138 | ->method('trans') |
||
139 | ->with($message, $messageVars, $domain) |
||
140 | ->willReturn($translatedMessage); |
||
141 | |||
142 | $this->flashBag->add($message, $messageVars, $level, $domain, $addNotification); |
||
143 | } |
||
144 | |||
145 | public function testReadStatusRead(): void |
||
146 | { |
||
147 | $this->assertReadStatus(1, true); |
||
148 | } |
||
149 | |||
150 | public function testReadStatusUnread(): void |
||
151 | { |
||
152 | $this->assertReadStatus(31, false); |
||
153 | } |
||
154 | |||
155 | public function testAddTypeError(): void |
||
156 | { |
||
157 | $this->assertAddTypeCases(FlashBag::LEVEL_ERROR, 'text-danger fa-exclamation-circle'); |
||
158 | } |
||
159 | |||
160 | public function testAddTypeNotice(): void |
||
161 | { |
||
162 | $this->assertAddTypeCases(FlashBag::LEVEL_NOTICE, 'fa-info-circle'); |
||
163 | } |
||
164 | |||
165 | public function testAddTypeDefault(): void |
||
166 | { |
||
167 | $this->assertAddTypeCases('default', 'fa-info-circle'); |
||
168 | } |
||
169 | |||
170 | private function assertReadStatus(int $mauticUserLastActive, bool $isRead): void |
||
171 | { |
||
172 | $message = 'message'; |
||
173 | $messageVars = []; |
||
174 | $level = FlashBag::LEVEL_NOTICE; |
||
175 | $translatedMessage = 'translatedMessage'; |
||
176 | $domain = 'flashes'; |
||
177 | $addNotification = true; |
||
178 | |||
179 | $this->symfonyFlashBag |
||
180 | ->expects($this->once()) |
||
181 | ->method('add') |
||
182 | ->with($level, $translatedMessage); |
||
183 | |||
184 | $this->session |
||
185 | ->expects($this->once()) |
||
186 | ->method('getFlashBag') |
||
187 | ->willReturn($this->symfonyFlashBag); |
||
188 | |||
189 | $this->translator |
||
190 | ->expects($this->once()) |
||
191 | ->method('trans') |
||
192 | ->with($message, $messageVars, $domain) |
||
193 | ->willReturn($translatedMessage); |
||
194 | |||
195 | $request = $this->createMock(Request::class); |
||
196 | $request |
||
197 | ->expects($this->once()) |
||
198 | ->method('get') |
||
199 | ->with('mauticUserLastActive', 0) |
||
200 | ->willReturn($mauticUserLastActive); |
||
201 | |||
202 | $this->requestStack |
||
203 | ->expects($this->once()) |
||
204 | ->method('getCurrentRequest') |
||
205 | ->willReturn($request); |
||
206 | |||
207 | $this->notificationModel |
||
208 | ->expects($this->once()) |
||
209 | ->method('addNotification') |
||
210 | ->with($message, $level, $isRead, null, 'fa-info-circle'); |
||
211 | |||
212 | $this->flashBag->add($message, $messageVars, $level, $domain, $addNotification); |
||
213 | } |
||
214 | |||
215 | private function assertAddTypeCases($level, $expectedIcon): void |
||
216 | { |
||
217 | $message = 'message'; |
||
218 | $messageVars = []; |
||
219 | $translatedMessage = 'translatedMessage'; |
||
220 | $domain = 'flashes'; |
||
221 | $addNotification = true; // <--- |
||
222 | $mauticUserLastActive = 1; // <--- |
||
223 | |||
224 | $this->symfonyFlashBag |
||
225 | ->expects($this->once()) |
||
226 | ->method('add') |
||
227 | ->with($level, $translatedMessage); |
||
228 | |||
229 | $this->session |
||
230 | ->expects($this->once()) |
||
231 | ->method('getFlashBag') |
||
232 | ->willReturn($this->symfonyFlashBag); |
||
233 | |||
234 | $this->translator |
||
235 | ->expects($this->once()) |
||
236 | ->method('trans') |
||
237 | ->with($message, $messageVars, $domain) |
||
238 | ->willReturn($translatedMessage); |
||
239 | |||
240 | $request = $this->createMock(Request::class); |
||
241 | $request |
||
242 | ->expects($this->once()) |
||
243 | ->method('get') |
||
244 | ->with('mauticUserLastActive', 0) |
||
245 | ->willReturn($mauticUserLastActive); |
||
246 | |||
247 | $this->requestStack |
||
248 | ->expects($this->once()) |
||
249 | ->method('getCurrentRequest') |
||
250 | ->willReturn($request); |
||
251 | |||
252 | $this->notificationModel |
||
253 | ->expects($this->once()) |
||
254 | ->method('addNotification') |
||
255 | ->with($message, $level, 1, null, $expectedIcon); |
||
256 | |||
257 | $this->flashBag->add($message, $messageVars, $level, $domain, $addNotification); |
||
258 | } |
||
259 | } |
||
260 |