1414namespace CodeIgniter \Commands ;
1515
1616use CodeIgniter \Cache \CacheFactory ;
17+ use CodeIgniter \Cache \Handlers \FileHandler ;
1718use CodeIgniter \Test \CIUnitTestCase ;
1819use CodeIgniter \Test \StreamFilterTrait ;
1920use Config \Services ;
@@ -31,15 +32,27 @@ protected function setUp(): void
3132 {
3233 parent ::setUp ();
3334
35+ $ this ->resetServices ();
36+
3437 // Make sure we are testing with the correct handler (override injections)
3538 Services::injectMock ('cache ' , CacheFactory::getHandler (config ('Cache ' )));
3639 }
3740
41+ protected function tearDown (): void
42+ {
43+ parent ::tearDown ();
44+
45+ $ this ->resetServices ();
46+ }
47+
3848 public function testClearCacheInvalidHandler (): void
3949 {
4050 command ('cache:clear junk ' );
4151
42- $ this ->assertStringContainsString ('junk is not a valid cache handler. ' , $ this ->getStreamFilterBuffer ());
52+ $ this ->assertSame (
53+ "Cache driver \"junk \" is not a valid cache handler. \n" ,
54+ preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()),
55+ );
4356 }
4457
4558 public function testClearCacheWorks (): void
@@ -52,4 +65,22 @@ public function testClearCacheWorks(): void
5265 $ this ->assertNull (cache ('foo ' ));
5366 $ this ->assertStringContainsString ('Cache cleared. ' , $ this ->getStreamFilterBuffer ());
5467 }
68+
69+ public function testClearCacheFails (): void
70+ {
71+ $ cache = $ this ->getMockBuilder (FileHandler::class)
72+ ->setConstructorArgs ([config ('Cache ' )])
73+ ->onlyMethods (['clean ' ])
74+ ->getMock ();
75+ $ cache ->expects ($ this ->once ())->method ('clean ' )->willReturn (false );
76+
77+ Services::injectMock ('cache ' , $ cache );
78+
79+ command ('cache:clear ' );
80+
81+ $ this ->assertSame (
82+ "Error while clearing the cache. \n" ,
83+ preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()),
84+ );
85+ }
5586}
0 commit comments