|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Tests\Channel; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 17 | +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; |
| 18 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 19 | +use Symfony\Component\Notifier\Channel\BrowserChannel; |
| 20 | +use Symfony\Component\Notifier\Exception\FlashMessageMappingException; |
| 21 | +use Symfony\Component\Notifier\FlashMessage\BootstrapFlashMessageImportanceMapper; |
| 22 | +use Symfony\Component\Notifier\FlashMessage\DefaultFlashMessageImportanceMapper; |
| 23 | +use Symfony\Component\Notifier\FlashMessage\FlashMessageImportanceMapperInterface; |
| 24 | +use Symfony\Component\Notifier\Notification\Notification; |
| 25 | +use Symfony\Component\Notifier\Recipient\Recipient; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Ben Roberts <[email protected]> |
| 29 | + */ |
| 30 | +class BrowserChannelTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @dataProvider defaultFlashMessageImportanceDataProvider |
| 34 | + */ |
| 35 | + public function testImportanceLevelIsReflectedInFlashMessageType( |
| 36 | + FlashMessageImportanceMapperInterface $mapper, |
| 37 | + string $importance, |
| 38 | + string $expectedFlashMessageType |
| 39 | + ) { |
| 40 | + $session = $this->createMock(Session::class); |
| 41 | + $session->method('getFlashBag')->willReturn(new FlashBag()); |
| 42 | + $browserChannel = $this->buildBrowserChannel($session, $mapper); |
| 43 | + $notification = new Notification(); |
| 44 | + $notification->importance($importance); |
| 45 | + $recipient = new Recipient( '[email protected]'); |
| 46 | + |
| 47 | + $browserChannel->notify($notification, $recipient); |
| 48 | + |
| 49 | + $this->assertEquals($expectedFlashMessageType, array_key_first($session->getFlashBag()->all())); |
| 50 | + } |
| 51 | + |
| 52 | + public function testUnknownImportanceMappingIsReported() |
| 53 | + { |
| 54 | + $session = $this->createMock(Session::class); |
| 55 | + $session->method('getFlashBag')->willReturn(new FlashBag()); |
| 56 | + $browserChannel = $this->buildBrowserChannel($session, new DefaultFlashMessageImportanceMapper()); |
| 57 | + $notification = new Notification(); |
| 58 | + $notification->importance('unknown-importance-string'); |
| 59 | + $recipient = new Recipient( '[email protected]'); |
| 60 | + |
| 61 | + $this->expectException(FlashMessageMappingException::class); |
| 62 | + |
| 63 | + $browserChannel->notify($notification, $recipient); |
| 64 | + } |
| 65 | + |
| 66 | + public function defaultFlashMessageImportanceDataProvider(): array |
| 67 | + { |
| 68 | + return [ |
| 69 | + [new DefaultFlashMessageImportanceMapper(), Notification::IMPORTANCE_URGENT, 'notification'], |
| 70 | + [new DefaultFlashMessageImportanceMapper(), Notification::IMPORTANCE_HIGH, 'notification'], |
| 71 | + [new DefaultFlashMessageImportanceMapper(), Notification::IMPORTANCE_MEDIUM, 'notification'], |
| 72 | + [new DefaultFlashMessageImportanceMapper(), Notification::IMPORTANCE_LOW, 'notification'], |
| 73 | + [new BootstrapFlashMessageImportanceMapper(), Notification::IMPORTANCE_URGENT, 'danger'], |
| 74 | + [new BootstrapFlashMessageImportanceMapper(), Notification::IMPORTANCE_HIGH, 'warning'], |
| 75 | + [new BootstrapFlashMessageImportanceMapper(), Notification::IMPORTANCE_MEDIUM, 'info'], |
| 76 | + [new BootstrapFlashMessageImportanceMapper(), Notification::IMPORTANCE_LOW, 'success'], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + private function buildBrowserChannel(Session $session, FlashMessageImportanceMapperInterface $mapper): BrowserChannel |
| 81 | + { |
| 82 | + $request = $this->createMock(Request::class); |
| 83 | + $request->method('getSession')->willReturn($session); |
| 84 | + $requestStack = $this->createStub(RequestStack::class); |
| 85 | + $requestStack->method('getCurrentRequest')->willReturn($request); |
| 86 | + |
| 87 | + return new BrowserChannel($requestStack, $mapper); |
| 88 | + } |
| 89 | +} |
0 commit comments