Skip to content

Commit

Permalink
[Bug]: Error using "As input quantity value" mapper (#345)
Browse files Browse the repository at this point in the history
* Fix: remove use

* Fix: stan

* change to use InputQuantityValue

* improve unit usage and output

* Apply php-cs-fixer changes

* Update InputQuantityValue.php

* try to see if it fixes stan

* retest

* fix tests

* fix test due Unit::getId always retrun typecasted string

* fix null issue with getByAbbreviation

* add bc layer for getValue

---------

Co-authored-by: JiaJia Ji <[email protected]>
Co-authored-by: kingjia90 <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent a3218a7 commit cac31ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
23 changes: 16 additions & 7 deletions src/Mapping/Operator/Factory/InputQuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@

use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Bundle\DataImporterBundle\Mapping\Type\TransformationDataTypeService;
use Pimcore\Model\DataObject\Data\QuantityValue;
use Pimcore\Model\DataObject\QuantityValue\Unit;

class InputQuantityValue extends QuantityValue
{
/**
* @param mixed $inputData
* @param bool $dryRun
*
* @return QuantityValue
* @return \Pimcore\Model\DataObject\Data\InputQuantityValue
*/
public function process($inputData, bool $dryRun = false)
{
return new \Pimcore\Model\DataObject\Data\QuantityValue(
$unit = isset($inputData[1]) ? Unit::getByAbbreviation($inputData[1]) : null;

return new \Pimcore\Model\DataObject\Data\InputQuantityValue(
$inputData[0] ?? null,
$inputData[1] ?? null
$unit
);
}

Expand All @@ -46,7 +48,12 @@ public function process($inputData, bool $dryRun = false)
public function evaluateReturnType(string $inputType, int $index = null): string
{
if ($inputType !== TransformationDataTypeService::DEFAULT_ARRAY) {
throw new InvalidConfigurationException(sprintf("Unsupported input type '%s' for input quantity value operator at transformation position %s", $inputType, $index));
throw new InvalidConfigurationException(
sprintf(
"Unsupported input type '%s' for input quantity value operator at transformation position %s",
$inputType,
$index)
);
}

return TransformationDataTypeService::INPUT_QUANTITY_VALUE;
Expand All @@ -59,8 +66,10 @@ public function evaluateReturnType(string $inputType, int $index = null): string
*/
public function generateResultPreview($inputData)
{
if ($inputData instanceof \Pimcore\Model\DataObject\Data\QuantityValue) {
return 'InputQuantityValue: ' . $inputData->getValue() . ' ' . ($inputData->getUnit() ? $inputData->getUnit()->getAbbreviation() : '');
if ($inputData instanceof \Pimcore\Model\DataObject\Data\InputQuantityValue) {
return 'InputQuantityValue: ' .
$inputData->getValue() .
($inputData->getUnit() ? ' ['.$inputData->getUnit()->getAbbreviation().']' : '');
}

return $inputData;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Operator/Factory/QuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setSettings(array $settings): void
* @param mixed $inputData
* @param bool $dryRun
*
* @return \Pimcore\Model\DataObject\Data\QuantityValue|null
* @return \Pimcore\Model\DataObject\Data\AbstractQuantityValue|null
*/
public function process($inputData, bool $dryRun = false)
{
Expand Down
18 changes: 13 additions & 5 deletions tests/unit/FactoryOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Model\DataObject\Data\Hotspotimage;
use Pimcore\Model\DataObject\Data\ImageGallery;
use Pimcore\Model\DataObject\Data\QuantityValue;
use Pimcore\Model\DataObject\Data\InputQuantityValue as ModelInputQuantityValue;
use Pimcore\Model\DataObject\QuantityValue\Unit;
use Pimcore\Tests\Support\Util\TestHelper;
use Codeception\Test;
Expand Down Expand Up @@ -226,19 +227,26 @@ public function testInputQuantityValue() {
$inputQuantityValue = $this->tester->grabService(InputQuantityValue::class);

$result = $inputQuantityValue->process(['12', 'm']);
$this->assertInstanceOf(QuantityValue::class, $result);
$this->assertInstanceOf(ModelInputQuantityValue::class, $result);
$this->assertEquals('12', $result->getValue());
$this->assertEquals('m', $result->getUnitId());
$this->assertEquals('Meter', $result->getUnit()->getLongname());

$result = $inputQuantityValue->process(['12']);
$this->assertInstanceOf(QuantityValue::class, $result);
$this->assertInstanceOf(ModelInputQuantityValue::class, $result);
$this->assertEquals('12', $result->getValue());
$this->assertNull($result->getUnitId());
$this->assertEquals('', $result->getUnitId());

$result = $inputQuantityValue->process([null, 'm']);
$this->assertInstanceOf(QuantityValue::class, $result);
$this->assertNull($result->getValue());
$this->assertInstanceOf(ModelInputQuantityValue::class, $result);

// In Pimcore 10, value is typecasted to string, in 11, it's nullable
// TODO: Remove assertEquals once Pimcore 10 support is dropped
if (!is_null($result->getValue())) {
$this->assertEquals('', $result->getValue());
}else{
$this->assertNull($result->getValue());
}
$this->assertEquals('m', $result->getUnitId());

$preview = $inputQuantityValue->generateResultPreview($result);
Expand Down

0 comments on commit cac31ed

Please sign in to comment.