AdminConfigFixture.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Implementation of the @magentoAdminConfigFixture DocBlock annotation
  8. */
  9. namespace Magento\TestFramework\Annotation;
  10. /**
  11. * Handler for applying magentoAdminConfig annotation
  12. *
  13. * @package Magento\TestFramework\Annotation
  14. */
  15. class AdminConfigFixture
  16. {
  17. /**
  18. * Test instance that is available between 'startTest' and 'stopTest' events
  19. *
  20. * @var \PHPUnit\Framework\TestCase
  21. */
  22. protected $_currentTest;
  23. /**
  24. * Original values for global configuration options that need to be restored
  25. *
  26. * @var array
  27. */
  28. private $_configValues = [];
  29. /**
  30. * Retrieve configuration node value
  31. *
  32. * @param string $configPath
  33. * @return string
  34. */
  35. protected function _getConfigValue($configPath)
  36. {
  37. return \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  38. \Magento\Framework\App\Config\MutableScopeConfigInterface::class
  39. )->getValue(
  40. $configPath
  41. );
  42. }
  43. /**
  44. * Assign configuration node value
  45. *
  46. * @param string $configPath
  47. * @param string $value
  48. */
  49. protected function _setConfigValue($configPath, $value)
  50. {
  51. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  52. \Magento\Framework\App\Config\MutableScopeConfigInterface::class
  53. )->setValue(
  54. $configPath,
  55. $value
  56. );
  57. }
  58. /**
  59. * Assign required config values and save original ones
  60. *
  61. * @param \PHPUnit\Framework\TestCase $test
  62. */
  63. protected function _assignConfigData(\PHPUnit\Framework\TestCase $test)
  64. {
  65. $annotations = $test->getAnnotations();
  66. if (!isset($annotations['method']['magentoAdminConfigFixture'])) {
  67. return;
  68. }
  69. foreach ($annotations['method']['magentoAdminConfigFixture'] as $configPathAndValue) {
  70. list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
  71. $originalValue = $this->_getConfigValue($configPath);
  72. $this->_configValues[$configPath] = $originalValue;
  73. $this->_setConfigValue($configPath, $requiredValue);
  74. }
  75. }
  76. /**
  77. * Restore original values for changed config options
  78. */
  79. protected function _restoreConfigData()
  80. {
  81. foreach ($this->_configValues as $configPath => $originalValue) {
  82. $this->_setConfigValue($configPath, $originalValue);
  83. }
  84. $this->_configValues = [];
  85. }
  86. /**
  87. * Handler for 'startTest' event
  88. *
  89. * @param \PHPUnit\Framework\TestCase $test
  90. */
  91. public function startTest(\PHPUnit\Framework\TestCase $test)
  92. {
  93. $this->_currentTest = $test;
  94. $this->_assignConfigData($test);
  95. }
  96. /**
  97. * Handler for 'endTest' event
  98. *
  99. * @param \PHPUnit\Framework\TestCase $test
  100. *
  101. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  102. */
  103. public function endTest(\PHPUnit\Framework\TestCase $test)
  104. {
  105. $this->_currentTest = null;
  106. $this->_restoreConfigData();
  107. }
  108. /**
  109. * Reassign configuration data whenever application is reset
  110. */
  111. public function initStoreAfter()
  112. {
  113. /* process events triggered from within a test only */
  114. if ($this->_currentTest) {
  115. $this->_assignConfigData($this->_currentTest);
  116. }
  117. }
  118. }