InvokerDefaultTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Event\Test\Unit\Invoker;
  7. class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $_observerFactoryMock;
  13. /**
  14. * @var \Magento\Framework\Event\Observer|PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_observerMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_listenerMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_appStateMock;
  25. /**
  26. * @var \Magento\Framework\Event\Invoker\InvokerDefault
  27. */
  28. protected $_invokerDefault;
  29. protected function setUp()
  30. {
  31. $this->_observerFactoryMock = $this->createMock(\Magento\Framework\Event\ObserverFactory::class);
  32. $this->_observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  33. $this->_listenerMock = $this->createPartialMock(
  34. \Magento\Framework\Event\Test\Unit\Invoker\ObserverExample::class,
  35. ['execute']
  36. );
  37. $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  38. $this->_invokerDefault = new \Magento\Framework\Event\Invoker\InvokerDefault(
  39. $this->_observerFactoryMock,
  40. $this->_appStateMock
  41. );
  42. }
  43. public function testDispatchWithDisabledObserver()
  44. {
  45. $this->_observerFactoryMock->expects($this->never())->method('get');
  46. $this->_observerFactoryMock->expects($this->never())->method('create');
  47. $this->_invokerDefault->dispatch(['disabled' => true], $this->_observerMock);
  48. }
  49. public function testDispatchWithNonSharedInstance()
  50. {
  51. $this->_listenerMock->expects($this->once())->method('execute');
  52. $this->_observerFactoryMock->expects($this->never())->method('get');
  53. $this->_observerFactoryMock->expects(
  54. $this->once()
  55. )->method(
  56. 'create'
  57. )->with(
  58. 'class_name'
  59. )->will(
  60. $this->returnValue($this->_listenerMock)
  61. );
  62. $this->_invokerDefault->dispatch(
  63. ['shared' => false, 'instance' => 'class_name', 'name' => 'observer'],
  64. $this->_observerMock
  65. );
  66. }
  67. public function testDispatchWithSharedInstance()
  68. {
  69. $this->_listenerMock->expects($this->once())->method('execute');
  70. $this->_observerFactoryMock->expects($this->never())->method('create');
  71. $this->_observerFactoryMock->expects(
  72. $this->once()
  73. )->method(
  74. 'get'
  75. )->with(
  76. 'class_name'
  77. )->will(
  78. $this->returnValue($this->_listenerMock)
  79. );
  80. $this->_invokerDefault->dispatch(
  81. ['shared' => true, 'instance' => 'class_name', 'method' => 'method_name', 'name' => 'observer'],
  82. $this->_observerMock
  83. );
  84. }
  85. /**
  86. * @param string $shared
  87. * @dataProvider dataProviderForMethodIsNotDefined
  88. * @expectedException \LogicException
  89. */
  90. public function testWrongInterfaceCallWithEnabledDeveloperMode($shared)
  91. {
  92. $notObserver = $this->getMockBuilder('NotObserver')->getMock();
  93. $this->_observerFactoryMock->expects(
  94. $this->any()
  95. )->method(
  96. 'create'
  97. )->with(
  98. 'class_name'
  99. )->will(
  100. $this->returnValue($notObserver)
  101. );
  102. $this->_observerFactoryMock->expects(
  103. $this->any()
  104. )->method(
  105. 'get'
  106. )->with(
  107. 'class_name'
  108. )->will(
  109. $this->returnValue($notObserver)
  110. );
  111. $this->_appStateMock->expects(
  112. $this->once()
  113. )->method(
  114. 'getMode'
  115. )->will(
  116. $this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)
  117. );
  118. $this->_invokerDefault->dispatch(
  119. [
  120. 'shared' => $shared,
  121. 'instance' => 'class_name',
  122. 'name' => 'observer',
  123. ],
  124. $this->_observerMock
  125. );
  126. }
  127. /**
  128. * @param string $shared
  129. * @dataProvider dataProviderForMethodIsNotDefined
  130. */
  131. public function testWrongInterfaceCallWithDisabledDeveloperMode($shared)
  132. {
  133. $notObserver = $this->getMockBuilder('NotObserver')->getMock();
  134. $this->_observerFactoryMock->expects(
  135. $this->any()
  136. )->method(
  137. 'create'
  138. )->with(
  139. 'class_name'
  140. )->will(
  141. $this->returnValue($notObserver)
  142. );
  143. $this->_observerFactoryMock->expects(
  144. $this->any()
  145. )->method(
  146. 'get'
  147. )->with(
  148. 'class_name'
  149. )->will(
  150. $this->returnValue($notObserver)
  151. );
  152. $this->_appStateMock->expects(
  153. $this->once()
  154. )->method(
  155. 'getMode'
  156. )->will(
  157. $this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION)
  158. );
  159. $this->_invokerDefault->dispatch(
  160. [
  161. 'shared' => $shared,
  162. 'instance' => 'class_name',
  163. 'method' => 'unknown_method_name',
  164. 'name' => 'observer',
  165. ],
  166. $this->_observerMock
  167. );
  168. }
  169. /**
  170. * @return array
  171. */
  172. public function dataProviderForMethodIsNotDefined()
  173. {
  174. return ['shared' => [true], 'non shared' => [false]];
  175. }
  176. }