Skip to content

Commit c2100db

Browse files
committed
[REFACTOR] Split the method to lower cyclomatic complexity
1 parent 3905580 commit c2100db

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CategoryLinkTest.php

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -134,53 +134,8 @@ public function testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affe
134134

135135
foreach ($affectedIds as $type => $ids) {
136136
$expectedResult = array_merge($expectedResult, $ids);
137-
138137
// Verify if the correct insert, update and/or delete actions are performed:
139-
switch ($type) {
140-
case 'insert':
141-
$this->connectionMock
142-
->expects($this->exactly(empty($ids) ? 0 : 1))
143-
->method('insertArray')
144-
->with(
145-
$this->anything(),
146-
$this->anything(),
147-
$this->callback(function ($data) use ($ids) {
148-
$foundIds = [];
149-
foreach ($data as $row) {
150-
$foundIds[] = $row['category_id'];
151-
}
152-
return $ids === $foundIds;
153-
})
154-
);
155-
break;
156-
case 'update':
157-
$this->connectionMock
158-
->expects($this->exactly(empty($ids) ? 0 : 1))
159-
->method('insertOnDuplicate')
160-
->with(
161-
$this->anything(),
162-
$this->callback(function ($data) use ($ids) {
163-
$foundIds = [];
164-
foreach ($data as $row) {
165-
$foundIds[] = $row['category_id'];
166-
}
167-
return $ids === $foundIds;
168-
})
169-
);
170-
break;
171-
case 'delete':
172-
$this->connectionMock
173-
->expects($this->exactly(empty($ids) ? 0 : 1))
174-
->method('delete')
175-
// Verify that the correct category ID's are touched:
176-
->with(
177-
$this->anything(),
178-
$this->callback(function ($data) use ($ids) {
179-
return array_values($data)[1] === $ids;
180-
})
181-
);
182-
break;
183-
}
138+
$this->setupExpectationsForConnection($type, $ids);
184139
}
185140

186141
$actualResult = $this->model->saveCategoryLinks($product, $newCategoryLinks);
@@ -273,4 +228,57 @@ public function getCategoryLinksDataProvider()
273228
]
274229
];
275230
}
231+
232+
/**
233+
* @param $type
234+
* @param $ids
235+
*/
236+
private function setupExpectationsForConnection($type, $ids): void
237+
{
238+
switch ($type) {
239+
case 'insert':
240+
$this->connectionMock
241+
->expects($this->exactly(empty($ids) ? 0 : 1))
242+
->method('insertArray')
243+
->with(
244+
$this->anything(),
245+
$this->anything(),
246+
$this->callback(function ($data) use ($ids) {
247+
$foundIds = [];
248+
foreach ($data as $row) {
249+
$foundIds[] = $row['category_id'];
250+
}
251+
return $ids === $foundIds;
252+
})
253+
);
254+
break;
255+
case 'update':
256+
$this->connectionMock
257+
->expects($this->exactly(empty($ids) ? 0 : 1))
258+
->method('insertOnDuplicate')
259+
->with(
260+
$this->anything(),
261+
$this->callback(function ($data) use ($ids) {
262+
$foundIds = [];
263+
foreach ($data as $row) {
264+
$foundIds[] = $row['category_id'];
265+
}
266+
return $ids === $foundIds;
267+
})
268+
);
269+
break;
270+
case 'delete':
271+
$this->connectionMock
272+
->expects($this->exactly(empty($ids) ? 0 : 1))
273+
->method('delete')
274+
// Verify that the correct category ID's are touched:
275+
->with(
276+
$this->anything(),
277+
$this->callback(function ($data) use ($ids) {
278+
return array_values($data)[1] === $ids;
279+
})
280+
);
281+
break;
282+
}
283+
}
276284
}

0 commit comments

Comments
 (0)