8
8
namespace Magento \Framework \Console ;
9
9
10
10
use Magento \Framework \App \Filesystem \DirectoryList ;
11
- use Magento \Framework \App \ObjectManager ;
12
11
use Magento \Framework \App \State ;
12
+ use Magento \Framework \Shell \ComplexParameter ;
13
+ use Magento \TestFramework \Helper \Bootstrap as TestBootstrap ;
13
14
use PHPUnit \Framework \TestCase ;
14
15
15
16
/**
@@ -22,29 +23,35 @@ class CliStateTest extends TestCase
22
23
*/
23
24
private $ originalArgv ;
24
25
26
+ /**
27
+ * @var mixed|null
28
+ */
29
+ private $ originalServer ;
30
+
25
31
/**
26
32
* @inheritDoc
27
33
*/
28
34
protected function setUp (): void
29
35
{
30
36
parent ::setUp ();
31
37
32
- // Store original argv
38
+ // Store original argv and server variables
33
39
$ this ->originalArgv = $ _SERVER ['argv ' ] ?? null ;
40
+ $ this ->originalServer = $ _SERVER ;
34
41
}
35
42
36
43
/**
37
44
* @inheritDoc
38
45
*/
39
46
protected function tearDown (): void
40
47
{
41
- // Restore original argv
48
+ // Restore original argv and server variables
42
49
if ($ this ->originalArgv !== null ) {
43
50
$ _SERVER ['argv ' ] = $ this ->originalArgv ;
44
- $ this ->originalArgv = null ;
45
51
} else {
46
52
unset($ _SERVER ['argv ' ]);
47
53
}
54
+ $ _SERVER = $ this ->originalServer ;
48
55
49
56
parent ::tearDown ();
50
57
}
@@ -67,8 +74,17 @@ public function testStateGetModeWithMagentoInitParams(string $mode)
67
74
];
68
75
$ _SERVER ['argv ' ] = $ testArgv ;
69
76
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 ]);
72
88
73
89
// Assert that State::getMode() returns the correct mode
74
90
$ this ->assertEquals (
@@ -99,11 +115,26 @@ public function testMultipleMagentoInitParams()
99
115
];
100
116
$ _SERVER ['argv ' ] = $ testArgv ;
101
117
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 );
104
126
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
+ ]);
107
138
108
139
// Assert that State::getMode() returns the correct mode
109
140
$ this ->assertEquals (
@@ -112,9 +143,6 @@ public function testMultipleMagentoInitParams()
112
143
'State::getMode() should return " ' . $ mode . '" when MAGE_MODE set via --magento-init-params '
113
144
);
114
145
115
- // Get the DirectoryList to verify filesystem paths were set
116
- $ directoryList = $ objectManager ->get (DirectoryList::class);
117
-
118
146
// Assert that custom filesystem paths were applied
119
147
$ this ->assertEquals (
120
148
$ cachePath ,
@@ -129,6 +157,28 @@ public function testMultipleMagentoInitParams()
129
157
);
130
158
}
131
159
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
+
132
182
/**
133
183
* Returns magento mode for cli command
134
184
*
@@ -142,21 +192,4 @@ public static function modeDataProvider(): array
142
192
['default ' ]
143
193
];
144
194
}
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
- }
162
195
}
0 commit comments