Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RowParserTest.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class RowParserTest extends \PHPUnit\Framework\TestCase
18 {
22  private $columnResolverMock;
23 
27  private $rowParser;
28 
32  private $locationDirectoryMock;
33 
37  protected function setUp()
38  {
39  $this->locationDirectoryMock = $this->getMockBuilder(LocationDirectory::class)
40  ->setMethods(['hasCountryId', 'getCountryId', 'hasRegionId', 'getRegionId'])
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->columnResolverMock = $this->getMockBuilder(ColumnResolver::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->rowParser = new RowParser(
47  $this->locationDirectoryMock
48  );
49  }
50 
54  public function testGetColumns()
55  {
56  $columns = $this->rowParser->getColumns();
57  $this->assertTrue(is_array($columns), 'Columns should be array, ' . gettype($columns) . ' given');
58  $this->assertNotEmpty($columns);
59  }
60 
64  public function testParse()
65  {
66  $expectedResult = [
67  'website_id' => 58,
68  'dest_country_id' => '0',
69  'dest_region_id' => 0,
70  'dest_zip' => '*',
71  'condition_name' => 'condition_short_name',
72  'condition_value' => 40.0,
73  'price' => 350.0,
74  ];
75  $rowData = ['a', 'b', 'c', 'd', 'e'];
76  $rowNumber = 120;
77  $websiteId = 58;
78  $conditionShortName = 'condition_short_name';
79  $conditionFullName = 'condition_full_name';
80  $columnValueMap = [
81  [ColumnResolver::COLUMN_COUNTRY, $rowData, '*'],
82  [ColumnResolver::COLUMN_REGION, $rowData, '*'],
83  [ColumnResolver::COLUMN_ZIP, $rowData, ''],
84  [$conditionFullName, $rowData, 40],
85  [ColumnResolver::COLUMN_PRICE, $rowData, 350],
86  ];
87  $result = $this->parse(
88  $rowData,
89  $conditionFullName,
90  $rowNumber,
91  $websiteId,
92  $conditionShortName,
93  $columnValueMap
94  );
95  $this->assertEquals($expectedResult, $result);
96  }
97 
107  public function testParseWithException(array $rowData, $conditionFullName, array $columnValueMap, $expectedMessage)
108  {
109  $rowNumber = 120;
110  $websiteId = 58;
111  $conditionShortName = 'condition_short_name';
112  $actualMessage = null;
113  $exception = null;
114  try {
115  $this->parse(
116  $rowData,
117  $conditionFullName,
118  $rowNumber,
119  $websiteId,
120  $conditionShortName,
121  $columnValueMap
122  );
123  } catch (\Exception $e) {
124  $actualMessage = $e->getMessage();
125  $exception = $e;
126  }
127  $this->assertEquals($expectedMessage, $actualMessage);
128  throw $exception;
129  }
130 
135  {
136  $rowData = ['a', 'b', 'c', 'd', 'e'];
137  $conditionFullName = 'condition_full_name';
138  return [
139  [
140  $rowData,
141  $conditionFullName,
142  [
143  [ColumnResolver::COLUMN_COUNTRY, $rowData, 'XX'],
144  [ColumnResolver::COLUMN_REGION, $rowData, '*'],
145  [ColumnResolver::COLUMN_ZIP, $rowData, ''],
146  [$conditionFullName, $rowData, 40],
147  [ColumnResolver::COLUMN_PRICE, $rowData, 350],
148  ],
149  'The "XX" country in row number "120" is incorrect. Verify the country and try again.',
150  ],
151  [
152  $rowData,
153  $conditionFullName,
154  [
155  [ColumnResolver::COLUMN_COUNTRY, $rowData, '*'],
156  [ColumnResolver::COLUMN_REGION, $rowData, 'AA'],
157  [ColumnResolver::COLUMN_ZIP, $rowData, ''],
158  [$conditionFullName, $rowData, 40],
159  [ColumnResolver::COLUMN_PRICE, $rowData, 350],
160  ],
161  'The "AA" region or state in row number "120" is incorrect. Verify the region or state and try again.',
162  ],
163  [
164  $rowData,
165  $conditionFullName,
166  [
167  [ColumnResolver::COLUMN_COUNTRY, $rowData, '*'],
168  [ColumnResolver::COLUMN_REGION, $rowData, '*'],
169  [ColumnResolver::COLUMN_ZIP, $rowData, ''],
170  [$conditionFullName, $rowData, 'QQQ'],
171  [ColumnResolver::COLUMN_PRICE, $rowData, 350],
172  ],
173  'Please correct condition_full_name "QQQ" in the Row #120.',
174  ],
175  [
176  $rowData,
177  $conditionFullName,
178  [
179  [ColumnResolver::COLUMN_COUNTRY, $rowData, '*'],
180  [ColumnResolver::COLUMN_REGION, $rowData, '*'],
181  [ColumnResolver::COLUMN_ZIP, $rowData, ''],
182  [$conditionFullName, $rowData, 40],
183  [ColumnResolver::COLUMN_PRICE, $rowData, 'BBB'],
184  ],
185  'The "BBB" shipping price in row number "120" is incorrect. Verify the shipping price and try again.',
186  ],
187  ];
188  }
189 
199  private function parse($rowData, $conditionFullName, $rowNumber, $websiteId, $conditionShortName, $columnValueMap)
200  {
201  $this->columnResolverMock->expects($this->any())
202  ->method('getColumnValue')
203  ->willReturnMap($columnValueMap);
204  $result = $this->rowParser->parse(
205  $rowData,
206  $rowNumber,
207  $websiteId,
208  $conditionShortName,
209  $conditionFullName,
210  $this->columnResolverMock
211  );
212  return $result;
213  }
214 }
$columns
Definition: default.phtml:15
testParseWithException(array $rowData, $conditionFullName, array $columnValueMap, $expectedMessage)