Skip to content

magento/magento2#: Remove a redundant getMappedNums from a loop #27939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
23 changes: 10 additions & 13 deletions lib/internal/Magento/Framework/GraphQl/Query/EnumLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Framework\GraphQl\Config\Element\Enum;
use Magento\Framework\GraphQl\ConfigInterface;
use Magento\Framework\GraphQl\Schema\Type\Enum\DataMapperInterface;
use Magento\Framework\Phrase;

/**
* Processor that looks up definition data of an enum to lookup and convert data as it's specified in the schema.
Expand All @@ -28,6 +27,8 @@ class EnumLookup
private $enumDataMapper;

/**
* EnumLookup constructor.
*
* @param ConfigInterface $typeConfig
* @param DataMapperInterface $enumDataMapper
*/
Expand All @@ -43,23 +44,19 @@ public function __construct(ConfigInterface $typeConfig, DataMapperInterface $en
* @param string $enumName
* @param string $fieldValue
* @return string
* @throws \Magento\Framework\Exception\RuntimeException
*/
public function getEnumValueFromField(string $enumName, string $fieldValue) : string
{
$priceViewEnum = $this->typeConfig->getConfigElement($enumName);
if ($priceViewEnum instanceof Enum) {
foreach ($priceViewEnum->getValues() as $enumItem) {
$mappedValues = $this->enumDataMapper->getMappedEnums($enumName);
if (isset($mappedValues[$enumItem->getName()]) && $mappedValues[$enumItem->getName()] == $fieldValue) {
return $enumItem->getValue();
}
/** @var Enum $enumObject */
$enumObject = $this->typeConfig->getConfigElement($enumName);
$mappedValues = $this->enumDataMapper->getMappedEnums($enumName);

foreach ($enumObject->getValues() as $enumItem) {
if (isset($mappedValues[$enumItem->getName()]) && $mappedValues[$enumItem->getName()] == $fieldValue) {
return $enumItem->getValue();
}
} else {
throw new \Magento\Framework\Exception\RuntimeException(
new Phrase('Enum type "%1" not defined', [$enumName])
);
}

return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\GraphQl\Test\Unit\Query;

use Magento\Framework\Config\DataInterface;
use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
use Magento\Framework\GraphQl\Config\Element\Enum;
use Magento\Framework\GraphQl\Config\Element\EnumValue;
use Magento\Framework\GraphQl\ConfigInterface;
use Magento\Framework\GraphQl\Query\EnumLookup;
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
use Magento\Framework\GraphQl\Schema\Type\Enum\DataMapperInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Unit test for \Magento\Framework\GraphQl\Query\EnumLookup
*/
class EnumLookupTest extends TestCase
{
private const ENUM_NAME = 'SubscriptionStatusesEnum';

/**
* @var DataInterface|MockObject
*/
private $configDataMock;

/**
* @var ConfigElementFactoryInterface|MockObject
*/
private $configElementFactoryMock;

/**
* @var DataMapperInterface|MockObject
*/
private $enumDataMapperMock;

/**
* Testable Object
*
* @var EnumLookup
*/
private $enumLookup;

/**
* @var Enum|MockObject
*/
private $enumMock;

/**
* Object Manager Instance
*
* @var ObjectManager
*/
private $objectManager;

/**
* @var QueryFields|MockObject
*/
private $queryFieldsMock;

/**
* @var ConfigInterface|MockObject
*/
private $typeConfigMock;

/**
* @var array
*/
private $map = [];

/**
* @var array
*/
private $values = [];

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);

$this->map = [
self::ENUM_NAME => [
'subscribed' => '1',
'not_active' => '2',
'unsubscribed' => '3',
'unconfirmed' => '4',
]
];

$this->values = [
'NOT_ACTIVE' => new EnumValue('not_active', 'NOT_ACTIVE'),
'SUBSCRIBED' => new EnumValue('subscribed', 'SUBSCRIBED'),
'UNSUBSCRIBED' => new EnumValue('unsubscribed', 'UNSUBSCRIBED'),
'UNCONFIRMED' => new EnumValue('unconfirmed', 'UNCONFIRMED'),
];

$this->enumMock = $this->getMockBuilder(Enum::class)
->setConstructorArgs(
[
self::ENUM_NAME,
$this->values,
'Subscription statuses',
]
)
->getMock();

$this->enumDataMapperMock = $this->getMockBuilder(DataMapperInterface::class)
->setConstructorArgs($this->map)
->getMock();

$this->configDataMock = $this->getMockBuilder(DataInterface::class)
->getMock();
$this->configElementFactoryMock = $this->getMockBuilder(ConfigElementFactoryInterface::class)
->getMock();
$this->queryFieldsMock = $this->getMockBuilder(QueryFields::class)
->getMock();

$this->typeConfigMock = $this->getMockBuilder(ConfigInterface::class)
->setConstructorArgs(
[
$this->configDataMock,
$this->configElementFactoryMock,
$this->queryFieldsMock,
]
)
->getMock();

$this->enumLookup = $this->objectManager->getObject(
EnumLookup::class,
[
'typeConfig' => $this->typeConfigMock,
'enumDataMapper' => $this->enumDataMapperMock,
]
);
}

public function testGetEnumValueFromField()
{
$enumName = self::ENUM_NAME;
$fieldValue = '1';

$this->enumDataMapperMock
->expects($this->once())
->method('getMappedEnums')
->willReturn($this->map[$enumName]);

$this->typeConfigMock
->expects($this->once())
->method('getConfigElement')
->willReturn($this->enumMock);

$this->enumMock
->expects($this->once())
->method('getValues')
->willReturn($this->values);

$this->assertEquals(
'SUBSCRIBED',
$this->enumLookup->getEnumValueFromField($enumName, $fieldValue)
);
}
}