123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Test\Unit\Interception;
- use Magento\Framework\Interception;
- class InterceptorTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Sample\Interceptor
- */
- private $sampleInterceptor;
- /**
- * @var array
- */
- private $samplePlugins;
- /**
- * @var Interception\PluginListInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $pluginListMock;
- protected function setUp()
- {
- $this->pluginListMock = $this->getMockBuilder(Interception\PluginListInterface::class)
- ->getMockForAbstractClass();
- $this->sampleInterceptor = new Sample\Interceptor();
- $this->samplePlugins = [
- 'plugin1' => new Sample\Plugin1(),
- 'plugin2' => new Sample\Plugin2(),
- 'plugin3' => new Sample\Plugin3(),
- 'plugin4' => new Sample\Plugin4()
- ];
- $this->sampleInterceptor->setPluginList($this->pluginListMock);
- }
- public function testCallPlugins()
- {
- $subjectType = Sample\Entity::class;
- $method = 'doSomething';
- $capMethod = ucfirst($method);
- $pluginMap = [
- [$subjectType, 'plugin1', $this->samplePlugins['plugin1']],
- [$subjectType, 'plugin2', $this->samplePlugins['plugin2']],
- [$subjectType, 'plugin3', $this->samplePlugins['plugin3']],
- [$subjectType, 'plugin4', $this->samplePlugins['plugin4']]
- ];
- $pluginInfoMap = [
- [
- $subjectType,
- $method,
- null,
- [
- Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin1', 'plugin2'],
- Interception\DefinitionInterface::LISTENER_AROUND => 'plugin3',
- Interception\DefinitionInterface::LISTENER_AFTER => ['plugin1', 'plugin2', 'plugin3']
- ]
- ],
- [
- $subjectType,
- $method,
- 'plugin3',
- [
- Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin4'],
- Interception\DefinitionInterface::LISTENER_AROUND => 'plugin4',
- Interception\DefinitionInterface::LISTENER_AFTER => ['plugin4']
- ]
- ],
- [
- $subjectType,
- $method,
- 'plugin4',
- null
- ]
- ];
- $expectedPluginCalls = [
- Sample\Plugin1::class . '::before' . $capMethod,
- Sample\Plugin2::class . '::before' . $capMethod,
- Sample\Plugin3::class . '::around' . $capMethod,
- Sample\Plugin4::class . '::before' . $capMethod,
- Sample\Plugin4::class . '::around' . $capMethod,
- Sample\Entity::class . '::' . $method,
- Sample\Plugin4::class . '::after' . $capMethod,
- Sample\Plugin1::class . '::after' . $capMethod,
- Sample\Plugin2::class . '::after' . $capMethod,
- Sample\Plugin3::class . '::after' . $capMethod
- ];
- $this->pluginListMock->expects(static::any())
- ->method('getPlugin')
- ->willReturnMap($pluginMap);
- $this->pluginListMock->expects(static::exactly(3))
- ->method('getNext')
- ->willReturnMap($pluginInfoMap);
- $this->assertTrue($this->sampleInterceptor->$method());
- $this->assertEquals($expectedPluginCalls, $this->sampleInterceptor->getPluginCalls());
- }
- }
|