Skip to content

Commit 896cba3

Browse files
a.laurowskiAleksander Laurowski
authored andcommitted
FFWEB-2455 check configurable product has variants
1 parent e016cc2 commit 896cba3

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
- Search Result Page, Category Page
55
- add Products per Page configurartion which allows user to define custom configuration without any change in code
66

7+
### Fix
8+
- Export
9+
- fix `Model\Export\Catalog\ProductType\ConfigurableDataProvider::getChildren` throws an SQL syntax error if configurable product has no variants assigned
10+
711
## [v2.5.0] - 2022.03.04
812
- Import
913
- fix error while push import feed using CLI after generate it for API version 7.x

src/Model/Export/Catalog/ProductType/ConfigurableDataProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ private function getOptions(Product $product): array
100100
*/
101101
private function getChildren(Product $product): array
102102
{
103+
$childrenIds = $this->productType->getChildrenIds($product->getId());
104+
//if $childrenIds is empty the entity_id filter will thrown an SQL syntax error
105+
if (empty($childrenIds[0])) {
106+
return [];
107+
}
103108
return $this->productRepository
104-
->getList($this->builder->addFilter('entity_id', $this->productType->getChildrenIds($this->product->getId()), 'in')
109+
->getList($this->builder->addFilter('entity_id', $childrenIds, 'in')
105110
->create())
106111
->getItems();
107112
}

src/Test/TestHelper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\Factfinder\Test;
6+
7+
use ReflectionClass;
8+
9+
class TestHelper
10+
{
11+
public static function invokeMethod(&$object, $methodName, array $parameters = [])
12+
{
13+
$reflection = new ReflectionClass(get_class($object));
14+
$method = $reflection->getMethod($methodName);
15+
$method->setAccessible(true);
16+
return $method->invokeArgs($object, $parameters);
17+
}
18+
}

src/Test/Unit/Model/Export/Catalog/ProductType/ConfigurableDataProviderTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace Omikron\Factfinder\Model\Export\Catalog\ProductType;
66

7-
use Magento\Catalog\Api\Data\ProductInterface;
87
use Magento\Catalog\Api\ProductRepositoryInterface;
98
use Magento\Catalog\Model\Product;
109
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProductType;
1110
use Magento\Framework\Api\SearchCriteriaBuilder;
1211
use Omikron\Factfinder\Api\Filter\FilterInterface;
1312
use Omikron\Factfinder\Model\Export\Catalog\Entity\ProductVariationFactory;
1413
use Omikron\Factfinder\Model\Formatter\NumberFormatter;
14+
use Omikron\Factfinder\Test\TestHelper;
1515
use PHPUnit\Framework\TestCase;
1616

1717
class ConfigurableDataProviderTest extends TestCase
@@ -24,7 +24,7 @@ class ConfigurableDataProviderTest extends TestCase
2424
*/
2525
public function test_will_return_string_on_string_value()
2626
{
27-
$getValueOrEmptyStringMethod = $this->invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', ['test']);
27+
$getValueOrEmptyStringMethod = TestHelper::invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', ['test']);
2828
$this->assertEquals('test', $getValueOrEmptyStringMethod);
2929
}
3030

@@ -33,7 +33,7 @@ public function test_will_return_string_on_string_value()
3333
*/
3434
public function test_will_return_empty_string_on_null_value()
3535
{
36-
$getValueOrEmptyStringMethod = $this->invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [null]);
36+
$getValueOrEmptyStringMethod = TestHelper::invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [null]);
3737
$this->assertEquals('', $getValueOrEmptyStringMethod);
3838
}
3939

@@ -42,7 +42,7 @@ public function test_will_return_empty_string_on_null_value()
4242
*/
4343
public function test_will_return_empty_string_on_bool_value()
4444
{
45-
$getValueOrEmptyStringMethod = $this->invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [false]);
45+
$getValueOrEmptyStringMethod = TestHelper::invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [false]);
4646
$this->assertEquals('', $getValueOrEmptyStringMethod);
4747
}
4848

@@ -51,16 +51,20 @@ public function test_will_return_empty_string_on_bool_value()
5151
*/
5252
public function test_will_return_empty_string_on_array_value()
5353
{
54-
$getValueOrEmptyStringMethod = $this->invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [[]]);
54+
$getValueOrEmptyStringMethod = TestHelper::invokeMethod($this->configurableDataProvider, 'getValueOrEmptyString', [[]]);
5555
$this->assertEquals('', $getValueOrEmptyStringMethod);
5656
}
5757

58-
public function invokeMethod(&$object, $methodName, array $parameters = array())
58+
/**
59+
* @covers ConfigurableDataProvider::getChildren
60+
*/
61+
public function test_will_no_throw_error_if_there_is_no_chlidren_ids()
5962
{
60-
$reflection = new \ReflectionClass(get_class($object));
61-
$method = $reflection->getMethod($methodName);
62-
$method->setAccessible(true);
63-
return $method->invokeArgs($object, $parameters);
63+
$this->productMock->method('getId')->willReturn('1');
64+
$this->configurableProductTypeMock->method('getChildrenIds')->with('1')
65+
->willReturn([0 => []]);
66+
$variants = TestHelper::invokeMethod($this->configurableDataProvider, 'getChildren', [$this->productMock]);
67+
$this->assertEquals([], $variants);
6468
}
6569

6670
protected function setUp(): void

0 commit comments

Comments
 (0)