InterceptorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit\Interception;
  7. use Magento\Framework\Interception;
  8. class InterceptorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Sample\Interceptor
  12. */
  13. private $sampleInterceptor;
  14. /**
  15. * @var array
  16. */
  17. private $samplePlugins;
  18. /**
  19. * @var Interception\PluginListInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $pluginListMock;
  22. protected function setUp()
  23. {
  24. $this->pluginListMock = $this->getMockBuilder(Interception\PluginListInterface::class)
  25. ->getMockForAbstractClass();
  26. $this->sampleInterceptor = new Sample\Interceptor();
  27. $this->samplePlugins = [
  28. 'plugin1' => new Sample\Plugin1(),
  29. 'plugin2' => new Sample\Plugin2(),
  30. 'plugin3' => new Sample\Plugin3(),
  31. 'plugin4' => new Sample\Plugin4()
  32. ];
  33. $this->sampleInterceptor->setPluginList($this->pluginListMock);
  34. }
  35. public function testCallPlugins()
  36. {
  37. $subjectType = Sample\Entity::class;
  38. $method = 'doSomething';
  39. $capMethod = ucfirst($method);
  40. $pluginMap = [
  41. [$subjectType, 'plugin1', $this->samplePlugins['plugin1']],
  42. [$subjectType, 'plugin2', $this->samplePlugins['plugin2']],
  43. [$subjectType, 'plugin3', $this->samplePlugins['plugin3']],
  44. [$subjectType, 'plugin4', $this->samplePlugins['plugin4']]
  45. ];
  46. $pluginInfoMap = [
  47. [
  48. $subjectType,
  49. $method,
  50. null,
  51. [
  52. Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin1', 'plugin2'],
  53. Interception\DefinitionInterface::LISTENER_AROUND => 'plugin3',
  54. Interception\DefinitionInterface::LISTENER_AFTER => ['plugin1', 'plugin2', 'plugin3']
  55. ]
  56. ],
  57. [
  58. $subjectType,
  59. $method,
  60. 'plugin3',
  61. [
  62. Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin4'],
  63. Interception\DefinitionInterface::LISTENER_AROUND => 'plugin4',
  64. Interception\DefinitionInterface::LISTENER_AFTER => ['plugin4']
  65. ]
  66. ],
  67. [
  68. $subjectType,
  69. $method,
  70. 'plugin4',
  71. null
  72. ]
  73. ];
  74. $expectedPluginCalls = [
  75. Sample\Plugin1::class . '::before' . $capMethod,
  76. Sample\Plugin2::class . '::before' . $capMethod,
  77. Sample\Plugin3::class . '::around' . $capMethod,
  78. Sample\Plugin4::class . '::before' . $capMethod,
  79. Sample\Plugin4::class . '::around' . $capMethod,
  80. Sample\Entity::class . '::' . $method,
  81. Sample\Plugin4::class . '::after' . $capMethod,
  82. Sample\Plugin1::class . '::after' . $capMethod,
  83. Sample\Plugin2::class . '::after' . $capMethod,
  84. Sample\Plugin3::class . '::after' . $capMethod
  85. ];
  86. $this->pluginListMock->expects(static::any())
  87. ->method('getPlugin')
  88. ->willReturnMap($pluginMap);
  89. $this->pluginListMock->expects(static::exactly(3))
  90. ->method('getNext')
  91. ->willReturnMap($pluginInfoMap);
  92. $this->assertTrue($this->sampleInterceptor->$method());
  93. $this->assertEquals($expectedPluginCalls, $this->sampleInterceptor->getPluginCalls());
  94. }
  95. }