1414namespace CodeIgniter \Commands \Cache ;
1515
1616use CodeIgniter \Cache \CacheFactory ;
17+ use CodeIgniter \CLI \CLI ;
1718use CodeIgniter \Test \CIUnitTestCase ;
1819use CodeIgniter \Test \StreamFilterTrait ;
1920use Config \Services ;
@@ -31,53 +32,69 @@ protected function setUp(): void
3132 {
3233 parent ::setUp ();
3334
34- // Make sure we are testing with the correct handler (override injections)
35+ CLI ::resetLastWrite ();
36+ $ this ->resetServices ();
3537 Services ::injectMock ('cache ' , CacheFactory ::getHandler (config ('Cache ' )));
3638 }
3739
3840 protected function tearDown (): void
3941 {
40- // restore default cache handler
41- config ('Cache ' )->handler = 'file ' ;
42+ parent ::tearDown ();
43+
44+ CLI ::resetLastWrite ();
45+ $ this ->resetServices ();
46+ $ this ->resetFactories ();
4247 }
4348
44- protected function getBuffer (): string
49+ private function getUndecoratedBuffer (): string
4550 {
46- return $ this ->getStreamFilterBuffer ();
51+ return preg_replace ( ' /\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()) ?? '' ;
4752 }
4853
4954 public function testInfoCacheErrorsOnInvalidHandler (): void
5055 {
5156 config ('Cache ' )->handler = 'redis ' ;
52- cache ()-> save ( ' foo ' , ' bar ' );
57+
5358 command ('cache:info ' );
5459
55- $ this ->assertStringContainsString ('This command only supports the file cache handler. ' , $ this ->getBuffer ());
60+ $ this ->assertSame (
61+ <<<'EOT'
62+
63+ This command only supports the file cache handler.
64+
65+ EOT,
66+ $ this ->getUndecoratedBuffer (),
67+ );
5668 }
5769
5870 public function testInfoCacheCanSeeFoo (): void
5971 {
6072 cache ()->save ('foo ' , 'bar ' );
6173 command ('cache:info ' );
6274
63- $ this ->assertStringContainsString ('foo ' , $ this ->getBuffer ());
75+ $ this ->assertStringContainsString ('foo ' , $ this ->getStreamFilterBuffer ());
6476 }
6577
66- public function testInfoCacheCanSeeTable (): void
78+ public function testInfoCacheCanSeeTheads (): void
6779 {
6880 command ('cache:info ' );
6981
70- $ this ->assertStringContainsString ( ' Name ' , $ this -> getBuffer ());
71- $ this -> assertStringContainsString ( ' Server Path' , $ this -> getBuffer ());
72- $ this -> assertStringContainsString ( ' Size ' , $ this ->getBuffer ());
73- $ this -> assertStringContainsString ( ' Date ' , $ this -> getBuffer () );
82+ $ this ->assertMatchesRegularExpression (
83+ ' /\|\sName[[:space:]]+\|\sServer Path[[:space:]]+\|\sSize[[:space:]]+\|\sDate[[:space:]]+\|/ ' ,
84+ $ this ->getUndecoratedBuffer (),
85+ );
7486 }
7587
7688 public function testInfoCacheCannotSeeFoo (): void
7789 {
78- cache ()->delete ('foo ' );
90+ cache ()->save ('foo ' , ' bar ' );
7991 command ('cache:info ' );
92+ $ this ->assertStringContainsString ('foo ' , $ this ->getStreamFilterBuffer ());
93+
94+ $ this ->resetStreamFilterBuffer ();
8095
81- $ this ->assertStringNotContainsString ('foo ' , $ this ->getBuffer ());
96+ cache ()->delete ('foo ' );
97+ command ('cache:info ' );
98+ $ this ->assertStringNotContainsString ('foo ' , $ this ->getUndecoratedBuffer ());
8299 }
83100}
0 commit comments