Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #162 from core23/symfony3
Browse files Browse the repository at this point in the history
Improved support for symfony 3
  • Loading branch information
rande committed Jan 17, 2016
2 parents bbd3fcd + 36673d3 commit 3953bbf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
32 changes: 19 additions & 13 deletions Consumer/LoggerConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Sonata\NotificationBundle\Consumer;

use Psr\Log\LoggerInterface;
use Sonata\NotificationBundle\Exception\InvalidParameterException;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface as LegacyLoggerInterface;

class LoggerConsumer implements ConsumerInterface
{
Expand All @@ -25,21 +26,24 @@ class LoggerConsumer implements ConsumerInterface
* @var string[]
*/
protected $types = array(
'emerg',
'alert',
'crit',
'err',
'warn',
'notice',
'info',
'debug',
'emerg' => 'emergency',
'alert' => 'alert',
'crit' => 'critical',
'err' => 'error',
'warn' => 'warning',
'notice' => 'notice',
'info' => 'info',
'debug' => 'debug',
);

/**
* @param LoggerInterface $logger
* @param LoggerInterface|LegacyLoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
public function __construct($logger)
{
if ($logger instanceof LegacyLoggerInterface) {
trigger_error(sprintf('Using an instance of "%s" is deprecated since version 2.3. Use Psr\Log\LoggerInterface instead.', get_class($logger)), E_USER_DEPRECATED);
}
$this->logger = $logger;
}

Expand All @@ -50,10 +54,12 @@ public function process(ConsumerEvent $event)
{
$message = $event->getMessage();

if (!in_array($message->getValue('level'), $this->types)) {
if (!array_key_exists($message->getValue('level'), $this->types)) {
throw new InvalidParameterException();
}

call_user_func(array($this->logger, $message->getValue('level')), $message->getValue('message'));
$level = $this->types[$message->getValue('level')];

$this->logger->{$level}($message->getValue('message'));
}
}
2 changes: 1 addition & 1 deletion Controller/Api/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function postMessageAction(Request $request)
'csrf_protection' => false,
));

$form->bind($request);
$form->handleRequest($request);

if ($form->isValid()) {
$message = $form->getData();
Expand Down
2 changes: 1 addition & 1 deletion Tests/Backend/MessageManagerBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testStatus($counts, $expectedStatus, $message)
public static function statusProvider()
{
if (!class_exists('ZendDiagnostics\Result\Success')) {
return array(array(1,1,1));
return array(array(1, 1, 1));
}

$data = array();
Expand Down
33 changes: 28 additions & 5 deletions Tests/Consumer/LoggerConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@

class LoggerConsumerTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
/**
* @dataProvider calledTypeProvider
*
* @param $type
* @param $calledType
*/
public function testProcess($type, $calledType)
{
$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger->expects($this->once())->method('crit');
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())->method($calledType);

$message = new Message();
$message->setBody(array(
'level' => 'crit',
'level' => $type,
'message' => 'Alert - Area 52 get compromised!!',
));

Expand All @@ -34,12 +40,29 @@ public function testProcess()
$consumer->process($event);
}

/**
* @return array[]
*/
public function calledTypeProvider()
{
return array(
array('emerg', 'emergency'),
array('alert', 'alert'),
array('crit', 'critical'),
array('err', 'error'),
array('warn', 'warning'),
array('notice', 'notice'),
array('info', 'info'),
array('debug', 'debug'),
);
}

/**
* @expectedException \Sonata\NotificationBundle\Exception\InvalidParameterException
*/
public function testInvalidType()
{
$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger = $this->getMock('Psr\Log\LoggerInterface');

$message = new Message();
$message->setBody(array(
Expand Down
4 changes: 2 additions & 2 deletions Tests/Controller/Api/MessageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testPostMessageAction()
$messageManager->expects($this->once())->method('save')->will($this->returnValue($message));

$form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
$form->expects($this->once())->method('bind');
$form->expects($this->once())->method('handleRequest');
$form->expects($this->once())->method('isValid')->will($this->returnValue(true));
$form->expects($this->once())->method('getData')->will($this->returnValue($message));

Expand All @@ -62,7 +62,7 @@ public function testPostMessageInvalidAction()
$messageManager->expects($this->never())->method('save')->will($this->returnValue($message));

$form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
$form->expects($this->once())->method('bind');
$form->expects($this->once())->method('handleRequest');
$form->expects($this->once())->method('isValid')->will($this->returnValue(false));

$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
Expand Down

0 comments on commit 3953bbf

Please sign in to comment.