Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvokerDefaultTest.php
Go to the documentation of this file.
1 <?php
7 
8 class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
9 {
14 
18  protected $_observerMock;
19 
23  protected $_listenerMock;
24 
28  protected $_appStateMock;
29 
33  protected $_invokerDefault;
34 
35  protected function setUp()
36  {
37  $this->_observerFactoryMock = $this->createMock(\Magento\Framework\Event\ObserverFactory::class);
38  $this->_observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
39  $this->_listenerMock = $this->createPartialMock(
40  \Magento\Framework\Event\Test\Unit\Invoker\ObserverExample::class,
41  ['execute']
42  );
43  $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
44 
45  $this->_invokerDefault = new \Magento\Framework\Event\Invoker\InvokerDefault(
46  $this->_observerFactoryMock,
47  $this->_appStateMock
48  );
49  }
50 
52  {
53  $this->_observerFactoryMock->expects($this->never())->method('get');
54  $this->_observerFactoryMock->expects($this->never())->method('create');
55 
56  $this->_invokerDefault->dispatch(['disabled' => true], $this->_observerMock);
57  }
58 
60  {
61  $this->_listenerMock->expects($this->once())->method('execute');
62  $this->_observerFactoryMock->expects($this->never())->method('get');
63  $this->_observerFactoryMock->expects(
64  $this->once()
65  )->method(
66  'create'
67  )->with(
68  'class_name'
69  )->will(
70  $this->returnValue($this->_listenerMock)
71  );
72 
73  $this->_invokerDefault->dispatch(
74  ['shared' => false, 'instance' => 'class_name', 'name' => 'observer'],
75  $this->_observerMock
76  );
77  }
78 
80  {
81  $this->_listenerMock->expects($this->once())->method('execute');
82  $this->_observerFactoryMock->expects($this->never())->method('create');
83  $this->_observerFactoryMock->expects(
84  $this->once()
85  )->method(
86  'get'
87  )->with(
88  'class_name'
89  )->will(
90  $this->returnValue($this->_listenerMock)
91  );
92 
93  $this->_invokerDefault->dispatch(
94  ['shared' => true, 'instance' => 'class_name', 'method' => 'method_name', 'name' => 'observer'],
95  $this->_observerMock
96  );
97  }
98 
105  {
106  $notObserver = $this->getMockBuilder('NotObserver')->getMock();
107  $this->_observerFactoryMock->expects(
108  $this->any()
109  )->method(
110  'create'
111  )->with(
112  'class_name'
113  )->will(
114  $this->returnValue($notObserver)
115  );
116  $this->_observerFactoryMock->expects(
117  $this->any()
118  )->method(
119  'get'
120  )->with(
121  'class_name'
122  )->will(
123  $this->returnValue($notObserver)
124  );
125  $this->_appStateMock->expects(
126  $this->once()
127  )->method(
128  'getMode'
129  )->will(
130  $this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)
131  );
132 
133  $this->_invokerDefault->dispatch(
134  [
135  'shared' => $shared,
136  'instance' => 'class_name',
137  'name' => 'observer',
138  ],
139  $this->_observerMock
140  );
141  }
142 
148  {
149  $notObserver = $this->getMockBuilder('NotObserver')->getMock();
150  $this->_observerFactoryMock->expects(
151  $this->any()
152  )->method(
153  'create'
154  )->with(
155  'class_name'
156  )->will(
157  $this->returnValue($notObserver)
158  );
159  $this->_observerFactoryMock->expects(
160  $this->any()
161  )->method(
162  'get'
163  )->with(
164  'class_name'
165  )->will(
166  $this->returnValue($notObserver)
167  );
168  $this->_appStateMock->expects(
169  $this->once()
170  )->method(
171  'getMode'
172  )->will(
173  $this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION)
174  );
175 
176  $this->_invokerDefault->dispatch(
177  [
178  'shared' => $shared,
179  'instance' => 'class_name',
180  'method' => 'unknown_method_name',
181  'name' => 'observer',
182  ],
183  $this->_observerMock
184  );
185  }
186 
191  {
192  return ['shared' => [true], 'non shared' => [false]];
193  }
194 }