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