Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCustomerTest.php
Go to the documentation of this file.
1 <?php
11 
13 
15 {
21  protected $_model;
22 
28  protected $_websites = [1 => 'website1', 2 => 'website2'];
29 
35  protected $_customers = [
36  ['id' => 1, 'email' => '[email protected]', 'website_id' => 1],
37  ['id' => 2, 'email' => '[email protected]', 'website_id' => 2],
38  ];
39 
45  protected $_availableBehaviors = [
48  ];
49 
50  protected function setUp()
51  {
52  parent::setUp();
53 
54  $this->_model = $this->_getModelMock();
55  }
56 
57  protected function tearDown()
58  {
59  unset($this->_model);
60 
61  parent::tearDown();
62  }
63 
69  protected function _getModelMock()
70  {
71  $customerCollection = new \Magento\Framework\Data\Collection(
72  $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
73  );
74  foreach ($this->_customers as $customer) {
75  $customerCollection->addItem(new \Magento\Framework\DataObject($customer));
76  }
77 
78  $modelMock = $this->getMockBuilder(\Magento\CustomerImportExport\Model\Import\AbstractCustomer::class)
79  ->disableOriginalConstructor()
80  ->setMethods(
81  [
82  'getErrorAggregator',
83  '_getCustomerCollection',
84  '_validateRowForUpdate',
85  '_validateRowForDelete'
86  ]
87  )->getMockForAbstractClass();
88  $modelMock->method('getErrorAggregator')->willReturn($this->getErrorAggregatorObject());
89 
90  $property = new \ReflectionProperty($modelMock, '_websiteCodeToId');
91  $property->setAccessible(true);
92  $property->setValue($modelMock, array_flip($this->_websites));
93 
94  $property = new \ReflectionProperty($modelMock, '_availableBehaviors');
95  $property->setAccessible(true);
96  $property->setValue($modelMock, $this->_availableBehaviors);
97 
98  $modelMock->expects($this->any())
99  ->method('_getCustomerCollection')
100  ->will($this->returnValue($customerCollection));
101 
102  return $modelMock;
103  }
104 
110  public function checkUniqueKeyDataProvider()
111  {
112  return [
113  'valid' => [
114  '$rowData' => include __DIR__ . '/_files/row_data_abstract_valid.php',
115  '$errors' => [],
116  '$isValid' => true,
117  ],
118  'no website' => [
119  '$rowData' => include __DIR__ . '/_files/row_data_abstract_no_website.php',
120  '$errors' => [
123  ],
124  ],
125  ],
126  'empty website' => [
127  '$rowData' => include __DIR__ . '/_files/row_data_abstract_empty_website.php',
128  '$errors' => [
131  ],
132  ],
133  ],
134  'no email' => [
135  '$rowData' => include __DIR__ . '/_files/row_data_abstract_no_email.php',
136  '$errors' => [
139  ],
140  ],
141  ],
142  'empty email' => [
143  '$rowData' => include __DIR__ . '/_files/row_data_abstract_empty_email.php',
144  '$errors' => [
147  ],
148  ],
149  ],
150  'invalid email' => [
151  '$rowData' => include __DIR__ . '/_files/row_data_abstract_invalid_email.php',
152  '$errors' => [
155  ],
156  ],
157  ],
158  'invalid website' => [
159  '$rowData' => include __DIR__ . '/_files/row_data_abstract_invalid_website.php',
160  '$errors' => [
163  ],
164  ],
165  ]
166  ];
167  }
168 
177  public function testCheckUniqueKey(array $rowData, array $errors, $isValid = false)
178  {
179  $checkUniqueKey = new \ReflectionMethod(
180  \Magento\CustomerImportExport\Model\Import\AbstractCustomer::class,
181  '_checkUniqueKey'
182  );
183  $checkUniqueKey->setAccessible(true);
184 
185  if ($isValid) {
186  $this->assertTrue($checkUniqueKey->invoke($this->_model, $rowData, 0));
187  } else {
188  $this->assertFalse($checkUniqueKey->invoke($this->_model, $rowData, 0));
189  }
190  }
191 
192  public function testValidateRowForUpdate()
193  {
194  // _validateRowForUpdate should be called only once
195  $this->_model->expects($this->once())->method('_validateRowForUpdate');
196 
197  $this->assertAttributeEquals(0, '_processedEntitiesCount', $this->_model);
198 
199  // update action
200  $this->_model->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE]);
201  $this->_clearValidatedRows();
202 
203  $this->assertAttributeEquals([], '_validatedRows', $this->_model);
204  $this->assertTrue($this->_model->validateRow([], 1));
205  $this->assertAttributeEquals([1 => true], '_validatedRows', $this->_model);
206  $this->assertAttributeEquals(1, '_processedEntitiesCount', $this->_model);
207  $this->assertTrue($this->_model->validateRow([], 1));
208  }
209 
210  public function testValidateRowForDelete()
211  {
212  // _validateRowForDelete should be called only once
213  $this->_model->expects($this->once())->method('_validateRowForDelete');
214 
215  // delete action
216  $this->_model->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE]);
217  $this->_clearValidatedRows();
218 
219  $this->assertAttributeEquals([], '_validatedRows', $this->_model);
220  $this->assertTrue($this->_model->validateRow([], 2));
221  $this->assertAttributeEquals([2 => true], '_validatedRows', $this->_model);
222  $this->assertAttributeEquals(1, '_processedEntitiesCount', $this->_model);
223  $this->assertTrue($this->_model->validateRow([], 2));
224  }
225 
229  protected function _clearValidatedRows()
230  {
231  // clear array
232  $validatedRows = new \ReflectionProperty(
233  \Magento\CustomerImportExport\Model\Import\AbstractCustomer::class,
234  '_validatedRows'
235  );
236  $validatedRows->setAccessible(true);
237  $validatedRows->setValue($this->_model, []);
238  $validatedRows->setAccessible(false);
239 
240  // reset counter
241  $entitiesCount = new \ReflectionProperty(
242  \Magento\CustomerImportExport\Model\Import\AbstractCustomer::class,
243  '_processedEntitiesCount'
244  );
245  $entitiesCount->setAccessible(true);
246  $entitiesCount->setValue($this->_model, 0);
247  $entitiesCount->setAccessible(false);
248  }
249 }
$customer
Definition: customers.php:11
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$errors
Definition: overview.phtml:9