Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UiComponentTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\View\Layout\Generator\UiComponent;
10 
14 
18 class UiComponentTest extends \PHPUnit\Framework\TestCase
19 {
24 
29 
33  protected $readerContextMock;
34 
39 
44 
49 
53  protected $blockFactoryMock;
54 
58  protected $uiComponent;
59 
60  protected function setUp()
61  {
62  $this->objectManagerHelper = new ObjectManagerHelper($this);
63  $this->argumentInterpreterMock = $this->getMockBuilder(
64  \Magento\Framework\Data\Argument\InterpreterInterface::class
65  )->disableOriginalConstructor()->getMockForAbstractClass();
66  $this->uiComponentFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
67  ->setMethods(['setLayout', 'create'])
68  ->disableOriginalConstructor()->getMock();
69  $this->scheduledStructureMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ScheduledStructure::class)
70  ->disableOriginalConstructor()->getMock();
71  $this->contextFactoryMock =
72  $this->createMock(\Magento\Framework\View\Element\UiComponent\ContextFactory::class);
73  $this->blockFactoryMock = $this->createMock(\Magento\Framework\View\Element\BlockFactory::class);
74 
75  $this->uiComponent = $this->objectManagerHelper->getObject(
76  \Magento\Framework\View\Layout\Generator\UiComponent::class,
77  [
78  'uiComponentFactory' => $this->uiComponentFactoryMock,
79  'blockFactory' => $this->blockFactoryMock,
80  'contextFactory' => $this->contextFactoryMock
81  ]
82  );
83  }
84 
85  public function testProcess()
86  {
88 
89  $this->readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class)
90  ->disableOriginalConstructor()->getMock();
91 
92  $this->readerContextMock->expects($this->any())
93  ->method('getScheduledStructure')
94  ->willReturn($this->scheduledStructureMock);
95 
96  $generatorContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Context::class)
97  ->disableOriginalConstructor()->getMock();
98 
99  $structureMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Data\Structure::class)
100  ->disableOriginalConstructor()->getMock();
101 
102  $structureMock->expects($this->once())
103  ->method('addToParentGroup')
104  ->with(UiComponent::TYPE, 'new_group')
105  ->willReturnSelf();
106 
107  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)->getMockForAbstractClass();
108 
109  $generatorContextMock->expects($this->any())
110  ->method('getStructure')
111  ->willReturn($structureMock);
112  $generatorContextMock->expects($this->any())
113  ->method('getLayout')
114  ->willReturn($layoutMock);
115 
116  $this->uiComponentFactoryMock->expects($this->any())
117  ->method('setLayout')
118  ->with($layoutMock)
119  ->willReturnSelf();
120 
121  $componentMock = $this->getMockForAbstractClass(
122  \Magento\Framework\View\Element\UiComponentInterface::class,
123  [],
124  '',
125  false,
126  true,
127  true,
128  []
129  );
130 
131  $contextMock = $this->getMockForAbstractClass(
132  \Magento\Framework\View\Element\UiComponent\ContextInterface::class,
133  [],
134  '',
135  false
136  );
137  $blockMock = $this->getMockForAbstractClass(
138  \Magento\Framework\View\Element\BlockInterface::class,
139  [],
140  '',
141  false
142  );
143 
144  $this->contextFactoryMock->expects($this->once())
145  ->method('create')
146  ->with(
147  [
148  'namespace' => 'uiComponent',
149  'pageLayout' => $layoutMock
150  ]
151  )->willReturn($contextMock);
152 
153  $this->uiComponentFactoryMock->expects($this->any())
154  ->method('create')
155  ->with(
156  'uiComponent',
157  null,
158  ['context' => $contextMock, 'structure' => $structureMock]
159  )->willReturn($componentMock);
160 
161  $this->blockFactoryMock->expects($this->once())
162  ->method('createBlock')
163  ->with(UiComponent::CONTAINER, ['component' => $componentMock])
164  ->willReturn($blockMock);
165 
166  $this->argumentInterpreterMock->expects($this->any())
167  ->method('evaluate')
168  ->will($this->returnValueMap([
169  [['key_1' => 'value_1'], 'value_1'],
170  [['key_2' => 'value_2'], 'value_2'],
171  ]));
172 
173  $layoutMock->expects($this->any())
174  ->method('setBlock')
175  ->with(UiComponent::TYPE, $blockMock)
176  ->willReturnSelf();
177 
178  $this->uiComponent->process($this->readerContextMock, $generatorContextMock);
179  }
180 
181  protected function prepareScheduledStructure()
182  {
183  $this->scheduledStructureMock->expects($this->any())
184  ->method('getElements')
185  ->willReturn([
186  UiComponent::TYPE => [
188  [
189  'attributes' => [
190  'group' => 'new_group',
191  'component' => 'component_name',
192  ],
193  'arguments' => [
194  'attribute_1' => ['key_1' => 'value_1'],
195  'attribute_2' => ['key_2' => 'value_2'],
196  ]
197  ],
198  ],
199  ]);
200  }
201 }