Skip to content

Commit

Permalink
ENGCOM-2445: 120 implement store configs coverage #126
Browse files Browse the repository at this point in the history
 - 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
magento-engcom-team committed Jul 23, 2018
2 parents f01227c + 8f3c6e9 commit b42fd5f
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 1 deletion.
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;
}
}
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);
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/StoreGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*"
"magento/framework": "*",
"magento/module-store": "*"
},
"suggest": {
"magento/module-graph-ql": "*",
Expand Down
22 changes: 22 additions & 0 deletions app/code/Magento/StoreGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
type Query {
storeConfig : StoreConfig @resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\StoreConfigResolver") @doc(description: "The store config query")
}

type Website @doc(description: "The type contains information about a website") {
id : Int @doc(description: "The ID number assigned to the website")
Expand All @@ -9,3 +12,22 @@ type Website @doc(description: "The type contains information about a website")
default_group_id : String @doc(description: "The default group id that the website has")
is_default : Boolean @doc(description: "Specifies if this is the default website")
}

type StoreConfig @doc(description: "The type contains information about a store config") {
id : Int @doc(description: "The ID number assigned to the store")
code : String @doc(description: "A code assigned to the store to identify it")
website_id : Int @doc(description: "The ID number assigned to the website store belongs")
locale : String @doc(description: "Store locale")
base_currency_code : String @doc(description: "Base currency code")
default_display_currency_code : String @doc(description: "Default display currency code")
timezone : String @doc(description: "Timezone of the store")
weight_unit : String @doc(description: "The unit of weight")
base_url : String @doc(description: "Base URL for the store")
base_link_url : String @doc(description: "Base link URL for the store")
base_static_url : String @doc(description: "Base static URL for the store")
base_media_url : String @doc(description: "Base media URL for the store")
secure_base_url : String @doc(description: "Secure base URL for the store")
secure_base_link_url : String @doc(description: "Secure base link URL for the store")
secure_base_static_url : String @doc(description: "Secure base static URL for the store")
secure_base_media_url : String @doc(description: "Secure base media URL for the store")
}
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']);
}
}

0 comments on commit b42fd5f

Please sign in to comment.