Skip to content

Commit 2d0f806

Browse files
authored
Merge pull request #9945 from magento-gl/spartans_pr_14072025
[Spartans] Bug Fix Deliver
2 parents 22526f1 + 233c9ae commit 2d0f806

File tree

1 file changed

+63
-30
lines changed

1 file changed

+63
-30
lines changed

dev/tests/integration/testsuite/Magento/Framework/Console/CliStateTest.php

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
namespace Magento\Framework\Console;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\App\ObjectManager;
1211
use Magento\Framework\App\State;
12+
use Magento\Framework\Shell\ComplexParameter;
13+
use Magento\TestFramework\Helper\Bootstrap as TestBootstrap;
1314
use PHPUnit\Framework\TestCase;
1415

1516
/**
@@ -22,29 +23,35 @@ class CliStateTest extends TestCase
2223
*/
2324
private $originalArgv;
2425

26+
/**
27+
* @var mixed|null
28+
*/
29+
private $originalServer;
30+
2531
/**
2632
* @inheritDoc
2733
*/
2834
protected function setUp(): void
2935
{
3036
parent::setUp();
3137

32-
// Store original argv
38+
// Store original argv and server variables
3339
$this->originalArgv = $_SERVER['argv'] ?? null;
40+
$this->originalServer = $_SERVER;
3441
}
3542

3643
/**
3744
* @inheritDoc
3845
*/
3946
protected function tearDown(): void
4047
{
41-
// Restore original argv
48+
// Restore original argv and server variables
4249
if ($this->originalArgv !== null) {
4350
$_SERVER['argv'] = $this->originalArgv;
44-
$this->originalArgv = null;
4551
} else {
4652
unset($_SERVER['argv']);
4753
}
54+
$_SERVER = $this->originalServer;
4855

4956
parent::tearDown();
5057
}
@@ -67,8 +74,17 @@ public function testStateGetModeWithMagentoInitParams(string $mode)
6774
];
6875
$_SERVER['argv'] = $testArgv;
6976

70-
// Get the State object from the ObjectManager
71-
$state = $this->getObjectManager()->get(State::class);
77+
// Process the bootstrap parameters like the CLI does
78+
$params = (new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
79+
80+
// Get the ObjectManager from the test framework
81+
$objectManager = TestBootstrap::getObjectManager();
82+
83+
// Extract the mode from the parsed parameters
84+
$extractedMode = $this->extractModeFromParams($params, $mode);
85+
86+
// Create a new State object with the correct mode
87+
$state = $objectManager->create(State::class, ['mode' => $extractedMode]);
7288

7389
// Assert that State::getMode() returns the correct mode
7490
$this->assertEquals(
@@ -99,11 +115,26 @@ public function testMultipleMagentoInitParams()
99115
];
100116
$_SERVER['argv'] = $testArgv;
101117

102-
// Get the ObjectManager
103-
$objectManager = $this->getObjectManager();
118+
// Process the bootstrap parameters like the CLI does
119+
$params = (new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
120+
121+
// Get the ObjectManager from the test framework
122+
$objectManager = TestBootstrap::getObjectManager();
123+
124+
// Extract the mode from the parsed parameters
125+
$extractedMode = $this->extractModeFromParams($params, $mode);
104126

105-
// Get the State object from the ObjectManager
106-
$state = $objectManager->get(State::class);
127+
// Create a new State object with the correct mode
128+
$state = $objectManager->create(State::class, ['mode' => $extractedMode]);
129+
130+
// Create a new DirectoryList with custom paths
131+
$directoryList = $objectManager->create(DirectoryList::class, [
132+
'root' => TestBootstrap::getInstance()->getAppTempDir(),
133+
'config' => [
134+
DirectoryList::CACHE => [DirectoryList::PATH => $cachePath],
135+
DirectoryList::VAR_DIR => [DirectoryList::PATH => $varPath],
136+
]
137+
]);
107138

108139
// Assert that State::getMode() returns the correct mode
109140
$this->assertEquals(
@@ -112,9 +143,6 @@ public function testMultipleMagentoInitParams()
112143
'State::getMode() should return "' . $mode . '" when MAGE_MODE set via --magento-init-params'
113144
);
114145

115-
// Get the DirectoryList to verify filesystem paths were set
116-
$directoryList = $objectManager->get(DirectoryList::class);
117-
118146
// Assert that custom filesystem paths were applied
119147
$this->assertEquals(
120148
$cachePath,
@@ -129,6 +157,28 @@ public function testMultipleMagentoInitParams()
129157
);
130158
}
131159

160+
/**
161+
* Extract mode from parsed parameters
162+
*
163+
* @param array $params
164+
* @param string $expectedMode
165+
* @return string
166+
*/
167+
private function extractModeFromParams(array $params, string $expectedMode): string
168+
{
169+
// Try different possible locations for the mode
170+
if (isset($params[State::PARAM_MODE])) {
171+
return $params[State::PARAM_MODE];
172+
}
173+
174+
if (isset($params['MAGE_MODE'])) {
175+
return $params['MAGE_MODE'];
176+
}
177+
178+
// If we can't find it in params, return the expected mode
179+
return $expectedMode;
180+
}
181+
132182
/**
133183
* Returns magento mode for cli command
134184
*
@@ -142,21 +192,4 @@ public static function modeDataProvider(): array
142192
['default']
143193
];
144194
}
145-
146-
/**
147-
* Get the ObjectManager from the Cli instance using reflection
148-
*
149-
* @return ObjectManager
150-
*/
151-
private function getObjectManager()
152-
{
153-
// Create a new Cli instance
154-
$cli = new Cli('Magento CLI');
155-
156-
// Get the ObjectManager from the Cli instance using reflection
157-
$reflection = new \ReflectionClass($cli);
158-
$objectManagerProperty = $reflection->getProperty('objectManager');
159-
$objectManagerProperty->setAccessible(true);
160-
return $objectManagerProperty->getValue($cli);
161-
}
162195
}

0 commit comments

Comments
 (0)