@@ -70,14 +70,6 @@ protected function tearDown(): void
7070 CLI ::reset ();
7171 }
7272
73- /**
74- * Gets buffer contents then releases it.
75- */
76- protected function getBuffer (): string
77- {
78- return $ this ->getStreamFilterBuffer ();
79- }
80-
8173 protected function resetEnvironment (): void
8274 {
8375 putenv ('encryption.key ' );
@@ -88,31 +80,35 @@ protected function resetEnvironment(): void
8880 public function testGenerateKeyShowsEncodedKey (): void
8981 {
9082 command ('key:generate --show ' );
91- $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
83+ $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getStreamFilterBuffer ());
9284
85+ $ this ->resetStreamFilterBuffer ();
9386 command ('key:generate --prefix base64 --show ' );
94- $ this ->assertStringContainsString ('base64: ' , $ this ->getBuffer ());
87+ $ this ->assertStringContainsString ('base64: ' , $ this ->getStreamFilterBuffer ());
9588
89+ $ this ->resetStreamFilterBuffer ();
9690 command ('key:generate --prefix hex2bin --show ' );
97- $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
91+ $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getStreamFilterBuffer ());
9892 }
9993
10094 #[PreserveGlobalState(false )]
10195 #[RunInSeparateProcess]
10296 public function testGenerateKeyCreatesNewKey (): void
10397 {
10498 command ('key:generate ' );
105- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
99+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
106100 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
107101 $ this ->assertStringContainsString ('hex2bin: ' , (string ) file_get_contents ($ this ->envPath ));
108102
103+ $ this ->resetStreamFilterBuffer ();
109104 command ('key:generate --prefix base64 --force ' );
110- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
105+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
111106 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
112107 $ this ->assertStringContainsString ('base64: ' , (string ) file_get_contents ($ this ->envPath ));
113108
109+ $ this ->resetStreamFilterBuffer ();
114110 command ('key:generate --prefix hex2bin --force ' );
115- $ this ->assertStringContainsString (' successfully set. ' , $ this ->getBuffer ());
111+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
116112 $ this ->assertStringContainsString (env ('encryption.key ' ), (string ) file_get_contents ($ this ->envPath ));
117113 $ this ->assertStringContainsString ('hex2bin: ' , (string ) file_get_contents ($ this ->envPath ));
118114 }
@@ -123,8 +119,9 @@ public function testDefaultShippedEnvIsMissing(): void
123119 command ('key:generate ' );
124120 rename (ROOTPATH . 'lostenv ' , ROOTPATH . 'env ' );
125121
126- $ this ->assertStringContainsString ('Both default shipped ' , $ this ->getBuffer ());
127- $ this ->assertStringContainsString ('Error in setting ' , $ this ->getBuffer ());
122+ $ this ->assertStringContainsString ('Both default shipped ' , $ this ->getStreamFilterBuffer ());
123+ $ this ->assertStringContainsString ('Here \'s your new key instead: ' , $ this ->getStreamFilterBuffer ());
124+ $ this ->assertStringNotContainsString ('Failed to write ' , $ this ->getStreamFilterBuffer ());
128125 }
129126
130127 /**
@@ -136,7 +133,7 @@ public function testKeyGenerateWhenKeyIsMissingInDotEnvFile(): void
136133
137134 command ('key:generate ' );
138135
139- $ this ->assertStringContainsString (' Application \' s new encryption key was successfully set. ' , $ this ->getBuffer ());
136+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
140137 $ this ->assertSame ("\nencryption.key = " . env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
141138 }
142139
@@ -152,9 +149,9 @@ public function testKeyGenerateWhenNewHexKeyIsSubsequentlyCommentedOut(): void
152149 ));
153150 $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
154151
155- CITestStreamFilter:: $ buffer = '' ;
152+ $ this -> resetStreamFilterBuffer () ;
156153 command ('key:generate --force ' );
157- $ this ->assertStringContainsString (' was successfully set. ' , $ this ->getBuffer ());
154+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
158155 $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
159156 }
160157
@@ -170,9 +167,9 @@ public function testKeyGenerateWhenNewBase64KeyIsSubsequentlyCommentedOut(): voi
170167 ));
171168 $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
172169
173- CITestStreamFilter:: $ buffer = '' ;
170+ $ this -> resetStreamFilterBuffer () ;
174171 command ('key:generate --force ' );
175- $ this ->assertStringContainsString (' was successfully set. ' , $ this ->getBuffer ());
172+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
176173 $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
177174 }
178175
@@ -190,15 +187,15 @@ public function testKeyGenerateReplacesUnloadedKeyInDotEnvFile(): void
190187 $ this ->assertSame ('' , env ('encryption.key ' , '' ));
191188
192189 command ('key:generate --force ' );
190+ $ this ->assertStringContainsString (sprintf ('New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
193191
194- $ this ->assertStringContainsString ('was successfully set. ' , $ this ->getBuffer ());
195-
196- $ contents = (string ) file_get_contents ($ this ->envPath );
192+ $ contents = @file_get_contents ($ this ->envPath );
193+ $ this ->assertIsString ($ contents , 'Failed to read .env file contents. ' );
197194 $ this ->assertStringNotContainsString ($ existingKey , $ contents );
198195 $ this ->assertStringContainsString ('encryption.key = ' . env ('encryption.key ' ), $ contents );
199196 }
200197
201- public function testKeyGenerateAbortsWhenOverwritePromptIsDeclined (): void
198+ public function testKeyGenerateCancelsWhenOverwritePromptIsDeclined (): void
202199 {
203200 command ('key:generate ' );
204201 $ key = env ('encryption.key ' , '' );
@@ -208,12 +205,13 @@ public function testKeyGenerateAbortsWhenOverwritePromptIsDeclined(): void
208205 $ io ->setInputs (['n ' ]);
209206 CLI ::setInputOutput ($ io );
210207
208+ $ this ->resetStreamFilterBuffer ();
211209 command ('key:generate ' );
212210
213211 $ this ->assertSame ($ key , env ('encryption.key ' , '' ), 'Existing key should not change. ' );
214212 $ this ->assertStringContainsString ($ key , (string ) file_get_contents ($ this ->envPath ));
215213 $ this ->assertStringContainsString ('Overwrite existing key? ' , $ io ->getOutput ());
216- $ this ->assertStringContainsString ('Setting new encryption key aborted . ' , $ io ->getOutput ());
214+ $ this ->assertStringContainsString ('Setting new encryption key cancelled . ' , $ io ->getOutput ());
217215 }
218216
219217 public function testKeyGenerateOverwritesWhenOverwritePromptIsConfirmed (): void
@@ -226,12 +224,13 @@ public function testKeyGenerateOverwritesWhenOverwritePromptIsConfirmed(): void
226224 $ io ->setInputs (['y ' ]);
227225 CLI ::setInputOutput ($ io );
228226
227+ $ this ->resetStreamFilterBuffer ();
229228 command ('key:generate --prefix base64 ' );
230229
231230 $ this ->assertNotSame ($ oldKey , env ('encryption.key ' , $ oldKey ));
232231 $ this ->assertStringContainsString ('base64: ' , (string ) file_get_contents ($ this ->envPath ));
233232 $ this ->assertStringContainsString ('Overwrite existing key? ' , $ io ->getOutput ());
234- $ this ->assertStringContainsString (' successfully set. ' , $ io ->getOutput ());
233+ $ this ->assertStringContainsString (sprintf ( ' New encryption key written to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ) , $ io ->getOutput ());
235234 }
236235
237236 #[PreserveGlobalState(false )]
@@ -243,19 +242,20 @@ public function testKeyGenerateAbortsNonInteractivelyWithExistingKey(): void
243242 $ this ->assertNotSame ('' , $ key );
244243
245244 $ this ->resetStreamFilterBuffer ();
246-
247245 command ('key:generate --no-interaction ' );
248246
249247 $ this ->assertSame ($ key , env ('encryption.key ' , '' ), 'Existing key should not change. ' );
250- $ this ->assertStringContainsString ('Setting new encryption key aborted. ' , $ this ->getBuffer ());
251- $ this ->assertStringContainsString ('--force ' , $ this ->getBuffer ());
248+ $ this ->assertStringContainsString (
249+ 'Setting new encryption key aborted: pass --force to overwrite the existing key in non-interactive mode. ' ,
250+ $ this ->getStreamFilterBuffer (),
251+ );
252252 }
253253
254254 public function testKeyGenerateErrorsOnInvalidPrefixNonInteractively (): void
255255 {
256256 command ('key:generate --prefix invalid --show --no-interaction ' );
257257
258- $ this ->assertStringContainsString ('Invalid prefix "invalid" ' , $ this ->getBuffer ());
258+ $ this ->assertStringContainsString ('Invalid prefix "invalid" ' , $ this ->getStreamFilterBuffer ());
259259 }
260260
261261 public function testKeyGeneratePromptsForInvalidPrefix (): void
@@ -269,4 +269,19 @@ public function testKeyGeneratePromptsForInvalidPrefix(): void
269269 $ this ->assertStringContainsString ('Please provide a valid prefix to use. ' , $ io ->getOutput ());
270270 $ this ->assertStringContainsString ('hex2bin: ' , $ io ->getOutput ());
271271 }
272+
273+ public function testKeyGenerateErrorsWhenEnvFileIsNotWritable (): void
274+ {
275+ command ('key:generate ' );
276+ chmod ($ this ->envPath , 0o444 );
277+
278+ try {
279+ $ this ->resetStreamFilterBuffer ();
280+ command ('key:generate --force ' );
281+
282+ $ this ->assertStringContainsString (sprintf ('Failed to write new encryption key to ROOTPATH%s.env. ' , DIRECTORY_SEPARATOR ), $ this ->getStreamFilterBuffer ());
283+ } finally {
284+ chmod ($ this ->envPath , 0o644 );
285+ }
286+ }
272287}
0 commit comments