ActionPoolTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Component\Control;
  7. use Magento\Framework\View\Element\AbstractBlock;
  8. use Magento\Framework\View\Element\UiComponent\Context;
  9. use Magento\Framework\View\Element\UiComponentInterface;
  10. use Magento\Framework\View\LayoutInterface;
  11. use Magento\Ui\Component\Control\ActionPool;
  12. class ActionPoolTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * Actions toolbar block name
  16. */
  17. const ACTIONS_PAGE_TOOLBAR = 'page.actions.toolbar';
  18. /**
  19. * @var ActionPool
  20. */
  21. protected $actionPool;
  22. /**
  23. * @var Context| \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $contextMock;
  26. /**
  27. * @var ItemFactory| \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $itemFactoryMock;
  30. /**
  31. * @var AbstractBlock| \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $toolbarBlockMock;
  34. /**
  35. * @var UiComponentInterface| \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $uiComponentInterfaceMock;
  38. /**
  39. * @var Object[]| \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $items;
  42. /**
  43. * @var LayoutInterface[]| \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $layoutMock;
  46. /**
  47. * @var string
  48. */
  49. protected $key = 'id';
  50. protected function setUp()
  51. {
  52. $this->contextMock = $this->createPartialMock(
  53. \Magento\Framework\View\Element\UiComponent\Context::class,
  54. ['getPageLayout']
  55. );
  56. $this->toolbarBlockMock = $this->createPartialMock(
  57. \Magento\Framework\View\Element\AbstractBlock::class,
  58. ['setChild']
  59. );
  60. $this->layoutMock = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class);
  61. $this->contextMock->expects($this->any())->method('getPageLayout')->willReturn($this->layoutMock);
  62. $this->layoutMock->expects($this->once())
  63. ->method('getBlock')
  64. ->with(static::ACTIONS_PAGE_TOOLBAR)
  65. ->willReturn($this->toolbarBlockMock);
  66. $this->itemFactoryMock = $this->createPartialMock(\Magento\Ui\Component\Control\ItemFactory::class, ['create']);
  67. $this->uiComponentInterfaceMock = $this->getMockForAbstractClass(
  68. \Magento\Framework\View\Element\UiComponentInterface::class
  69. );
  70. $this->items[$this->key] = $this->createPartialMock(\Magento\Ui\Component\Control\Item::class, ['setData']);
  71. $this->actionPool = new ActionPool(
  72. $this->contextMock,
  73. $this->itemFactoryMock
  74. );
  75. }
  76. public function testAdd()
  77. {
  78. $data = ['id' => 'id'];
  79. $this->uiComponentInterfaceMock->expects($this->once())->method('getName')->willReturn('name');
  80. $this->itemFactoryMock->expects($this->any())->method('create')->willReturn($this->items[$this->key]);
  81. $this->items[$this->key]->expects($this->any())->method('setData')->with($data)->willReturnSelf();
  82. $this->contextMock->expects($this->any())->method('getPageLayout')->willReturn($this->layoutMock);
  83. $toolbarContainerMock = $this->createMock(\Magento\Backend\Block\Widget\Button\Toolbar\Container::class);
  84. $this->layoutMock->expects($this->once())
  85. ->method('createBlock')
  86. ->with(
  87. \Magento\Ui\Component\Control\Container::class,
  88. 'container-name-' . $this->key,
  89. [
  90. 'data' => [
  91. 'button_item' => $this->items[$this->key],
  92. 'context' => $this->uiComponentInterfaceMock,
  93. ]
  94. ]
  95. )
  96. ->willReturn($toolbarContainerMock);
  97. $this->toolbarBlockMock->expects($this->once())
  98. ->method('setChild')
  99. ->with($this->key, $toolbarContainerMock)
  100. ->willReturnSelf();
  101. $this->actionPool->add($this->key, $data, $this->uiComponentInterfaceMock);
  102. }
  103. public function testRemove()
  104. {
  105. $this->testAdd();
  106. $this->actionPool->remove($this->key);
  107. }
  108. public function testUpdate()
  109. {
  110. $this->testAdd();
  111. $data = ['id' => 'id'];
  112. $this->items[$this->key]->expects($this->any())->method('setData')->with($data)->willReturnSelf();
  113. $this->actionPool->update($this->key, $data);
  114. }
  115. }