GeneralTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Interception;
  7. /**
  8. * Class GeneralTest
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class GeneralTest extends AbstractPlugin
  12. {
  13. public function setUp()
  14. {
  15. $this->setUpInterceptionConfig(
  16. [\Magento\Framework\Interception\Fixture\InterceptedInterface::class =>
  17. [
  18. 'plugins' => [
  19. 'first' => [
  20. 'instance' =>
  21. \Magento\Framework\Interception\Fixture\Intercepted\InterfacePlugin::class,
  22. 'sortOrder' => 10,
  23. ],
  24. ],
  25. ], \Magento\Framework\Interception\Fixture\Intercepted::class =>
  26. [
  27. 'plugins' => [
  28. 'second' => [
  29. 'instance' => \Magento\Framework\Interception\Fixture\Intercepted\Plugin::class,
  30. 'sortOrder' => 20,
  31. ],
  32. ],
  33. ],
  34. ]
  35. );
  36. parent::setUp();
  37. }
  38. public function testMethodCanBePluginized()
  39. {
  40. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  41. $this->assertEquals('<P:D>1: <D>test</D></P:D>', $subject->D('test'));
  42. }
  43. public function testPluginCanCallOnlyNextMethodOnNext()
  44. {
  45. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  46. $this->assertEquals(
  47. '<IP:aG><P:aG><G><P:G><P:bG><IP:G><IP:bG>test</IP:bG></IP:G></P:bG></P:G></G></P:aG></IP:aG>',
  48. $subject->G('test')
  49. );
  50. }
  51. public function testBeforeAndAfterPluginsAreExecuted()
  52. {
  53. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  54. $this->assertEquals(
  55. '<IP:F><P:D>1: <D>prefix_<F><IP:C><P:C><C>test</C></P:C>' . '</IP:C></F></D></P:D></IP:F>',
  56. $subject->A('prefix_')->F('test')
  57. );
  58. }
  59. public function testPluginCallsOtherMethodsOnSubject()
  60. {
  61. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  62. $this->assertEquals(
  63. '<P:K><IP:F><P:D>1: <D>prefix_<F><IP:C><P:C><C><IP:C><P:C><C>test' .
  64. '</C></P:C></IP:C></C></P:C></IP:C></F></D></P:D></IP:F></P:K>',
  65. $subject->A('prefix_')->K('test')
  66. );
  67. }
  68. public function testInterfacePluginsAreInherited()
  69. {
  70. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  71. $this->assertEquals('<IP:C><P:C><C>test</C></P:C></IP:C>', $subject->C('test'));
  72. }
  73. public function testInternalMethodCallsAreIntercepted()
  74. {
  75. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  76. $this->assertEquals('<B>12<IP:C><P:C><C>1</C></P:C></IP:C></B>', $subject->B('1', '2'));
  77. }
  78. public function testChainedMethodsAreIntercepted()
  79. {
  80. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  81. $this->assertEquals('<P:D>1: <D>prefix_test</D></P:D>', $subject->A('prefix_')->D('test'));
  82. }
  83. public function testFinalMethodWorks()
  84. {
  85. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  86. $this->assertEquals('<P:D>1: <D>prefix_test</D></P:D>', $subject->A('prefix_')->D('test'));
  87. $this->assertEquals('<E>prefix_final</E>', $subject->E('final'));
  88. $this->assertEquals('<P:D>2: <D>prefix_test</D></P:D>', $subject->D('test'));
  89. }
  90. public function testObjectKeepsStateBetweenInvocations()
  91. {
  92. $subject = $this->_objectManager->create(\Magento\Framework\Interception\Fixture\Intercepted::class);
  93. $this->assertEquals('<P:D>1: <D>test</D></P:D>', $subject->D('test'));
  94. $this->assertEquals('<P:D>2: <D>test</D></P:D>', $subject->D('test'));
  95. }
  96. }