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