1212namespace CodeIgniter \Debug \Toolbar \Collectors ;
1313
1414use CodeIgniter \Test \CIUnitTestCase ;
15+ use CodeIgniter \Test \Filters \CITestStreamFilter ;
1516use DateTime ;
1617
1718/**
1819 * @internal
1920 */
2021final class HistoryTest extends CIUnitTestCase
2122{
22- private const STEP = 0.0001 ;
23+ private const STEP = 0.000001 ;
2324
2425 private float $ time ;
26+ private $ streamFilter ;
2527
2628 protected function setUp (): void
2729 {
2830 parent ::setUp ();
29- $ this ->time = (float ) sprintf ('%.4f ' , microtime (true ));
31+
32+ CITestStreamFilter::$ buffer = '' ;
33+ $ this ->streamFilter = stream_filter_append (STDOUT , 'CITestStreamFilter ' );
34+ $ this ->streamFilter = stream_filter_append (STDERR , 'CITestStreamFilter ' );
35+
36+ $ this ->time = (float ) sprintf ('%.6f ' , microtime (true ));
37+ }
38+
39+ protected function tearDown (): void
40+ {
41+ command ('debugbar:clear ' );
42+
43+ stream_filter_remove ($ this ->streamFilter );
3044 }
3145
3246 private function createDummyDebugbarJson ()
@@ -45,35 +59,36 @@ private function createDummyDebugbarJson()
4559 'url ' => 'localhost ' ,
4660 'isAJAX ' => false ,
4761 ];
62+
4863 // create 20 dummy debugbar json files
4964 for ($ i = 0 ; $ i < 20 ; $ i ++) {
50- $ path = str_replace ($ time , sprintf ('%.4f ' , $ time - self ::STEP ), $ path );
65+ $ path = str_replace ($ time , sprintf ('%.6f ' , $ time - self ::STEP ), $ path );
5166 file_put_contents ($ path , json_encode ($ dummyData ));
52- $ time = sprintf ('%.4f ' , $ time - self ::STEP );
67+ $ time = sprintf ('%.6f ' , $ time - self ::STEP );
5368 }
5469 }
5570
56- protected function tearDown (): void
57- {
58- command ('debugbar:clear ' );
59- }
60-
6171 public function testSetFiles ()
6272 {
6373 $ time = $ this ->time ;
74+
6475 // test dir is now populated with json
6576 $ this ->createDummyDebugbarJson ();
6677
6778 $ activeRowTime = $ time = sprintf ('%.6f ' , $ time - self ::STEP );
6879
6980 $ history = new History ();
7081 $ history ->setFiles ($ time , 20 );
82+
7183 $ this ->assertArrayHasKey ('files ' , $ history ->display ());
7284 $ this ->assertNotEmpty ($ history ->display ()['files ' ], 'Dummy Debugbar data is empty ' );
7385
7486 foreach ($ history ->display ()['files ' ] as $ request ) {
7587 $ this ->assertSame ($ request ['time ' ], sprintf ('%.6f ' , $ time ));
76- $ this ->assertSame ($ request ['datetime ' ], DateTime::createFromFormat ('U.u ' , $ time )->format ('Y-m-d H:i:s.u ' ));
88+ $ this ->assertSame (
89+ $ request ['datetime ' ],
90+ DateTime::createFromFormat ('U.u ' , $ time )->format ('Y-m-d H:i:s.u ' )
91+ );
7792 $ this ->assertSame ($ request ['active ' ], ($ time === $ activeRowTime ));
7893
7994 $ time = sprintf ('%.6f ' , $ time - self ::STEP );
0 commit comments