Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InterceptorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class InterceptorTest extends \PHPUnit\Framework\TestCase
11 {
15  private $sampleInterceptor;
16 
20  private $samplePlugins;
21 
25  private $pluginListMock;
26 
27  protected function setUp()
28  {
29  $this->pluginListMock = $this->getMockBuilder(Interception\PluginListInterface::class)
30  ->getMockForAbstractClass();
31 
32  $this->sampleInterceptor = new Sample\Interceptor();
33  $this->samplePlugins = [
34  'plugin1' => new Sample\Plugin1(),
35  'plugin2' => new Sample\Plugin2(),
36  'plugin3' => new Sample\Plugin3(),
37  'plugin4' => new Sample\Plugin4()
38  ];
39 
40  $this->sampleInterceptor->setPluginList($this->pluginListMock);
41  }
42 
43  public function testCallPlugins()
44  {
45  $subjectType = Sample\Entity::class;
46  $method = 'doSomething';
47  $capMethod = ucfirst($method);
48  $pluginMap = [
49  [$subjectType, 'plugin1', $this->samplePlugins['plugin1']],
50  [$subjectType, 'plugin2', $this->samplePlugins['plugin2']],
51  [$subjectType, 'plugin3', $this->samplePlugins['plugin3']],
52  [$subjectType, 'plugin4', $this->samplePlugins['plugin4']]
53  ];
54  $pluginInfoMap = [
55  [
56  $subjectType,
57  $method,
58  null,
59  [
60  Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin1', 'plugin2'],
62  Interception\DefinitionInterface::LISTENER_AFTER => ['plugin1', 'plugin2', 'plugin3']
63  ]
64  ],
65  [
66  $subjectType,
67  $method,
68  'plugin3',
69  [
73  ]
74  ],
75  [
76  $subjectType,
77  $method,
78  'plugin4',
79  null
80  ]
81  ];
82  $expectedPluginCalls = [
83  Sample\Plugin1::class . '::before' . $capMethod,
84  Sample\Plugin2::class . '::before' . $capMethod,
85  Sample\Plugin3::class . '::around' . $capMethod,
86  Sample\Plugin4::class . '::before' . $capMethod,
87  Sample\Plugin4::class . '::around' . $capMethod,
88  Sample\Entity::class . '::' . $method,
89  Sample\Plugin4::class . '::after' . $capMethod,
90  Sample\Plugin1::class . '::after' . $capMethod,
91  Sample\Plugin2::class . '::after' . $capMethod,
92  Sample\Plugin3::class . '::after' . $capMethod
93  ];
94 
95  $this->pluginListMock->expects(static::any())
96  ->method('getPlugin')
97  ->willReturnMap($pluginMap);
98  $this->pluginListMock->expects(static::exactly(3))
99  ->method('getNext')
100  ->willReturnMap($pluginInfoMap);
101 
102  $this->assertTrue($this->sampleInterceptor->$method());
103  $this->assertEquals($expectedPluginCalls, $this->sampleInterceptor->getPluginCalls());
104  }
105 }
$method
Definition: info.phtml:13