DataProviderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Ui\Component;
  7. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  8. use Magento\Customer\Ui\Component\DataProvider;
  9. use Magento\Customer\Ui\Component\Listing\AttributeRepository;
  10. use Magento\Framework\Api\FilterBuilder;
  11. use Magento\Framework\Api\Search\SearchCriteriaInterface;
  12. use Magento\Framework\View\Element\UiComponent\DataProvider\Reporting;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class DataProviderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. const TEST_REQUEST_NAME = 'test_request_name';
  19. /**
  20. * @var DataProvider
  21. */
  22. protected $model;
  23. /**
  24. * @var Reporting | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $reporting;
  27. /**
  28. * @var SearchCriteriaInterface | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $searchCriteria;
  31. /**
  32. * @var \Magento\Framework\App\RequestInterface | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $request;
  35. /**
  36. * @var FilterBuilder | \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $filterBuilder;
  39. /**
  40. * @var AttributeRepository | \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $attributeRepository;
  43. protected function setUp()
  44. {
  45. $this->reporting = $this->getMockBuilder(
  46. \Magento\Framework\View\Element\UiComponent\DataProvider\Reporting::class
  47. )->disableOriginalConstructor()->getMock();
  48. $searchCriteriaBuilder = $this->mockSearchCriteria();
  49. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  50. ->getMockForAbstractClass();
  51. $this->filterBuilder = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->attributeRepository = $this->getMockBuilder(
  55. \Magento\Customer\Ui\Component\Listing\AttributeRepository::class
  56. )->disableOriginalConstructor()->getMock();
  57. $this->model = new DataProvider(
  58. self::TEST_REQUEST_NAME,
  59. '',
  60. '',
  61. $this->reporting,
  62. $searchCriteriaBuilder,
  63. $this->request,
  64. $this->filterBuilder,
  65. $this->attributeRepository
  66. );
  67. }
  68. public function testGetData()
  69. {
  70. $attributeCode = 'attribute_code';
  71. $attributeValue = [
  72. AttributeMetadataInterface::OPTIONS => [
  73. [
  74. 'label' => 'opt1_label',
  75. 'value' => 'opt1_value',
  76. ],
  77. ],
  78. ];
  79. $expected = [
  80. [
  81. 'attribute_code' => ['opt1_value'],
  82. ],
  83. ];
  84. $attributeMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeInterface::class)
  85. ->getMockForAbstractClass();
  86. $attributeMock->expects($this->once())
  87. ->method('getAttributeCode')
  88. ->willReturn($attributeCode);
  89. $attributeMock->expects($this->once())
  90. ->method('getValue')
  91. ->willReturn('opt1_value');
  92. $searchDocumentMock = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
  93. ->getMockForAbstractClass();
  94. $searchDocumentMock->expects($this->once())
  95. ->method('getCustomAttributes')
  96. ->willReturn([$attributeMock]);
  97. $searchResultMock = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchResultInterface::class)
  98. ->getMockForAbstractClass();
  99. $searchResultMock->expects($this->once())
  100. ->method('getTotalCount')
  101. ->willReturn(1);
  102. $searchResultMock->expects($this->once())
  103. ->method('getItems')
  104. ->willReturn([$searchDocumentMock]);
  105. $this->searchCriteria->expects($this->once())
  106. ->method('setRequestName')
  107. ->with(self::TEST_REQUEST_NAME)
  108. ->willReturnSelf();
  109. $this->reporting->expects($this->once())
  110. ->method('search')
  111. ->with($this->searchCriteria)
  112. ->willReturn($searchResultMock);
  113. $this->attributeRepository->expects($this->once())
  114. ->method('getList')
  115. ->willReturn([$attributeCode => $attributeValue]);
  116. $result = $this->model->getData();
  117. $this->assertTrue(is_array($result));
  118. $this->assertArrayHasKey('totalRecords', $result);
  119. $this->assertEquals(1, $result['totalRecords']);
  120. $this->assertArrayHasKey('items', $result);
  121. $this->assertTrue(is_array($result['items']));
  122. $this->assertEquals($result['items'], $expected);
  123. }
  124. /**
  125. * @return \PHPUnit_Framework_MockObject_MockObject
  126. */
  127. protected function mockSearchCriteria()
  128. {
  129. $this->searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchCriteriaInterface::class)
  130. ->getMockForAbstractClass();
  131. $searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchCriteriaBuilder::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $searchCriteriaBuilder->expects($this->any())
  135. ->method('create')
  136. ->willReturn($this->searchCriteria);
  137. return $searchCriteriaBuilder;
  138. }
  139. }