Skip to content

Commit

Permalink
MAGETWO-81245: All mapping methods for criterias must be arrays.
Browse files Browse the repository at this point in the history
Fixed test and set InvalidArgumentException for wrong cases.

(cherry picked from commit b44335b)
  • Loading branch information
kirmorozov committed Nov 6, 2017
1 parent 0f1c2f1 commit 4122d76
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StockItemCriteria extends AbstractCriteria implements \Magento\CatalogInve
public function __construct($mapper = '')
{
$this->mapperInterfaceName = $mapper ?: \Magento\CatalogInventory\Model\ResourceModel\Stock\Item\StockItemCriteriaMapper::class;
$this->data['initial_condition'] = true;
$this->data['initial_condition'] = [true];
}

/**
Expand All @@ -38,7 +38,7 @@ public function setStockStatus($storeId = null)
*/
public function setStockFilter($stock)
{
$this->data['stock_filter'] = $stock;
$this->data['stock_filter'] = [$stock];
return true;
}

Expand All @@ -47,7 +47,7 @@ public function setStockFilter($stock)
*/
public function setScopeFilter($scope)
{
$this->data['website_filter'] = $scope;
$this->data['website_filter'] = [$scope];
return true;
}

Expand All @@ -56,7 +56,7 @@ public function setScopeFilter($scope)
*/
public function setProductsFilter($products)
{
$this->data['products_filter'] = $products;
$this->data['products_filter'] = [$products];
return true;
}

Expand All @@ -65,7 +65,7 @@ public function setProductsFilter($products)
*/
public function setManagedFilter($isStockManagedInConfig)
{
$this->data['managed_filter'] = $isStockManagedInConfig;
$this->data['managed_filter'] = [$isStockManagedInConfig];
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ class StockStatusCriteria extends AbstractCriteria implements \Magento\CatalogIn
public function __construct($mapper = '')
{
$this->mapperInterfaceName = $mapper ?: \Magento\CatalogInventory\Model\ResourceModel\Stock\Status\StockStatusCriteriaMapper::class;
$this->data['initial_condition'] = true;
$this->data['initial_condition'] = [true];
}

/**
* @inheritdoc
*/
public function setScopeFilter($scope)
{
$this->data['website_filter'] = $scope;
$this->data['website_filter'] = [$scope];
}

/**
* @inheritdoc
*/
public function setProductsFilter($products)
{
$this->data['products_filter'] = $products;
$this->data['products_filter'] = [$products];
}

/**
* @inheritdoc
*/
public function setQtyFilter($qty)
{
$this->data['qty_filter'] = $qty;
$this->data['qty_filter'] = [$qty];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function __construct($mapper = '')
{
$this->mapperInterfaceName =
$mapper ?: \Magento\CatalogInventory\Model\ResourceModel\Stock\StockCriteriaMapper::class;
$this->data['initial_condition'] = true;
$this->data['initial_condition'] = [true];
}

/**
* @inheritdoc
*/
public function setScopeFilter($scope)
{
$this->data['website_filter'] = $scope;
$this->data['website_filter'] = [$scope];
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/DB/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function map(CriteriaInterface $criteria)
$mapperMethod = 'map' . $camelCaseKey;
if (method_exists($this, $mapperMethod)) {
if (!is_array($value)) {
$value = [$value];
throw new \InvalidArgumentException('Wrong type of argument, expecting array for '. $mapperMethod);
}
call_user_func_array([$this, $mapperMethod], $value);
}
Expand Down
47 changes: 45 additions & 2 deletions lib/internal/Magento/Framework/DB/Test/Unit/AbstractMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,49 @@ public function testMap(array $mapperMethods, array $criteriaParts)
$this->assertEquals($this->selectMock, $mapper->map($criteriaMock));
}


public function testMapException()
{
$mapperMethods = [
'my-test-value1' => 'mapMyMapperMethodOne'
];

$criteriaParts = [
'my_mapper_method_one' => 'my-test-value1'
];
/** @var \Magento\Framework\DB\AbstractMapper|\PHPUnit_Framework_MockObject_MockObject $mapper */
$mapper = $this->getMockForAbstractClass(
\Magento\Framework\DB\AbstractMapper::class,
[
'logger' => $this->loggerMock,
'fetchStrategy' => $this->fetchStrategyMock,
'objectFactory' => $this->objectFactoryMock,
'mapperFactory' => $this->mapperFactoryMock,
'select' => $this->selectMock
],
'',
true,
true,
true,
$mapperMethods
);
$criteriaMock = $this->getMockForAbstractClass(
\Magento\Framework\Api\CriteriaInterface::class,
[],
'',
false,
true,
true,
['toArray']
);
$criteriaMock->expects($this->once())
->method('toArray')
->will($this->returnValue($criteriaParts));
$this->expectException(\InvalidArgumentException::class);
$mapper->map($criteriaMock);

}

/**
* Run test addExpressionFieldToSelect method
*
Expand Down Expand Up @@ -254,8 +297,8 @@ public function dataProviderMap()
'my-test-value2' => 'mapMyMapperMethodTwo',
],
'criteriaParts' => [
'my_mapper_method_one' => 'my-test-value1',
'my_mapper_method_two' => 'my-test-value2',
'my_mapper_method_one' => ['my-test-value1'],
'my_mapper_method_two' => ['my-test-value2'],
],
]
];
Expand Down

0 comments on commit 4122d76

Please sign in to comment.