-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento/magento2#: Remove a redundant getMappedNums from a loop
- Loading branch information
1 parent
ed3f42b
commit 1c87648
Showing
2 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/EnumLookupTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |