Skip to content

Instantly share code, notes, and snippets.

@naydav
Created September 8, 2018 17:40
Show Gist options
  • Save naydav/184c2ae214a0fdb8863e11896aa7e4d9 to your computer and use it in GitHub Desktop.
Save naydav/184c2ae214a0fdb8863e11896aa7e4d9 to your computer and use it in GitHub Desktop.
From 4ff6fa558a6333d6828ddd0719da07e708709e87 Mon Sep 17 00:00:00 2001
From: Igor Melnykov <melnykov@adobe.com>
Date: Wed, 1 Aug 2018 22:18:58 -0500
Subject: [PATCH] Add support for @magentoApiConfigFixture in web api tests
---
.../Annotation/ApiConfigFixture.php | 27 ++++
.../TestFramework/App/MutableScopeConfig.php | 125 ++++++++++++++++++
.../Bootstrap/WebapiDocBlock.php | 4 +-
.../Annotation/ConfigFixture.php | 9 +-
4 files changed, 162 insertions(+), 3 deletions(-)
create mode 100644 dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php
create mode 100644 dev/tests/api-functional/framework/Magento/TestFramework/App/MutableScopeConfig.php
diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php b/dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php
new file mode 100644
index 000000000000..0f87c274b78b
--- /dev/null
+++ b/dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiConfigFixture.php
@@ -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()
+ {
+ }
+}
diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/App/MutableScopeConfig.php b/dev/tests/api-functional/framework/Magento/TestFramework/App/MutableScopeConfig.php
new file mode 100644
index 000000000000..efcb5be34e59
--- /dev/null
+++ b/dev/tests/api-functional/framework/Magento/TestFramework/App/MutableScopeConfig.php
@@ -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();
+ }
+}
diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php b/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php
index 514d8e5344d5..336456de0ed2 100644
--- a/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php
+++ b/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php
@@ -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
@@ -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;
}
}
diff --git a/dev/tests/integration/framework/Magento/TestFramework/Annotation/ConfigFixture.php b/dev/tests/integration/framework/Magento/TestFramework/Annotation/ConfigFixture.php
index dcac794703a5..1ec0b3eac6fc 100644
--- a/dev/tests/integration/framework/Magento/TestFramework/Annotation/ConfigFixture.php
+++ b/dev/tests/integration/framework/Magento/TestFramework/Annotation/ConfigFixture.php
@@ -39,6 +39,11 @@ class ConfigFixture
*/
private $_storeConfigValues = [];
+ /**
+ * @var string
+ */
+ protected $annotation = 'magentoConfigFixture';
+
/**
* Retrieve configuration node value
*
@@ -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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment