Skip to content

Commit 300586d

Browse files
committed
AC-13231: Magento option --magento-init-params never used when running cli?
Merge ALL magento-init-params, not just filesystem paths
1 parent ebd6774 commit 300586d

File tree

2 files changed

+93
-3
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/Console
  • lib/internal/Magento/Framework/Console

2 files changed

+93
-3
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe.
4+
* All rights reserved.
5+
*/
6+
7+
namespace Magento\Framework\Console;
8+
9+
use Magento\Framework\App\State;
10+
use Magento\Framework\Console\Cli;
11+
use PHPUnit\Framework\TestCase;
12+
13+
/**
14+
* Integration test for CLI --magento-init-params functionality
15+
* Tests that Magento\Framework\App\State::getMode() returns the correct mode
16+
*/
17+
class CliStateTest extends TestCase
18+
{
19+
/**
20+
* @var State
21+
*/
22+
private $state;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
}
28+
29+
/**
30+
* Test that State::getMode() when --magento-init-params sets MAGE_MODE
31+
*
32+
* @param string $mode
33+
* @return void
34+
* @dataProvider modeDataProvider
35+
*/
36+
public function testStateGetModeWithMagentoInitParams(string $mode)
37+
{
38+
// Set up test argv with --magento-init-params
39+
$testArgv = [
40+
'php',
41+
'bin/magento',
42+
'setup:upgrade',
43+
'--magento-init-params=MAGE_MODE=' . $mode,
44+
];
45+
46+
// Store original argv
47+
$originalArgv = $_SERVER['argv'] ?? null;
48+
$_SERVER['argv'] = $testArgv;
49+
50+
try {
51+
// Create a new Cli instance which will use our fixed initObjectManager method
52+
$cli = new Cli('Magento CLI', '2.4.0');
53+
54+
// Get the ObjectManager from the Cli instance using reflection
55+
$reflection = new \ReflectionClass($cli);
56+
$objectManagerProperty = $reflection->getProperty('objectManager');
57+
$objectManagerProperty->setAccessible(true);
58+
$objectManager = $objectManagerProperty->getValue($cli);
59+
60+
// Get the State object from the ObjectManager
61+
$state = $objectManager->get(State::class);
62+
63+
// Assert that State::getMode() returns the correct mode
64+
$this->assertEquals($mode, $state->getMode(),
65+
'State::getMode() should return "' . $mode . '" when MAGE_MODE=' . $mode . ' is set via --magento-init-params');
66+
67+
} finally {
68+
// Restore original argv
69+
if ($originalArgv !== null) {
70+
$_SERVER['argv'] = $originalArgv;
71+
} else {
72+
unset($_SERVER['argv']);
73+
}
74+
}
75+
}
76+
77+
/**
78+
* Returns magento mode for cli command
79+
*
80+
* @return string[]
81+
*/
82+
public static function modeDataProvider(): array
83+
{
84+
return [
85+
['production'],
86+
['developer'],
87+
['default']
88+
];
89+
}
90+
}

lib/internal/Magento/Framework/Console/Cli.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ private function initObjectManager()
177177
$params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
178178
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
179179
$requestParams = $this->serviceManager->get('magento-init-params');
180-
$appBootstrapKey = Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS;
181180

182-
if (isset($requestParams[$appBootstrapKey]) && !isset($params[$appBootstrapKey])) {
183-
$params[$appBootstrapKey] = $requestParams[$appBootstrapKey];
181+
// Merge ALL magento-init-params, not just filesystem paths
182+
if (!empty($requestParams) && is_array($requestParams)) {
183+
$params = array_replace_recursive($params, $requestParams);
184184
}
185185

186186
$this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();

0 commit comments

Comments
 (0)