Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PluginListTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 require_once __DIR__ . '/../Custom/Module/Model/Item.php';
12 require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php';
13 require_once __DIR__ . '/../Custom/Module/Model/ItemContainer.php';
14 require_once __DIR__ . '/../Custom/Module/Model/ItemContainer/Enhanced.php';
15 require_once __DIR__ . '/../Custom/Module/Model/ItemContainerPlugin/Simple.php';
16 require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Simple.php';
17 require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Advanced.php';
18 require_once __DIR__ . '/../Custom/Module/Model/StartingBackslash.php';
19 require_once __DIR__ . '/../Custom/Module/Model/StartingBackslash/Plugin.php';
20 
24 class PluginListTest extends \PHPUnit\Framework\TestCase
25 {
29  private $object;
30 
34  private $configScopeMock;
35 
39  private $cacheMock;
40 
44  private $loggerMock;
45 
49  private $serializerMock;
50 
54  private $objectManagerMock;
55 
56  protected function setUp()
57  {
58  $readerMap = include __DIR__ . '/../_files/reader_mock_map.php';
59  $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class);
60  $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap));
61 
62  $this->configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
63  $this->cacheMock = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class)
64  ->setMethods(['get'])
65  ->getMockForAbstractClass();
66  // turn cache off
67  $this->cacheMock->expects($this->any())
68  ->method('get')
69  ->will($this->returnValue(false));
70 
71  $omConfigMock = $this->getMockForAbstractClass(
72  \Magento\Framework\Interception\ObjectManager\ConfigInterface::class
73  );
74 
75  $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0));
76 
77  $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
78  ->setMethods(['get'])
79  ->getMockForAbstractClass();
80  $this->objectManagerMock->expects($this->any())
81  ->method('get')
82  ->willReturnArgument(0);
83  $this->serializerMock = $this->createMock(SerializerInterface::class);
84 
85  $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
86 
87  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
88  $this->object = $objectManagerHelper->getObject(
89  \Magento\Framework\Interception\PluginList\PluginList::class,
90  [
91  'reader' => $readerMock,
92  'configScope' => $this->configScopeMock,
93  'cache' => $this->cacheMock,
94  'relations' => new \Magento\Framework\ObjectManager\Relations\Runtime(),
95  'omConfig' => $omConfigMock,
96  'definitions' => new \Magento\Framework\Interception\Definition\Runtime(),
97  'objectManager' => $this->objectManagerMock,
98  'classDefinitions' => $definitions,
99  'scopePriorityScheme' => ['global'],
100  'cacheId' => 'interception',
101  'serializer' => $this->serializerMock
102  ]
103  );
104 
105  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
106  $objectManagerHelper->setBackwardCompatibleProperty(
107  $this->object,
108  'logger',
109  $this->loggerMock
110  );
111  }
112 
113  public function testGetPlugin()
114  {
115  $this->configScopeMock->expects($this->any())->method('getCurrentScope')->will($this->returnValue('backend'));
116  $this->object->getNext(\Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'getName');
117  $this->object->getNext(
118  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
119  'getName'
120  );
121  $this->object->getNext(
122  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash::class,
123  'getName'
124  );
125  $this->assertEquals(
126  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class,
127  $this->object->getPlugin(
128  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
129  'simple_plugin'
130  )
131  );
132  $this->assertEquals(
133  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class,
134  $this->object->getPlugin(
135  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
136  'advanced_plugin'
137  )
138  );
139  $this->assertEquals(
140  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainerPlugin\Simple::class,
141  $this->object->getPlugin(
142  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
143  'simple_plugin'
144  )
145  );
146  $this->assertEquals(
147  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash\Plugin::class,
148  $this->object->getPlugin(
149  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash::class,
150  'simple_plugin'
151  )
152  );
153  }
154 
163  public function testGetPlugins($expectedResult, $type, $method, $scopeCode, $code = '__self')
164  {
165  $this->configScopeMock->expects(
166  $this->any()
167  )->method(
168  'getCurrentScope'
169  )->will(
170  $this->returnValue($scopeCode)
171  );
172  $this->assertEquals($expectedResult, $this->object->getNext($type, $method, $code));
173  }
174 
178  public function getPluginsDataProvider()
179  {
180  return [
181  [
182  [4 => ['simple_plugin']], \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
183  'getName',
184  'global',
185  ],
186  [
187  // advanced plugin has lower sort order
188  [2 => 'advanced_plugin', 4 => ['advanced_plugin']],
189  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
190  'getName',
191  'backend'
192  ],
193  [
194  // advanced plugin has lower sort order
195  [4 => ['simple_plugin']],
196  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
197  'getName',
198  'backend',
199  'advanced_plugin'
200  ],
201  // simple plugin is disabled in configuration for
202  // \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item in frontend
203  [null, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'getName', 'frontend'],
204  // test plugin inheritance
205  [
206  [4 => ['simple_plugin']],
207  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class,
208  'getName',
209  'global'
210  ],
211  [
212  // simple plugin is disabled in configuration for parent
213  [2 => 'advanced_plugin', 4 => ['advanced_plugin']],
214  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class,
215  'getName',
216  'frontend'
217  ],
218  [
219  null,
220  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
221  'getName',
222  'global'
223  ],
224  [
225  [4 => ['simple_plugin']],
226  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
227  'getName',
228  'backend'
229  ]
230  ];
231  }
232 
239  {
240  $this->configScopeMock->expects($this->any())
241  ->method('getCurrentScope')
242  ->will($this->returnValue('frontend'));
243 
244  $this->object->getNext('SomeType', 'someMethod');
245  }
246 
247  public function testLoadScopedDataNotCached()
248  {
249  $this->configScopeMock->expects($this->exactly(3))
250  ->method('getCurrentScope')
251  ->will($this->returnValue('scope'));
252  $this->serializerMock->expects($this->once())
253  ->method('serialize');
254  $this->serializerMock->expects($this->never())
255  ->method('unserialize');
256  $this->cacheMock->expects($this->once())
257  ->method('save');
258 
259  $this->assertEquals(null, $this->object->getNext('Type', 'method'));
260  }
261 
267  {
268  $this->loggerMock->expects($this->once())
269  ->method('info')
270  ->with("Reference to undeclared plugin with name 'simple_plugin'.");
271  $this->configScopeMock->expects($this->any())
272  ->method('getCurrentScope')
273  ->will($this->returnValue('frontend'));
274 
275  $this->assertNull($this->object->getNext('typeWithoutInstance', 'someMethod'));
276  }
277 
282  public function testLoadScopedDataCached()
283  {
284  $this->configScopeMock->expects($this->once())
285  ->method('getCurrentScope')
286  ->will($this->returnValue('scope'));
287 
288  $data = [['key'], ['key'], ['key']];
289  $serializedData = 'serialized data';
290 
291  $this->serializerMock->expects($this->never())
292  ->method('serialize');
293  $this->serializerMock->expects($this->once())
294  ->method('unserialize')
295  ->willReturn($data);
296  $this->cacheMock->expects($this->once())
297  ->method('load')
298  ->with('global|scope|interception')
299  ->willReturn($serializedData);
300 
301  $this->assertEquals(null, $this->object->getNext('Type', 'method'));
302  }
303 
309  {
310  $this->objectManagerMock->expects($this->any())
311  ->method('get')
312  ->will($this->returnArgument(0));
313  $this->configScopeMock->expects($this->any())
314  ->method('getCurrentScope')
315  ->will($this->returnValue('emptyscope'));
316 
317  $this->assertEquals(
318  [4 => ['simple_plugin']],
319  $this->object->getNext(
320  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
321  'getName'
322  )
323  );
324  $this->assertEquals(
325  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class,
326  $this->object->getPlugin(
327  \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
328  'simple_plugin'
329  )
330  );
331  }
332 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$type
Definition: item.phtml:13
$method
Definition: info.phtml:13
testGetPlugins($expectedResult, $type, $method, $scopeCode, $code='__self')
$code
Definition: info.phtml:12