123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Ui\Test\Unit\Component;
- use Magento\Framework\Api\Filter;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\View\Element\UiComponent\ContextInterface;
- use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
- use Magento\Framework\View\Element\UiComponent\Processor;
- use Magento\Ui\Component\Form;
- class FormTest extends \PHPUnit\Framework\TestCase
- {
- /** @var Form */
- protected $model;
- /** @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $contextMock;
- /** @var FilterBuilder|\PHPUnit_Framework_MockObject_MockObject */
- protected $filterBuilderMock;
- protected function setUp()
- {
- $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
- ->getMockForAbstractClass();
- $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->contextMock->expects($this->never())->method('getProcessor');
- $this->model = new Form(
- $this->contextMock,
- $this->filterBuilderMock
- );
- }
- public function testGetComponentName()
- {
- $this->assertEquals(Form::NAME, $this->model->getComponentName());
- }
- public function testGetDataSourceData()
- {
- $requestFieldName = 'request_id';
- $primaryFieldName = 'primary_id';
- $fieldId = 44;
- $row = ['key' => 'value'];
- $data = [
- $fieldId => $row,
- ];
- $dataSource = [
- 'data' => $row,
- ];
- /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
- $dataProviderMock =
- $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
- ->getMock();
- $dataProviderMock->expects($this->once())
- ->method('getRequestFieldName')
- ->willReturn($requestFieldName);
- $dataProviderMock->expects($this->once())
- ->method('getPrimaryFieldName')
- ->willReturn($primaryFieldName);
- $this->contextMock->expects($this->any())
- ->method('getDataProvider')
- ->willReturn($dataProviderMock);
- $this->contextMock->expects($this->once())
- ->method('getRequestParam')
- ->with($requestFieldName)
- ->willReturn($fieldId);
- /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
- $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filterBuilderMock->expects($this->once())
- ->method('setField')
- ->with($primaryFieldName)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('setValue')
- ->with($fieldId)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('addFilter')
- ->with($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('getData')
- ->willReturn($data);
- $this->assertEquals($dataSource, $this->model->getDataSourceData());
- }
- public function testGetDataSourceDataWithoutData()
- {
- $requestFieldName = 'request_id';
- $primaryFieldName = 'primary_id';
- $fieldId = 44;
- $data = [];
- $dataSource = [];
- /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
- $dataProviderMock =
- $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
- ->getMock();
- $dataProviderMock->expects($this->once())
- ->method('getRequestFieldName')
- ->willReturn($requestFieldName);
- $dataProviderMock->expects($this->once())
- ->method('getPrimaryFieldName')
- ->willReturn($primaryFieldName);
- $this->contextMock->expects($this->any())
- ->method('getDataProvider')
- ->willReturn($dataProviderMock);
- $this->contextMock->expects($this->once())
- ->method('getRequestParam')
- ->with($requestFieldName)
- ->willReturn($fieldId);
- /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
- $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filterBuilderMock->expects($this->once())
- ->method('setField')
- ->with($primaryFieldName)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('setValue')
- ->with($fieldId)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('addFilter')
- ->with($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('getData')
- ->willReturn($data);
- $this->assertEquals($dataSource, $this->model->getDataSourceData());
- }
- public function testGetDataSourceDataWithoutId()
- {
- $requestFieldName = 'request_id';
- $primaryFieldName = 'primary_id';
- $fieldId = null;
- $row = ['key' => 'value'];
- $data = [
- $fieldId => $row,
- ];
- $dataSource = [
- 'data' => $row,
- ];
- /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
- $dataProviderMock =
- $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
- ->getMock();
- $dataProviderMock->expects($this->once())
- ->method('getRequestFieldName')
- ->willReturn($requestFieldName);
- $dataProviderMock->expects($this->once())
- ->method('getPrimaryFieldName')
- ->willReturn($primaryFieldName);
- $this->contextMock->expects($this->any())
- ->method('getDataProvider')
- ->willReturn($dataProviderMock);
- $this->contextMock->expects($this->once())
- ->method('getRequestParam')
- ->with($requestFieldName)
- ->willReturn($fieldId);
- /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
- $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filterBuilderMock->expects($this->once())
- ->method('setField')
- ->with($primaryFieldName)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('setValue')
- ->with($fieldId)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('addFilter')
- ->with($filterMock);
- $dataProviderMock->expects($this->once())
- ->method('getData')
- ->willReturn($data);
- $this->assertEquals($dataSource, $this->model->getDataSourceData());
- }
- }
|