|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Model\Customer; |
| 7 | + |
| 8 | +use Magento\Eav\Model\Config; |
| 9 | +use Magento\Eav\Model\Entity\Type; |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\Ui\DataProvider\EavValidationRules; |
| 12 | +use Magento\Customer\Model\Customer\DataProvider; |
| 13 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 14 | +use Magento\Customer\Model\Resource\Customer\CollectionFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class DataProviderTest |
| 18 | + * |
| 19 | + * Test for class \Magento\Customer\Model\Customer\DataProvider |
| 20 | + */ |
| 21 | +class DataProviderTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + const ATTRIBUTE_CODE = 'test-code'; |
| 24 | + const OPTIONS_RESULT = 'test-options'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Config|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $eavConfigMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $customerCollectionFactoryMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var EavValidationRules|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $eavValidationRulesMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * Set up |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $this->eavConfigMock = $this->getMockBuilder('Magento\Eav\Model\Config') |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->getMock(); |
| 51 | + $this->customerCollectionFactoryMock = $this->getMock( |
| 52 | + 'Magento\Customer\Model\Resource\Customer\CollectionFactory', |
| 53 | + ['create'], |
| 54 | + [], |
| 55 | + '', |
| 56 | + false |
| 57 | + ); |
| 58 | + $this->eavValidationRulesMock = $this |
| 59 | + ->getMockBuilder('Magento\Ui\DataProvider\EavValidationRules') |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Run test getAttributesMeta method |
| 66 | + * |
| 67 | + * @param array $expected |
| 68 | + * @return void |
| 69 | + * |
| 70 | + * @dataProvider getAttributesMetaDataProvider |
| 71 | + */ |
| 72 | + public function testGetAttributesMetaWithOptions(array $expected) |
| 73 | + { |
| 74 | + $helper = new ObjectManager($this); |
| 75 | + $dataProvider = $helper->getObject( |
| 76 | + '\Magento\Customer\Model\Customer\DataProvider', |
| 77 | + [ |
| 78 | + 'name' => 'test-name', |
| 79 | + 'primaryFieldName' => 'primary-field-name', |
| 80 | + 'requestFieldName' => 'request-field-name', |
| 81 | + 'eavValidationRules' => $this->eavValidationRulesMock, |
| 82 | + 'customerCollectionFactory' => $this->getCustomerCollectionFactoryMock(), |
| 83 | + 'eavConfig' => $this->getEavConfigMock() |
| 84 | + ] |
| 85 | + ); |
| 86 | + |
| 87 | + $meta = $dataProvider->getMeta(); |
| 88 | + $this->assertNotEmpty($meta); |
| 89 | + $this->assertEquals($expected, $meta); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Data provider for testGetAttributesMeta |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function getAttributesMetaDataProvider() |
| 98 | + { |
| 99 | + return [ |
| 100 | + [ |
| 101 | + 'expected' => [ |
| 102 | + 'customer' => [ |
| 103 | + 'fields' => [ |
| 104 | + self::ATTRIBUTE_CODE => [ |
| 105 | + 'dataType' => 'frontend_input', |
| 106 | + 'formElement' => 'frontend_input', |
| 107 | + 'options' => 'test-options', |
| 108 | + 'visible' => 'is_visible', |
| 109 | + 'required' => 'is_required', |
| 110 | + 'label' => 'frontend_label', |
| 111 | + 'sortOrder' => 'sort_order', |
| 112 | + 'notice' => 'note', |
| 113 | + 'default' => 'default_value', |
| 114 | + 'size' => 'multiline_count', |
| 115 | + ] |
| 116 | + ] |
| 117 | + ], |
| 118 | + 'address' => [ |
| 119 | + 'fields' => [ |
| 120 | + self::ATTRIBUTE_CODE => [ |
| 121 | + 'dataType' => 'frontend_input', |
| 122 | + 'formElement' => 'frontend_input', |
| 123 | + 'options' => 'test-options', |
| 124 | + 'visible' => 'is_visible', |
| 125 | + 'required' => 'is_required', |
| 126 | + 'label' => 'frontend_label', |
| 127 | + 'sortOrder' => 'sort_order', |
| 128 | + 'notice' => 'note', |
| 129 | + 'default' => 'default_value', |
| 130 | + 'size' => 'multiline_count', |
| 131 | + ] |
| 132 | + ] |
| 133 | + ] |
| 134 | + ] |
| 135 | + ] |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @return CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 141 | + */ |
| 142 | + protected function getCustomerCollectionFactoryMock() |
| 143 | + { |
| 144 | + $collectionMock = $this->getMockBuilder('Magento\Customer\Model\Resource\Customer\Collection') |
| 145 | + ->disableOriginalConstructor() |
| 146 | + ->getMock(); |
| 147 | + |
| 148 | + $collectionMock->expects($this->once()) |
| 149 | + ->method('addAttributeToSelect') |
| 150 | + ->with('*'); |
| 151 | + |
| 152 | + $this->customerCollectionFactoryMock->expects($this->once()) |
| 153 | + ->method('create') |
| 154 | + ->willReturn($collectionMock); |
| 155 | + |
| 156 | + return $this->customerCollectionFactoryMock; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @return Config|\PHPUnit_Framework_MockObject_MockObject |
| 161 | + */ |
| 162 | + protected function getEavConfigMock() |
| 163 | + { |
| 164 | + $this->eavConfigMock->expects($this->at(0)) |
| 165 | + ->method('getEntityType') |
| 166 | + ->with('customer') |
| 167 | + ->willReturn($this->getTypeCustomerMock()); |
| 168 | + $this->eavConfigMock->expects($this->at(1)) |
| 169 | + ->method('getEntityType') |
| 170 | + ->with('customer_address') |
| 171 | + ->willReturn($this->getTypeAddressMock()); |
| 172 | + |
| 173 | + return $this->eavConfigMock; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return Type|\PHPUnit_Framework_MockObject_MockObject |
| 178 | + */ |
| 179 | + protected function getTypeCustomerMock() |
| 180 | + { |
| 181 | + $typeCustomerMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Type') |
| 182 | + ->disableOriginalConstructor() |
| 183 | + ->getMock(); |
| 184 | + |
| 185 | + $typeCustomerMock->expects($this->once()) |
| 186 | + ->method('getAttributeCollection') |
| 187 | + ->willReturn($this->getAttributeMock()); |
| 188 | + |
| 189 | + return $typeCustomerMock; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @return Type|\PHPUnit_Framework_MockObject_MockObject |
| 194 | + */ |
| 195 | + protected function getTypeAddressMock() |
| 196 | + { |
| 197 | + $typeAddressMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Type') |
| 198 | + ->disableOriginalConstructor() |
| 199 | + ->getMock(); |
| 200 | + |
| 201 | + $typeAddressMock->expects($this->once()) |
| 202 | + ->method('getAttributeCollection') |
| 203 | + ->willReturn($this->getAttributeMock()); |
| 204 | + |
| 205 | + return $typeAddressMock; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[] |
| 210 | + */ |
| 211 | + protected function getAttributeMock() |
| 212 | + { |
| 213 | + $attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute') |
| 214 | + ->setMethods(['getAttributeCode', 'getDataUsingMethod', 'usesSource', 'getSource']) |
| 215 | + ->disableOriginalConstructor() |
| 216 | + ->getMockForAbstractClass(); |
| 217 | + $sourceMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Source\AbstractSource') |
| 218 | + ->disableOriginalConstructor() |
| 219 | + ->getMockForAbstractClass(); |
| 220 | + |
| 221 | + $sourceMock->expects($this->any()) |
| 222 | + ->method('getAllOptions') |
| 223 | + ->willReturn(self::OPTIONS_RESULT); |
| 224 | + |
| 225 | + $attributeMock->expects($this->once()) |
| 226 | + ->method('getAttributeCode') |
| 227 | + ->willReturn(self::ATTRIBUTE_CODE); |
| 228 | + |
| 229 | + $attributeMock->expects($this->any()) |
| 230 | + ->method('getDataUsingMethod') |
| 231 | + ->willReturnCallback( |
| 232 | + function ($origName) { |
| 233 | + return $origName; |
| 234 | + } |
| 235 | + ); |
| 236 | + $attributeMock->expects($this->any()) |
| 237 | + ->method('usesSource') |
| 238 | + ->willReturn(true); |
| 239 | + $attributeMock->expects($this->any()) |
| 240 | + ->method('getSource') |
| 241 | + ->willReturn($sourceMock); |
| 242 | + |
| 243 | + $this->eavValidationRulesMock->expects($this->any()) |
| 244 | + ->method('build') |
| 245 | + ->with($attributeMock, $this->logicalNot($this->isEmpty())); |
| 246 | + |
| 247 | + return [$attributeMock]; |
| 248 | + } |
| 249 | +} |
0 commit comments