@@ -121,6 +121,111 @@ public function testExecuteGeneratesEnvironmentKey(): void
121121 $ this ->assertStringContainsString ( "Generated production environment key at: {$ keyPath }" , $ outputContent );
122122 }
123123
124+ /**
125+ * Test that config directory is created for master key when missing
126+ */
127+ public function testExecuteCreatesMasterKeyDirectory (): void
128+ {
129+ // Use a non-existent config path
130+ $ nonExistentPath = sys_get_temp_dir () . '/test_config_ ' . uniqid () . '/config ' ;
131+
132+ // Create input with options pointing to non-existent path
133+ $ input = new Input ( [ '--config= ' . $ nonExistentPath ] );
134+ $ input ->parse ( $ this ->command );
135+
136+ // Create output
137+ $ output = new Output ( false );
138+
139+ $ this ->command ->setInput ( $ input );
140+ $ this ->command ->setOutput ( $ output );
141+
142+ // Capture output
143+ ob_start ();
144+ $ result = $ this ->command ->execute ();
145+ $ outputContent = ob_get_clean ();
146+
147+ // Execute should succeed
148+ $ this ->assertEquals ( 0 , $ result );
149+
150+ // Directory should be created
151+ $ this ->assertDirectoryExists ( $ nonExistentPath );
152+
153+ // Key file should exist
154+ $ keyPath = $ nonExistentPath . '/master.key ' ;
155+ $ this ->assertFileExists ( $ keyPath );
156+
157+ // Check output contains success message
158+ $ this ->assertStringContainsString ( "Generated master key at: {$ keyPath }" , $ outputContent );
159+
160+ // Clean up
161+ unlink ( $ keyPath );
162+ rmdir ( $ nonExistentPath );
163+ rmdir ( dirname ( $ nonExistentPath ) );
164+ }
165+
166+ /**
167+ * Test that key is only shown when --show flag is used
168+ */
169+ public function testKeyOnlyShownWithShowFlag (): void
170+ {
171+ // Test WITHOUT --show flag (default)
172+ $ input = new Input ( [ '--config= ' . $ this ->testConfigPath ] );
173+ $ input ->parse ( $ this ->command );
174+
175+ $ output = new Output ( false );
176+
177+ $ this ->command ->setInput ( $ input );
178+ $ this ->command ->setOutput ( $ output );
179+
180+ ob_start ();
181+ $ result = $ this ->command ->execute ();
182+ $ outputContentNoShow = ob_get_clean ();
183+
184+ $ this ->assertEquals ( 0 , $ result );
185+
186+ $ keyPath = $ this ->testConfigPath . '/master.key ' ;
187+ $ this ->assertFileExists ( $ keyPath );
188+
189+ // Read the actual key
190+ $ actualKey = file_get_contents ( $ keyPath );
191+
192+ // Key should NOT be in the output (except in the placeholder)
193+ $ this ->assertStringNotContainsString ( "export NEURON_MASTER_KEY= {$ actualKey }" , $ outputContentNoShow );
194+ $ this ->assertStringContainsString ( "export NEURON_MASTER_KEY=<KEY_FROM_ {$ keyPath }> " , $ outputContentNoShow );
195+ $ this ->assertStringNotContainsString ( "Generated Key " , $ outputContentNoShow );
196+
197+ // Clean up before second test
198+ unlink ( $ keyPath );
199+
200+ // Test WITH --show flag
201+ $ input2 = new Input ( [
202+ '--config= ' . $ this ->testConfigPath ,
203+ '--show '
204+ ] );
205+ $ input2 ->parse ( $ this ->command );
206+
207+ $ output2 = new Output ( false );
208+
209+ $ this ->command ->setInput ( $ input2 );
210+ $ this ->command ->setOutput ( $ output2 );
211+
212+ ob_start ();
213+ $ result2 = $ this ->command ->execute ();
214+ $ outputContentWithShow = ob_get_clean ();
215+
216+ $ this ->assertEquals ( 0 , $ result2 );
217+
218+ // Read the new key
219+ $ actualKey2 = file_get_contents ( $ keyPath );
220+
221+ // Key SHOULD be in the output
222+ $ this ->assertStringContainsString ( "export NEURON_MASTER_KEY= {$ actualKey2 }" , $ outputContentWithShow );
223+ $ this ->assertStringNotContainsString ( "<KEY_FROM_ " , $ outputContentWithShow );
224+ $ this ->assertStringContainsString ( "Generated Key " , $ outputContentWithShow );
225+ $ this ->assertStringContainsString ( $ actualKey2 , $ outputContentWithShow );
226+ $ this ->assertStringContainsString ( "This key is shown only once " , $ outputContentWithShow );
227+ }
228+
124229 /**
125230 * Test error when key already exists without force
126231 */
0 commit comments