Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

GraphQL-167: added support for @magentoApiConfigFixture #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\TestFramework\Annotation;

/**
* Processor for magentoApiConfigFixture annotation
*/
class ApiConfigFixture extends \Magento\TestFramework\Annotation\ConfigFixture
{
/**
* @var string
*/
protected $annotation = 'magentoApiConfigFixture';

/**
* Reassign configuration data whenever application is reset
*/
public function initStoreAfter()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Application configuration object. Used to access configuration when application is installed.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\TestFramework\App;

use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\ObjectManager;

/**
* @inheritdoc
*/
class MutableScopeConfig implements MutableScopeConfigInterface
{
/**
* @var Config
*/
private $testAppConfig;

/**
* @inheritdoc
*/
public function isSetFlag($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
{
return $this->getTestAppConfig()->isSetFlag($path, $scopeType, $scopeCode);
}

/**
* @inheritdoc
*/
public function getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
{
return $this->getTestAppConfig()->getValue($path, $scopeType, $scopeCode);
}

/**
* @inheritdoc
*/
public function setValue(
$path,
$value,
$scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$this->persistConfig($path, $value, $scopeType, $scopeCode);
return $this->getTestAppConfig()->setValue($path, $value, $scopeType, $scopeCode);
}

/**
* Clean app config cache
*
* @param string|null $type
* @return void
*/
public function clean()
{
$this->getTestAppConfig()->clean();
}

/**
* Retrieve test app config instance
*
* @return \Magento\TestFramework\App\Config
*/
private function getTestAppConfig()
{
if (!$this->testAppConfig) {
$this->testAppConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class);
}

return $this->testAppConfig;
}

/**
* Persist config in database
*
* @param string $path
* @param string $value
* @param string $scopeType
* @param string|null $scopeCode
*/
private function persistConfig($path, $value, $scopeType, $scopeCode): void
{
$pathParts = explode('/', $path);
$store = '';
if ($scopeType === \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
if ($scopeCode !== null) {
$store = ObjectManager::getInstance()
->get(\Magento\Store\Api\StoreRepositoryInterface::class)
->get($scopeCode)
->getId();
} else {
$store = ObjectManager::getInstance()
->get(\Magento\Store\Model\StoreManagerInterface::class)
->getStore()
->getId();
}
}
$configData = [
'section' => $pathParts[0],
'website' => '',
'store' => $store,
'groups' => [
$pathParts[1] => [
'fields' => [
$pathParts[2] => [
'value' => $value
]
]
]
]
];
ObjectManager::getInstance()
->get(\Magento\Config\Model\Config\Factory::class)
->create(['data' => $configData])
->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class WebapiDocBlock extends \Magento\TestFramework\Bootstrap\DocBlock
{
/**
* Get list of subscribers. In addition, register <b>magentoApiDataFixture</b> annotation processing.
* Get list of subscribers. In addition, register magentoApiDataFixture and magentoApiConfigFixture
* annotation processors
*
* @param \Magento\TestFramework\Application $application
* @return array
Expand All @@ -19,6 +20,7 @@ protected function _getSubscribers(\Magento\TestFramework\Application $applicati
{
$subscribers = parent::_getSubscribers($application);
$subscribers[] = new \Magento\TestFramework\Annotation\ApiDataFixture($this->_fixturesBaseDir);
$subscribers[] = new \Magento\TestFramework\Annotation\ApiConfigFixture($this->_fixturesBaseDir);
return $subscribers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ConfigFixture
*/
private $_storeConfigValues = [];

/**
* @var string
*/
protected $annotation = 'magentoConfigFixture';

/**
* Retrieve configuration node value
*
Expand Down Expand Up @@ -104,10 +109,10 @@ protected function _setConfigValue($configPath, $value, $storeCode = false)
protected function _assignConfigData(\PHPUnit\Framework\TestCase $test)
{
$annotations = $test->getAnnotations();
if (!isset($annotations['method']['magentoConfigFixture'])) {
if (!isset($annotations['method'][$this->annotation])) {
return;
}
foreach ($annotations['method']['magentoConfigFixture'] as $configPathAndValue) {
foreach ($annotations['method'][$this->annotation] as $configPathAndValue) {
if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) {
/* Store-scoped config value */
$storeCode = $matches[0] != 'current' ? $matches[0] : null;
Expand Down