@@ -46,40 +46,76 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
46
46
*/
47
47
private $ attributeTypes ;
48
48
49
+ /**
50
+ * @var Collection
51
+ */
52
+ private $ attributeCollection ;
53
+
49
54
/**
50
55
* @inheritdoc
51
56
*/
52
57
protected function setUp ()
53
58
{
54
59
$ this ->objectManager = Bootstrap::getObjectManager ();
55
60
$ this ->_model = $ this ->objectManager ->create (Customer::class);
61
+ $ this ->attributeCollection = $ this ->objectManager ->create (Collection::class);
56
62
}
57
63
58
64
/**
59
65
* Export "Customer Main File".
60
66
*
61
67
* @magentoDataFixture Magento/Customer/_files/import_export/customers.php
68
+ * @return void
62
69
*/
63
70
public function testExport ()
64
71
{
65
- /** @var Collection $collection */
66
- $ collection = $ this ->objectManager ->create (Collection::class);
67
- $ this ->initAttributeValues ($ collection );
68
- $ this ->initAttributeTypes ($ collection );
72
+ $ this ->processCustomerAttribute ();
73
+ $ expectedAttributes = $ this ->getExpectedAttributes ();
74
+ $ lines = $ this ->export ($ expectedAttributes );
75
+ $ this ->checkExportData ($ lines , $ expectedAttributes );
76
+ }
69
77
78
+ /**
79
+ * Return attributes which should be exported.
80
+ *
81
+ * @return array
82
+ */
83
+ private function getExpectedAttributes (): array
84
+ {
70
85
$ expectedAttributes = [];
71
86
/** @var Attribute $attribute */
72
- foreach ($ collection as $ attribute ) {
87
+ foreach ($ this -> attributeCollection as $ attribute ) {
73
88
$ expectedAttributes [] = $ attribute ->getAttributeCode ();
74
89
}
75
- $ expectedAttributes = array_diff ($ expectedAttributes , $ this ->_model ->getDisabledAttributes ());
76
90
77
- $ this ->_model ->setWriter ($ this ->objectManager ->get (Csv::class));
91
+ return array_diff ($ expectedAttributes , $ this ->_model ->getDisabledAttributes ());
92
+ }
93
+
94
+ /**
95
+ * Prepare Customer attribute.
96
+ *
97
+ * @return void
98
+ */
99
+ private function processCustomerAttribute (): void
100
+ {
101
+ $ this ->initAttributeValues ($ this ->attributeCollection );
102
+ $ this ->initAttributeTypes ($ this ->attributeCollection );
103
+ }
104
+
105
+ /**
106
+ * Export customer.
107
+ *
108
+ * @param array $expectedAttributes
109
+ * @return array
110
+ */
111
+ private function export (array $ expectedAttributes ): array
112
+ {
113
+ $ this ->_model ->setWriter ($ this ->objectManager ->create (Csv::class));
78
114
$ data = $ this ->_model ->export ();
115
+
79
116
$ this ->assertNotEmpty ($ data );
80
117
81
118
$ lines = $ this ->_csvToArray ($ data , 'email ' );
82
-
83
119
$ this ->assertEquals (
84
120
count ($ expectedAttributes ),
85
121
count (array_intersect ($ expectedAttributes , $ lines ['header ' ])),
@@ -88,8 +124,20 @@ public function testExport()
88
124
89
125
$ this ->assertNotEmpty ($ lines ['data ' ], 'No data was exported. ' );
90
126
127
+ return $ lines ;
128
+ }
129
+
130
+ /**
131
+ * Check that exported data is correct.
132
+ *
133
+ * @param array $lines
134
+ * @param array $expectedAttributes
135
+ * @return void
136
+ */
137
+ private function checkExportData (array $ lines , array $ expectedAttributes ): void
138
+ {
91
139
/** @var CustomerModel[] $customers */
92
- $ customers = $ this ->objectManager ->create (CustomerCollection::class)-> getItems () ;
140
+ $ customers = $ this ->objectManager ->create (CustomerCollection::class);
93
141
foreach ($ customers as $ customer ) {
94
142
$ data = $ this ->processCustomerData ($ customer , $ expectedAttributes );
95
143
$ exportData = $ lines ['data ' ][$ data ['email ' ]];
@@ -111,7 +159,7 @@ function (&$value) {
111
159
* Initialize attribute option values.
112
160
*
113
161
* @param Collection $attributeCollection
114
- * @return $this
162
+ * @return CustomerTest
115
163
*/
116
164
private function initAttributeValues (Collection $ attributeCollection ): CustomerTest
117
165
{
@@ -127,7 +175,7 @@ private function initAttributeValues(Collection $attributeCollection): CustomerT
127
175
* Initialize attribute types.
128
176
*
129
177
* @param \Magento\Customer\Model\ResourceModel\Attribute\Collection $attributeCollection
130
- * @return $this
178
+ * @return CustomerTest
131
179
*/
132
180
private function initAttributeTypes (Collection $ attributeCollection ): CustomerTest
133
181
{
@@ -184,7 +232,7 @@ private function isMultiselect(string $attributeCode): bool
184
232
*
185
233
* @param string $attributeCode
186
234
* @param int|string $valueId
187
- * @return mixed
235
+ * @return int|string|array
188
236
*/
189
237
private function getAttributeValueById (string $ attributeCode , $ valueId )
190
238
{
0 commit comments