Skip to content

Commit a1eb0cb

Browse files
committed
MAGETWO-96906: [MAGETWO-94346] PR stabilization
- Fix unit tests;
1 parent eb7eb48 commit a1eb0cb

File tree

2 files changed

+156
-35
lines changed

2 files changed

+156
-35
lines changed

app/code/Magento/Customer/Test/Unit/Model/Address/DataProviderTest.php

Lines changed: 156 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Customer\Test\Unit\Model\Address;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Address\DataProvider;
1112
use Magento\Customer\Model\AttributeMetadataResolver;
1213
use Magento\Customer\Model\FileUploaderDataResolver;
1314
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
@@ -17,12 +18,17 @@
1718
use Magento\Customer\Api\Data\CustomerInterface;
1819
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1920
use Magento\Customer\Model\Address as AddressModel;
21+
use Magento\Ui\Component\Form\Element\Multiline;
22+
use Magento\Ui\Component\Form\Field;
23+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
2024

2125
/**
2226
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2327
*/
2428
class DataProviderTest extends \PHPUnit\Framework\TestCase
2529
{
30+
private const ATTRIBUTE_CODE = 'street';
31+
2632
/**
2733
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
2834
*/
@@ -53,11 +59,6 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
5359
*/
5460
private $context;
5561

56-
/**
57-
* @var Type|\PHPUnit_Framework_MockObject_MockObject
58-
*/
59-
private $entityType;
60-
6162
/**
6263
* @var AddressModel|\PHPUnit_Framework_MockObject_MockObject
6364
*/
@@ -74,7 +75,7 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
7475
private $attributeMetadataResolver;
7576

7677
/**
77-
* @var \Magento\Customer\Model\Address\DataProvider
78+
* @var DataProvider
7879
*/
7980
private $model;
8081

@@ -102,26 +103,68 @@ protected function setUp()
102103
$this->eavConfig = $this->getMockBuilder(Config::class)
103104
->disableOriginalConstructor()
104105
->getMock();
105-
$this->entityType = $this->getMockBuilder(Type::class)
106-
->disableOriginalConstructor()
107-
->getMock();
108-
$this->entityType->expects($this->once())
109-
->method('getAttributeCollection')
110-
->willReturn([]);
111106
$this->eavConfig->expects($this->once())
112107
->method('getEntityType')
113-
->willReturn($this->entityType);
108+
->with('customer_address')
109+
->willReturn($this->getTypeAddressMock([]));
114110
$this->customer = $this->getMockForAbstractClass(CustomerInterface::class);
115111
$this->address = $this->getMockBuilder(AddressModel::class)
116112
->disableOriginalConstructor()
117113
->getMock();
118-
114+
$this->attributeMetadataResolver->expects($this->at(0))
115+
->method('getAttributesMeta')
116+
->willReturn(
117+
[
118+
'arguments' => [
119+
'data' => [
120+
'config' => [
121+
'dataType' => Multiline::NAME,
122+
'formElement' => 'frontend_input',
123+
'options' => 'test-options',
124+
'visible' => null,
125+
'required' => 'is_required',
126+
'label' => __('Street'),
127+
'sortOrder' => 'sort_order',
128+
'default' => 'default_value',
129+
'size' => 'multiline_count',
130+
'componentType' => Field::NAME,
131+
],
132+
],
133+
],
134+
]
135+
);
136+
$this->attributeMetadataResolver->expects($this->at(1))
137+
->method('getAttributesMeta')
138+
->willReturn(
139+
[
140+
'arguments' => [
141+
'data' => [
142+
'config' => [
143+
'dataType' => 'frontend_input',
144+
'formElement' => 'frontend_input',
145+
'visible' => null,
146+
'required' => 'is_required',
147+
'label' => __('frontend_label'),
148+
'sortOrder' => 'sort_order',
149+
'default' => 'default_value',
150+
'size' => 'multiline_count',
151+
'componentType' => Field::NAME,
152+
'prefer' => 'toggle',
153+
'valueMap' => [
154+
'true' => 1,
155+
'false' => 0,
156+
],
157+
],
158+
],
159+
],
160+
]
161+
);
119162
$this->model = $objectManagerHelper->getObject(
120-
\Magento\Customer\Model\Address\DataProvider::class,
163+
DataProvider::class,
121164
[
122-
'',
123-
'',
124-
'',
165+
'name' => 'test-name',
166+
'primaryFieldName' => 'primary-field-name',
167+
'requestFieldName' => 'request-field-name',
125168
'addressCollectionFactory' => $this->addressCollectionFactory,
126169
'customerRepository' => $this->customerRepository,
127170
'eavConfig' => $this->eavConfig,
@@ -168,8 +211,10 @@ public function testGetDefaultData(): void
168211
public function testGetData(): void
169212
{
170213
$expectedData = [
171-
'3' => [
172-
'parent_id' => "1",
214+
'1' => [
215+
'parent_id' => '1',
216+
'default_billing' => '1',
217+
'default_shipping' => '1',
173218
'firstname' => 'John',
174219
'lastname' => 'Doe',
175220
'street' => [
@@ -197,16 +242,16 @@ public function testGetData(): void
197242

198243
$this->address->expects($this->once())
199244
->method('getEntityId')
200-
->willReturn('3');
245+
->willReturn('1');
201246
$this->address->expects($this->once())
202247
->method('load')
203-
->with("3")
248+
->with('1')
204249
->willReturnSelf();
205250
$this->address->expects($this->once())
206251
->method('getData')
207252
->willReturn([
208-
'parent_id' => "1",
209-
'firstname' => "John",
253+
'parent_id' => '1',
254+
'firstname' => 'John',
210255
'lastname' => 'Doe',
211256
'street' => "42000 Ave W 55 Cedar City\nApt. 33"
212257
]);
@@ -216,4 +261,91 @@ public function testGetData(): void
216261

217262
$this->assertEquals($expectedData, $this->model->getData());
218263
}
264+
265+
/**
266+
* Get customer address type mock
267+
*
268+
* @param array $customerAttributes
269+
* @return Type|\PHPUnit_Framework_MockObject_MockObject
270+
*/
271+
protected function getTypeAddressMock($customerAttributes = [])
272+
{
273+
$typeAddressMock = $this->getMockBuilder(Type::class)
274+
->disableOriginalConstructor()
275+
->getMock();
276+
$attributesCollection = !empty($customerAttributes) ? $customerAttributes : $this->getAttributeMock();
277+
foreach ($attributesCollection as $attribute) {
278+
$attribute->expects($this->any())
279+
->method('getEntityType')
280+
->willReturn($typeAddressMock);
281+
}
282+
283+
$typeAddressMock->expects($this->once())
284+
->method('getAttributeCollection')
285+
->willReturn($attributesCollection);
286+
287+
return $typeAddressMock;
288+
}
289+
290+
/**
291+
* Get attribute mock
292+
*
293+
* @param array $options
294+
* @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
295+
*/
296+
protected function getAttributeMock($options = []): array
297+
{
298+
$attributeMock = $this->getMockBuilder(AbstractAttribute::class)
299+
->setMethods(
300+
[
301+
'getAttributeCode',
302+
'getDataUsingMethod',
303+
'getFrontendInput',
304+
'getIsVisible',
305+
'getSource',
306+
'getIsUserDefined',
307+
'getUsedInForms',
308+
'getEntityType',
309+
]
310+
)
311+
->disableOriginalConstructor()
312+
->getMockForAbstractClass();
313+
314+
$attributeCode = self::ATTRIBUTE_CODE;
315+
if (isset($options[self::ATTRIBUTE_CODE]['specific_code_prefix'])) {
316+
$attributeCode .= $options[self::ATTRIBUTE_CODE]['specific_code_prefix'];
317+
}
318+
319+
$attributeMock->expects($this->exactly(2))
320+
->method('getAttributeCode')
321+
->willReturn($attributeCode);
322+
323+
$attributeBooleanMock = $this->getMockBuilder(AbstractAttribute::class)
324+
->setMethods(
325+
[
326+
'getAttributeCode',
327+
'getDataUsingMethod',
328+
'getFrontendInput',
329+
'getIsVisible',
330+
'getIsUserDefined',
331+
'getUsedInForms',
332+
'getSource',
333+
'getEntityType',
334+
]
335+
)
336+
->disableOriginalConstructor()
337+
->getMockForAbstractClass();
338+
339+
$booleanAttributeCode = 'test-code-boolean';
340+
if (isset($options['test-code-boolean']['specific_code_prefix'])) {
341+
$booleanAttributeCode .= $options['test-code-boolean']['specific_code_prefix'];
342+
}
343+
344+
$attributeBooleanMock->expects($this->exactly(2))
345+
->method('getAttributeCode')
346+
->willReturn($booleanAttributeCode);
347+
348+
$mocks = [$attributeMock, $attributeBooleanMock];
349+
return $mocks;
350+
}
219351
}

app/code/Magento/Ui/Test/Unit/Component/Form/FieldsetTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,4 @@ public function testGetComponentName()
5555
{
5656
$this->assertEquals(self::NAME, $this->fieldset->getComponentName());
5757
}
58-
59-
/**
60-
* Run test for canShow() method
61-
*
62-
* @return void
63-
*
64-
*/
65-
public function testCanShow()
66-
{
67-
$this->assertEquals(true, $this->fieldset->canShow());
68-
}
6958
}

0 commit comments

Comments
 (0)