-
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.
ENGCOM-2445: 120 implement store configs coverage #126
- Merge Pull Request magento/graphql-ce#126 from VitaliyBoyko/graphql-ce:120-Implement-storeConfigs-coverage - Merged commits: 1. e1f53d4 2. 5229cd6 3. af4b258 4. 3b64955 5. b5a2a98 6. e85a6e5 7. 8f3c6e9
- Loading branch information
Showing
5 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigDataProvider.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,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StoreGraphQl\Model\Resolver\Store; | ||
|
||
use Magento\Store\Api\Data\StoreConfigInterface; | ||
use Magento\Store\Api\StoreConfigManagerInterface; | ||
use Magento\Store\Api\StoreRepositoryInterface; | ||
use Magento\Store\Api\StoreResolverInterface; | ||
|
||
/** | ||
* StoreConfig field data provider, used for GraphQL request processing. | ||
*/ | ||
class StoreConfigDataProvider | ||
{ | ||
/** | ||
* @var StoreConfigManagerInterface | ||
*/ | ||
private $storeConfigManager; | ||
|
||
/** | ||
* @var StoreResolverInterface | ||
*/ | ||
private $storeResolver; | ||
|
||
/** | ||
* @var StoreRepositoryInterface | ||
*/ | ||
private $storeRepository; | ||
|
||
/** | ||
* @param StoreConfigManagerInterface $storeConfigManager | ||
* @param StoreResolverInterface $storeResolver | ||
* @param StoreRepositoryInterface $storeRepository | ||
*/ | ||
public function __construct( | ||
StoreConfigManagerInterface $storeConfigManager, | ||
StoreResolverInterface $storeResolver, | ||
StoreRepositoryInterface $storeRepository | ||
) { | ||
$this->storeConfigManager = $storeConfigManager; | ||
$this->storeResolver = $storeResolver; | ||
$this->storeRepository = $storeRepository; | ||
} | ||
|
||
/** | ||
* Get store config for current store | ||
* | ||
* @return array | ||
*/ | ||
public function getStoreConfig() : array | ||
{ | ||
$storeId = $this->storeResolver->getCurrentStoreId(); | ||
$store = $this->storeRepository->getById($storeId); | ||
$storeConfig = current($this->storeConfigManager->getStoreConfigs([$store->getCode()])); | ||
|
||
return $this->hidrateStoreConfig($storeConfig); | ||
} | ||
|
||
/** | ||
* Transform StoreConfig object to in array format | ||
* | ||
* @param StoreConfigInterface $storeConfig | ||
* @return array | ||
*/ | ||
private function hidrateStoreConfig($storeConfig): array | ||
{ | ||
/** @var StoreConfigInterface $storeConfig */ | ||
$storeConfigData = [ | ||
'id' => $storeConfig->getId(), | ||
'code' => $storeConfig->getCode(), | ||
'website_id' => $storeConfig->getWebsiteId(), | ||
'locale' => $storeConfig->getLocale(), | ||
'base_currency_code' => $storeConfig->getBaseCurrencyCode(), | ||
'default_display_currency_code' => $storeConfig->getDefaultDisplayCurrencyCode(), | ||
'timezone' => $storeConfig->getTimezone(), | ||
'weight_unit' => $storeConfig->getWeightUnit(), | ||
'base_url' => $storeConfig->getBaseUrl(), | ||
'base_link_url' => $storeConfig->getBaseLinkUrl(), | ||
'base_static_url' => $storeConfig->getSecureBaseStaticUrl(), | ||
'base_media_url' => $storeConfig->getBaseMediaUrl(), | ||
'secure_base_url' => $storeConfig->getSecureBaseUrl(), | ||
'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(), | ||
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(), | ||
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl() | ||
]; | ||
|
||
return $storeConfigData; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigResolver.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,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StoreGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider; | ||
|
||
/** | ||
* StoreConfig page field resolver, used for GraphQL request processing. | ||
*/ | ||
class StoreConfigResolver implements ResolverInterface | ||
{ | ||
/** | ||
* @var StoreConfigDataProvider | ||
*/ | ||
private $storeConfigDataProvider; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param StoreConfigDataProvider $storeConfigsDataProvider | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
StoreConfigDataProvider $storeConfigsDataProvider, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->valueFactory = $valueFactory; | ||
$this->storeConfigDataProvider = $storeConfigsDataProvider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
$storeConfigData = $this->storeConfigDataProvider->getStoreConfig(); | ||
|
||
$result = function () use ($storeConfigData) { | ||
return !empty($storeConfigData) ? $storeConfigData : []; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
} |
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
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
93 changes: 93 additions & 0 deletions
93
dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.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,93 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Store; | ||
|
||
use Magento\Store\Api\Data\StoreConfigInterface; | ||
use Magento\Store\Api\StoreConfigManagerInterface; | ||
use Magento\Store\Api\StoreRepositoryInterface; | ||
use Magento\Store\Api\StoreResolverInterface; | ||
use Magento\TestFramework\ObjectManager; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test the GraphQL endpoint's StoreConfigs query | ||
*/ | ||
class StoreConfigResolverTest extends GraphQlAbstract | ||
{ | ||
|
||
/** @var ObjectManager */ | ||
private $objectManager; | ||
|
||
protected function setUp() | ||
{ | ||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Store/_files/store.php | ||
*/ | ||
public function testGetStoreConfig() | ||
{ | ||
/** @var StoreConfigManagerInterface $storeConfigsManager */ | ||
$storeConfigsManager = $this->objectManager->get(StoreConfigManagerInterface::class); | ||
/** @var StoreResolverInterface $storeResolver */ | ||
$storeResolver = $this->objectManager->get(StoreResolverInterface::class); | ||
/** @var StoreRepositoryInterface $storeRepository */ | ||
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); | ||
$storeId = $storeResolver->getCurrentStoreId(); | ||
$store = $storeRepository->getById($storeId); | ||
/** @var StoreConfigInterface $storeConfig */ | ||
$storeConfig = current($storeConfigsManager->getStoreConfigs([$store->getCode()])); | ||
$query | ||
= <<<QUERY | ||
{ | ||
storeConfig{ | ||
id, | ||
code, | ||
website_id, | ||
locale, | ||
base_currency_code, | ||
default_display_currency_code, | ||
timezone, | ||
weight_unit, | ||
base_url, | ||
base_link_url, | ||
base_static_url, | ||
base_media_url, | ||
secure_base_url, | ||
secure_base_link_url, | ||
secure_base_static_url, | ||
secure_base_media_url | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query); | ||
$this->assertArrayHasKey('storeConfig', $response); | ||
$this->assertEquals($storeConfig->getId(), $response['storeConfig']['id']); | ||
$this->assertEquals($storeConfig->getCode(), $response['storeConfig']['code']); | ||
$this->assertEquals($storeConfig->getLocale(), $response['storeConfig']['locale']); | ||
$this->assertEquals($storeConfig->getBaseCurrencyCode(), $response['storeConfig']['base_currency_code']); | ||
$this->assertEquals( | ||
$storeConfig->getDefaultDisplayCurrencyCode(), | ||
$response['storeConfig']['default_display_currency_code'] | ||
); | ||
$this->assertEquals($storeConfig->getTimezone(), $response['storeConfig']['timezone']); | ||
$this->assertEquals($storeConfig->getWeightUnit(), $response['storeConfig']['weight_unit']); | ||
$this->assertEquals($storeConfig->getBaseUrl(), $response['storeConfig']['base_url']); | ||
$this->assertEquals($storeConfig->getBaseLinkUrl(), $response['storeConfig']['base_link_url']); | ||
$this->assertEquals($storeConfig->getBaseStaticUrl(), $response['storeConfig']['base_static_url']); | ||
$this->assertEquals($storeConfig->getBaseMediaUrl(), $response['storeConfig']['base_media_url']); | ||
$this->assertEquals($storeConfig->getSecureBaseUrl(), $response['storeConfig']['secure_base_url']); | ||
$this->assertEquals($storeConfig->getSecureBaseLinkUrl(), $response['storeConfig']['secure_base_link_url']); | ||
$this->assertEquals( | ||
$storeConfig->getSecureBaseStaticUrl(), | ||
$response['storeConfig']['secure_base_static_url'] | ||
); | ||
$this->assertEquals($storeConfig->getSecureBaseMediaUrl(), $response['storeConfig']['secure_base_media_url']); | ||
} | ||
} |