Skip to content

Commit 8fc9670

Browse files
authored
ENGCOM-4505: Magento should create a log entry if an observer does not implement ObserverInterface #21767
2 parents 2c3b106 + 894c418 commit 8fc9670

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
namespace Magento\Framework\Event\Invoker;
1010

1111
use Magento\Framework\Event\Observer;
12+
use Psr\Log\LoggerInterface;
13+
use Magento\Framework\App\State;
1214

15+
/**
16+
* Default Invoker.
17+
*/
1318
class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
1419
{
1520
/**
@@ -22,20 +27,29 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
2227
/**
2328
* Application state
2429
*
25-
* @var \Magento\Framework\App\State
30+
* @var State
2631
*/
2732
protected $_appState;
2833

34+
/**
35+
* @var LoggerInterface
36+
*/
37+
private $logger;
38+
2939
/**
3040
* @param \Magento\Framework\Event\ObserverFactory $observerFactory
31-
* @param \Magento\Framework\App\State $appState
41+
* @param State $appState
42+
* @param LoggerInterface $logger
3243
*/
3344
public function __construct(
3445
\Magento\Framework\Event\ObserverFactory $observerFactory,
35-
\Magento\Framework\App\State $appState
46+
State $appState,
47+
LoggerInterface $logger = null
3648
) {
3749
$this->_observerFactory = $observerFactory;
3850
$this->_appState = $appState;
51+
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()
52+
->get(LoggerInterface::class);
3953
}
4054

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

6377
/**
78+
* Execute Observer.
79+
*
6480
* @param \Magento\Framework\Event\ObserverInterface $object
6581
* @param Observer $observer
6682
* @return $this
@@ -70,14 +86,20 @@ protected function _callObserverMethod($object, $observer)
7086
{
7187
if ($object instanceof \Magento\Framework\Event\ObserverInterface) {
7288
$object->execute($observer);
73-
} elseif ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
89+
} elseif ($this->_appState->getMode() == State::MODE_DEVELOPER) {
7490
throw new \LogicException(
7591
sprintf(
7692
'Observer "%s" must implement interface "%s"',
7793
get_class($object),
7894
\Magento\Framework\Event\ObserverInterface::class
7995
)
8096
);
97+
} else {
98+
$this->logger->warning(sprintf(
99+
'Observer "%s" must implement interface "%s"',
100+
get_class($object),
101+
\Magento\Framework\Event\ObserverInterface::class
102+
));
81103
}
82104
return $this;
83105
}

lib/internal/Magento/Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Framework\Event\Test\Unit\Invoker;
77

8+
/**
9+
* Test for Magento\Framework\Event\Invoker\InvokerDefault.
10+
*/
811
class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
912
{
1013
/**
@@ -32,6 +35,11 @@ class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
3235
*/
3336
protected $_invokerDefault;
3437

38+
/**
39+
* @var |Psr\Log|LoggerInterface
40+
*/
41+
private $loggerMock;
42+
3543
protected function setUp()
3644
{
3745
$this->_observerFactoryMock = $this->createMock(\Magento\Framework\Event\ObserverFactory::class);
@@ -41,10 +49,12 @@ protected function setUp()
4149
['execute']
4250
);
4351
$this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
52+
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
4453

4554
$this->_invokerDefault = new \Magento\Framework\Event\Invoker\InvokerDefault(
4655
$this->_observerFactoryMock,
47-
$this->_appStateMock
56+
$this->_appStateMock,
57+
$this->loggerMock
4858
);
4959
}
5060

@@ -166,13 +176,15 @@ public function testWrongInterfaceCallWithDisabledDeveloperMode($shared)
166176
$this->returnValue($notObserver)
167177
);
168178
$this->_appStateMock->expects(
169-
$this->once()
179+
$this->exactly(1)
170180
)->method(
171181
'getMode'
172182
)->will(
173183
$this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION)
174184
);
175185

186+
$this->loggerMock->expects($this->once())->method('warning');
187+
176188
$this->_invokerDefault->dispatch(
177189
[
178190
'shared' => $shared,

0 commit comments

Comments
 (0)