Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hluchas authored and dennisameling committed Aug 16, 2020
1 parent ba0d877 commit 9dca9b4
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/bundles/CoreBundle/Service/FlashBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Mautic\CoreBundle\Service;

use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\CoreBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Translation\TranslatorInterface;
Expand All @@ -22,13 +21,17 @@
*/
class FlashBag
{
const LEVEL_ERROR = 'error';
const LEVEL_WARNING = 'warning';
const LEVEL_NOTICE = 'notice';

/**
* @var Session
*/
private $session;

/**
* @var Translator
* @var TranslatorInterface
*/
private $translator;

Expand Down
205 changes: 205 additions & 0 deletions app/bundles/CoreBundle/Test/Service/FlashBagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

/*
* @copyright 2018 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\CoreBundle\Test\Service;

use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\CoreBundle\Service\FlashBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag as SymfonyFlashBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Translation\TranslatorInterface;

class FlashBagTest extends \PHPUnit_Framework_TestCase
{
public function testAdd()
{
// define('MAUTIC_INSTALLER', true); - this is dangerous for further tests, not used

$message = 'message';
$messageVars = [];
$type = FlashBag::LEVEL_NOTICE;
$domain = false;
$addNotification = false;
$symfonyFlashBag = $this->createMock(SymfonyFlashBag::class);
$symfonyFlashBag
->expects($this->once())
->method('add')
->with($type, $message);
$session = $this->createMock(Session::class);
$session
->expects($this->once())
->method('getFlashBag')
->willReturn($symfonyFlashBag);
$translator = $this->createMock(TranslatorInterface::class);
$requestStack = $this->createMock(RequestStack::class);
$notificationModel = $this->createMock(NotificationModel::class);
$flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel);

$flashBag->add($message, $messageVars, $type, $domain, $addNotification);

$message = 'message';
$messageVars['pluralCount'] = 2;
$translatedMessage = 'translatedMessage';
$type = FlashBag::LEVEL_NOTICE;
$domain = 'flashes';
$addNotification = false;
$symfonyFlashBag = $this->createMock(SymfonyFlashBag::class);
$symfonyFlashBag
->expects($this->once())
->method('add')
->with($type, $translatedMessage);
$session = $this->createMock(Session::class);
$session
->expects($this->once())
->method('getFlashBag')
->willReturn($symfonyFlashBag);
$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->once())
->method('transChoice')
->with($message, $messageVars['pluralCount'], $messageVars, $domain)
->willReturn($translatedMessage);
$requestStack = $this->createMock(RequestStack::class);
$notificationModel = $this->createMock(NotificationModel::class);
$flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel);

$flashBag->add($message, $messageVars, $type, $domain, $addNotification);

$message = 'message';
$messageVars = [];
$translatedMessage = 'translatedMessage';
$type = FlashBag::LEVEL_NOTICE;
$domain = 'flashes';
$addNotification = false;
$symfonyFlashBag = $this->createMock(SymfonyFlashBag::class);
$symfonyFlashBag
->expects($this->once())
->method('add')
->with($type, $translatedMessage);
$session = $this->createMock(Session::class);
$session
->expects($this->once())
->method('getFlashBag')
->willReturn($symfonyFlashBag);
$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->once())
->method('trans')
->with($message, $messageVars, $domain)
->willReturn($translatedMessage);
$requestStack = $this->createMock(RequestStack::class);
$notificationModel = $this->createMock(NotificationModel::class);
$flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel);

$flashBag->add($message, $messageVars, $type, $domain, $addNotification);

$this->testReadStatus(1, true);
$this->testReadStatus(31, false);

$this->testAddTypeCases(FlashBag::LEVEL_ERROR, 'text-danger fa-exclamation-circle');
$this->testAddTypeCases(FlashBag::LEVEL_WARNING, 'text-warning fa-exclamation-triangle');
$this->testAddTypeCases(FlashBag::LEVEL_NOTICE, 'fa-info-circle');
$this->testAddTypeCases('default', 'fa-info-circle');
}

private function testReadStatus($mauticUserLastActive, $isRead)
{
$message = 'message';
$messageVars = [];
$type = FlashBag::LEVEL_NOTICE;
$translatedMessage = 'translatedMessage';
$domain = 'flashes';
$addNotification = true;
$isRead = $mauticUserLastActive > 30 ? 0 : 1;
$symfonyFlashBag = $this->createMock(SymfonyFlashBag::class);
$symfonyFlashBag
->expects($this->once())
->method('add')
->with($type, $translatedMessage);
$session = $this->createMock(Session::class);
$session
->expects($this->once())
->method('getFlashBag')
->willReturn($symfonyFlashBag);
$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->once())
->method('trans')
->with($message, $messageVars, $domain)
->willReturn($translatedMessage);
$request = $this->createMock(Request::class);
$request
->expects($this->once())
->method('get')
->with('mauticUserLastActive', 0)
->willReturn($mauticUserLastActive);
$requestStack = $this->createMock(RequestStack::class);
$requestStack
->expects($this->once())
->method('getCurrentRequest')
->willReturn($request);
$notificationModel = $this->createMock(NotificationModel::class);
$notificationModel
->expects($this->once())
->method('addNotification')
->with($message, $type, $isRead, null, 'fa-info-circle');
$flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel);

$flashBag->add($message, $messageVars, $type, $domain, $addNotification);
}

private function testAddTypeCases($type, $expectedIcon)
{
$message = 'message';
$messageVars = [];
$translatedMessage = 'translatedMessage';
$domain = 'flashes';
$addNotification = true; // <---
$mauticUserLastActive = 1; // <---
$symfonyFlashBag = $this->createMock(SymfonyFlashBag::class);
$symfonyFlashBag
->expects($this->once())
->method('add')
->with($type, $translatedMessage);
$session = $this->createMock(Session::class);
$session
->expects($this->once())
->method('getFlashBag')
->willReturn($symfonyFlashBag);
$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->once())
->method('trans')
->with($message, $messageVars, $domain)
->willReturn($translatedMessage);
$request = $this->createMock(Request::class);
$request
->expects($this->once())
->method('get')
->with('mauticUserLastActive', 0)
->willReturn($mauticUserLastActive);
$requestStack = $this->createMock(RequestStack::class);
$requestStack
->expects($this->once())
->method('getCurrentRequest')
->willReturn($request);
$notificationModel = $this->createMock(NotificationModel::class);
$notificationModel
->expects($this->once())
->method('addNotification')
->with($message, $type, 1, null, $expectedIcon);
$flashBag = new FlashBag($session, $translator, $requestStack, $notificationModel);

$flashBag->add($message, $messageVars, $type, $domain, $addNotification);
}
}

0 comments on commit 9dca9b4

Please sign in to comment.