@@ -74,13 +74,13 @@ protected function resetEnvironment()
7474
7575 public function testGenerateKeyShowsEncodedKey ()
7676 {
77- command ('key:generate -show ' );
77+ command ('key:generate -- show ' );
7878 $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
7979
80- command ('key:generate -prefix base64 -show ' );
80+ command ('key:generate -- prefix base64 - -show ' );
8181 $ this ->assertStringContainsString ('base64: ' , $ this ->getBuffer ());
8282
83- command ('key:generate -prefix hex2bin -show ' );
83+ command ('key:generate -- prefix hex2bin - -show ' );
8484 $ this ->assertStringContainsString ('hex2bin: ' , $ this ->getBuffer ());
8585 }
8686
@@ -95,12 +95,12 @@ public function testGenerateKeyCreatesNewKey()
9595 $ this ->assertStringContainsString (env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
9696 $ this ->assertStringContainsString ('hex2bin: ' , file_get_contents ($ this ->envPath ));
9797
98- command ('key:generate -prefix base64 -force ' );
98+ command ('key:generate -- prefix base64 - -force ' );
9999 $ this ->assertStringContainsString ('successfully set. ' , $ this ->getBuffer ());
100100 $ this ->assertStringContainsString (env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
101101 $ this ->assertStringContainsString ('base64: ' , file_get_contents ($ this ->envPath ));
102102
103- command ('key:generate -prefix hex2bin -force ' );
103+ command ('key:generate -- prefix hex2bin - -force ' );
104104 $ this ->assertStringContainsString ('successfully set. ' , $ this ->getBuffer ());
105105 $ this ->assertStringContainsString (env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
106106 $ this ->assertStringContainsString ('hex2bin: ' , file_get_contents ($ this ->envPath ));
@@ -115,4 +115,53 @@ public function testDefaultShippedEnvIsMissing()
115115 $ this ->assertStringContainsString ('Both default shipped ' , $ this ->getBuffer ());
116116 $ this ->assertStringContainsString ('Error in setting ' , $ this ->getBuffer ());
117117 }
118+
119+ /**
120+ * @see https://github.com/codeigniter4/CodeIgniter4/issues/6838
121+ */
122+ public function testKeyGenerateWhenKeyIsMissingInDotEnvFile ()
123+ {
124+ file_put_contents ($ this ->envPath , '' );
125+
126+ command ('key:generate ' );
127+
128+ $ this ->assertStringContainsString ('Application \'s new encryption key was successfully set. ' , $ this ->getBuffer ());
129+ $ this ->assertSame ("\nencryption.key = " . env ('encryption.key ' ), file_get_contents ($ this ->envPath ));
130+ }
131+
132+ public function testKeyGenerateWhenNewHexKeyIsSubsequentlyCommentedOut ()
133+ {
134+ command ('key:generate ' );
135+ $ key = env ('encryption.key ' , '' );
136+ file_put_contents ($ this ->envPath , str_replace (
137+ 'encryption.key = ' . $ key ,
138+ '# encryption.key = ' . $ key ,
139+ file_get_contents ($ this ->envPath ),
140+ $ count
141+ ));
142+ $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
143+
144+ CITestStreamFilter::$ buffer = '' ;
145+ command ('key:generate --force ' );
146+ $ this ->assertStringContainsString ('was successfully set. ' , $ this ->getBuffer ());
147+ $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
148+ }
149+
150+ public function testKeyGenerateWhenNewBase64KeyIsSubsequentlyCommentedOut ()
151+ {
152+ command ('key:generate --prefix base64 ' );
153+ $ key = env ('encryption.key ' , '' );
154+ file_put_contents ($ this ->envPath , str_replace (
155+ 'encryption.key = ' . $ key ,
156+ '# encryption.key = ' . $ key ,
157+ file_get_contents ($ this ->envPath ),
158+ $ count
159+ ));
160+ $ this ->assertSame (1 , $ count , 'Failed commenting out the previously set application key. ' );
161+
162+ CITestStreamFilter::$ buffer = '' ;
163+ command ('key:generate --force ' );
164+ $ this ->assertStringContainsString ('was successfully set. ' , $ this ->getBuffer ());
165+ $ this ->assertNotSame ($ key , env ('encryption.key ' , $ key ), 'Failed replacing the commented out key. ' );
166+ }
118167}
0 commit comments