BuilderTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model\Menu;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class BuilderTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Backend\Model\Menu\Builder
  12. */
  13. private $model;
  14. /**
  15. * @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $menuMock;
  18. /**
  19. * @var \Magento\Backend\Model\Menu\Item\Factory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $factoryMock;
  22. protected function setUp()
  23. {
  24. $this->factoryMock = $this->createMock(\Magento\Backend\Model\Menu\Item\Factory::class);
  25. $this->menuMock = $this->createPartialMock(\Magento\Backend\Model\Menu::class, ['addChild', 'add']);
  26. $this->model = (new ObjectManager($this))->getObject(
  27. \Magento\Backend\Model\Menu\Builder::class,
  28. [
  29. 'menuItemFactory' => $this->factoryMock
  30. ]
  31. );
  32. }
  33. public function testProcessCommand()
  34. {
  35. $command = $this->createMock(\Magento\Backend\Model\Menu\Builder\Command\Add::class);
  36. $command->expects($this->any())->method('getId')->will($this->returnValue(1));
  37. $command2 = $this->createMock(\Magento\Backend\Model\Menu\Builder\Command\Update::class);
  38. $command2->expects($this->any())->method('getId')->will($this->returnValue(1));
  39. $command->expects($this->once())->method('chain')->with($this->equalTo($command2));
  40. $this->model->processCommand($command);
  41. $this->model->processCommand($command2);
  42. }
  43. public function testGetResultBuildsTreeStructure()
  44. {
  45. $item1 = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  46. $item1->expects($this->once())->method('getChildren')->will($this->returnValue($this->menuMock));
  47. $this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
  48. $item2 = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  49. $this->factoryMock->expects($this->at(1))->method('create')->will($this->returnValue($item2));
  50. $this->menuMock->expects(
  51. $this->at(0)
  52. )->method(
  53. 'add'
  54. )->with(
  55. $this->isInstanceOf(\Magento\Backend\Model\Menu\Item::class),
  56. $this->equalTo(null),
  57. $this->equalTo(2)
  58. );
  59. $this->menuMock->expects(
  60. $this->at(1)
  61. )->method(
  62. 'add'
  63. )->with(
  64. $this->isInstanceOf(\Magento\Backend\Model\Menu\Item::class),
  65. $this->equalTo(null),
  66. $this->equalTo(4)
  67. );
  68. $this->model->processCommand(
  69. new \Magento\Backend\Model\Menu\Builder\Command\Add(
  70. [
  71. 'id' => 'item1',
  72. 'title' => 'Item 1',
  73. 'module' => 'Magento_Backend',
  74. 'sortOrder' => 2,
  75. 'resource' => 'Magento_Backend::item1',
  76. ]
  77. )
  78. );
  79. $this->model->processCommand(
  80. new \Magento\Backend\Model\Menu\Builder\Command\Add(
  81. [
  82. 'id' => 'item2',
  83. 'parent' => 'item1',
  84. 'title' => 'two',
  85. 'module' => 'Magento_Backend',
  86. 'sortOrder' => 4,
  87. 'resource' => 'Magento_Backend::item2',
  88. ]
  89. )
  90. );
  91. $this->model->getResult($this->menuMock);
  92. }
  93. public function testGetResultSkipsRemovedItems()
  94. {
  95. $this->model->processCommand(
  96. new \Magento\Backend\Model\Menu\Builder\Command\Add(
  97. [
  98. 'id' => 1,
  99. 'title' => 'Item 1',
  100. 'module' => 'Magento_Backend',
  101. 'resource' => 'Magento_Backend::i1',
  102. ]
  103. )
  104. );
  105. $this->model->processCommand(new \Magento\Backend\Model\Menu\Builder\Command\Remove(['id' => 1]));
  106. $this->menuMock->expects($this->never())->method('addChild');
  107. $this->model->getResult($this->menuMock);
  108. }
  109. /**
  110. * @expectedException \OutOfRangeException
  111. */
  112. public function testGetResultSkipItemsWithInvalidParent()
  113. {
  114. $item1 = $this->createMock(\Magento\Backend\Model\Menu\Item::class);
  115. $this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
  116. $this->model->processCommand(
  117. new \Magento\Backend\Model\Menu\Builder\Command\Add(
  118. [
  119. 'id' => 'item1',
  120. 'parent' => 'not_exists',
  121. 'title' => 'Item 1',
  122. 'module' => 'Magento_Backend',
  123. 'resource' => 'Magento_Backend::item1',
  124. ]
  125. )
  126. );
  127. $this->model->getResult($this->menuMock);
  128. }
  129. }