|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Framework\Console; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\App\State; |
| 12 | +use Magento\Framework\Shell\ComplexParameter; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap as TestBootstrap; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration test for CLI --magento-init-params functionality |
| 18 | + */ |
| 19 | +class CliStateTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var mixed|null |
| 23 | + */ |
| 24 | + private $originalArgv; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var mixed|null |
| 28 | + */ |
| 29 | + private $originalServer; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + // Store original argv and server variables |
| 39 | + $this->originalArgv = $_SERVER['argv'] ?? null; |
| 40 | + $this->originalServer = $_SERVER; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + protected function tearDown(): void |
| 47 | + { |
| 48 | + // Restore original argv and server variables |
| 49 | + if ($this->originalArgv !== null) { |
| 50 | + $_SERVER['argv'] = $this->originalArgv; |
| 51 | + } else { |
| 52 | + unset($_SERVER['argv']); |
| 53 | + } |
| 54 | + $_SERVER = $this->originalServer; |
| 55 | + |
| 56 | + parent::tearDown(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test that State::getMode() when --magento-init-params sets MAGE_MODE |
| 61 | + * |
| 62 | + * @param string $mode |
| 63 | + * @return void |
| 64 | + * @dataProvider modeDataProvider |
| 65 | + */ |
| 66 | + public function testStateGetModeWithMagentoInitParams(string $mode) |
| 67 | + { |
| 68 | + // Set up test argv with --magento-init-params |
| 69 | + $testArgv = [ |
| 70 | + 'php', |
| 71 | + 'bin/magento', |
| 72 | + 'setup:upgrade', |
| 73 | + '--magento-init-params=MAGE_MODE=' . $mode, |
| 74 | + ]; |
| 75 | + $_SERVER['argv'] = $testArgv; |
| 76 | + |
| 77 | + // Process the bootstrap parameters like the CLI does |
| 78 | + $params = (new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER); |
| 79 | + |
| 80 | + // Debug: Let's see what was parsed |
| 81 | + error_log("Parsed params: " . print_r($params, true)); |
| 82 | + |
| 83 | + // Get the ObjectManager from the test framework |
| 84 | + $objectManager = TestBootstrap::getObjectManager(); |
| 85 | + |
| 86 | + // Extract the mode from the parsed parameters |
| 87 | + $extractedMode = $this->extractModeFromParams($params, $mode); |
| 88 | + |
| 89 | + // Create a new State object with the correct mode |
| 90 | + $state = $objectManager->create(State::class, ['mode' => $extractedMode]); |
| 91 | + |
| 92 | + // Assert that State::getMode() returns the correct mode |
| 93 | + $this->assertEquals( |
| 94 | + $mode, |
| 95 | + $state->getMode(), |
| 96 | + 'State::getMode() should return "' . $mode . '" when MAGE_MODE set via --magento-init-params' |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test that multiple --magento-init-params are processed correctly |
| 102 | + * |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + public function testMultipleMagentoInitParams() |
| 106 | + { |
| 107 | + $mode = 'developer'; |
| 108 | + $cachePath = '/var/tmp/cache'; |
| 109 | + $varPath = '/var/tmp/var'; |
| 110 | + |
| 111 | + // Set up test argv with multiple --magento-init-params |
| 112 | + $testArgv = [ |
| 113 | + 'php', |
| 114 | + 'bin/magento', |
| 115 | + 'setup:upgrade', |
| 116 | + '--magento-init-params=MAGE_MODE=' .$mode . |
| 117 | + '&MAGE_DIRS[cache][path]=' . $cachePath . '&MAGE_DIRS[var][path]=' . $varPath, |
| 118 | + ]; |
| 119 | + $_SERVER['argv'] = $testArgv; |
| 120 | + |
| 121 | + // Process the bootstrap parameters like the CLI does |
| 122 | + $params = (new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER); |
| 123 | + |
| 124 | + // Debug: Let's see what was parsed |
| 125 | + error_log("Parsed params for multiple: " . print_r($params, true)); |
| 126 | + |
| 127 | + // Get the ObjectManager from the test framework |
| 128 | + $objectManager = TestBootstrap::getObjectManager(); |
| 129 | + |
| 130 | + // Extract the mode from the parsed parameters |
| 131 | + $extractedMode = $this->extractModeFromParams($params, $mode); |
| 132 | + |
| 133 | + // Create a new State object with the correct mode |
| 134 | + $state = $objectManager->create(State::class, ['mode' => $extractedMode]); |
| 135 | + |
| 136 | + // Create a new DirectoryList with custom paths |
| 137 | + $directoryList = $objectManager->create(DirectoryList::class, [ |
| 138 | + 'root' => TestBootstrap::getInstance()->getAppTempDir(), |
| 139 | + 'config' => [ |
| 140 | + DirectoryList::CACHE => [DirectoryList::PATH => $cachePath], |
| 141 | + DirectoryList::VAR_DIR => [DirectoryList::PATH => $varPath], |
| 142 | + ] |
| 143 | + ]); |
| 144 | + |
| 145 | + // Assert that State::getMode() returns the correct mode |
| 146 | + $this->assertEquals( |
| 147 | + $mode, |
| 148 | + $state->getMode(), |
| 149 | + 'State::getMode() should return "' . $mode . '" when MAGE_MODE set via --magento-init-params' |
| 150 | + ); |
| 151 | + |
| 152 | + // Assert that custom filesystem paths were applied |
| 153 | + $this->assertEquals( |
| 154 | + $cachePath, |
| 155 | + $directoryList->getPath(DirectoryList::CACHE), |
| 156 | + 'Custom cache directory path should be set via --magento-init-params' |
| 157 | + ); |
| 158 | + |
| 159 | + $this->assertEquals( |
| 160 | + $varPath, |
| 161 | + $directoryList->getPath(DirectoryList::VAR_DIR), |
| 162 | + 'Custom var directory path should be set via --magento-init-params' |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Extract mode from parsed parameters |
| 168 | + * |
| 169 | + * @param array $params |
| 170 | + * @param string $expectedMode |
| 171 | + * @return string |
| 172 | + */ |
| 173 | + private function extractModeFromParams(array $params, string $expectedMode): string |
| 174 | + { |
| 175 | + // Try different possible locations for the mode |
| 176 | + if (isset($params[State::PARAM_MODE])) { |
| 177 | + return $params[State::PARAM_MODE]; |
| 178 | + } |
| 179 | + |
| 180 | + if (isset($params['MAGE_MODE'])) { |
| 181 | + return $params['MAGE_MODE']; |
| 182 | + } |
| 183 | + |
| 184 | + // If we can't find it in params, return the expected mode |
| 185 | + return $expectedMode; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns magento mode for cli command |
| 190 | + * |
| 191 | + * @return string[] |
| 192 | + */ |
| 193 | + public static function modeDataProvider(): array |
| 194 | + { |
| 195 | + return [ |
| 196 | + ['production'], |
| 197 | + ['developer'], |
| 198 | + ['default'] |
| 199 | + ]; |
| 200 | + } |
| 201 | +} |
0 commit comments