Skip to content

Commit

Permalink
Magento should create log if an observer not implement ObserverInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Apr 9, 2019
1 parent 9d4f9a0 commit 4223d2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
30 changes: 26 additions & 4 deletions lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
namespace Magento\Framework\Event\Invoker;

use Magento\Framework\Event\Observer;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\State;

/**
* Default Invoker.
*/
class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
{
/**
Expand All @@ -22,20 +27,29 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
/**
* Application state
*
* @var \Magento\Framework\App\State
* @var State
*/
protected $_appState;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param \Magento\Framework\Event\ObserverFactory $observerFactory
* @param \Magento\Framework\App\State $appState
* @param State $appState
* @param LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\Event\ObserverFactory $observerFactory,
\Magento\Framework\App\State $appState
State $appState,
LoggerInterface $logger = null
) {
$this->_observerFactory = $observerFactory;
$this->_appState = $appState;
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(LoggerInterface::class);
}

/**
Expand All @@ -61,6 +75,8 @@ public function dispatch(array $configuration, Observer $observer)
}

/**
* Execute Observer.
*
* @param \Magento\Framework\Event\ObserverInterface $object
* @param Observer $observer
* @return $this
Expand All @@ -70,14 +86,20 @@ protected function _callObserverMethod($object, $observer)
{
if ($object instanceof \Magento\Framework\Event\ObserverInterface) {
$object->execute($observer);
} elseif ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
} elseif ($this->_appState->getMode() == State::MODE_DEVELOPER) {
throw new \LogicException(
sprintf(
'Observer "%s" must implement interface "%s"',
get_class($object),
\Magento\Framework\Event\ObserverInterface::class
)
);
} else {
$this->logger->warning(sprintf(
'Observer "%s" must implement interface "%s"',
get_class($object),
\Magento\Framework\Event\ObserverInterface::class
));
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Framework\Event\Test\Unit\Invoker;

/**
* Test for Magento\Framework\Event\Invoker\InvokerDefault.
*/
class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -32,6 +35,11 @@ class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
*/
protected $_invokerDefault;

/**
* @var |Psr\Log|LoggerInterface
*/
private $loggerMock;

protected function setUp()
{
$this->_observerFactoryMock = $this->createMock(\Magento\Framework\Event\ObserverFactory::class);
Expand All @@ -41,10 +49,12 @@ protected function setUp()
['execute']
);
$this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);

$this->_invokerDefault = new \Magento\Framework\Event\Invoker\InvokerDefault(
$this->_observerFactoryMock,
$this->_appStateMock
$this->_appStateMock,
$this->loggerMock
);
}

Expand Down Expand Up @@ -166,13 +176,15 @@ public function testWrongInterfaceCallWithDisabledDeveloperMode($shared)
$this->returnValue($notObserver)
);
$this->_appStateMock->expects(
$this->once()
$this->exactly(1)
)->method(
'getMode'
)->will(
$this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION)
);

$this->loggerMock->expects($this->once())->method('warning');

$this->_invokerDefault->dispatch(
[
'shared' => $shared,
Expand Down

0 comments on commit 4223d2b

Please sign in to comment.