|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Debug\Toolbar\Collectors; |
| 13 | + |
| 14 | +use CodeIgniter\Test\CIUnitTestCase; |
| 15 | +use CodeIgniter\Test\Filters\CITestStreamFilter; |
| 16 | +use DateTime; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class HistoryTest extends CIUnitTestCase |
| 22 | +{ |
| 23 | + private const STEP = 0.000001; |
| 24 | + |
| 25 | + private float $time; |
| 26 | + private $streamFilter; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 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); |
| 44 | + } |
| 45 | + |
| 46 | + private function createDummyDebugbarJson() |
| 47 | + { |
| 48 | + $time = $this->time; |
| 49 | + $path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json"; |
| 50 | + |
| 51 | + $dummyData = [ |
| 52 | + 'vars' => [ |
| 53 | + 'response' => [ |
| 54 | + 'statusCode' => 200, |
| 55 | + 'contentType' => 'text/html; charset=UTF-8', |
| 56 | + ], |
| 57 | + ], |
| 58 | + 'method' => 'get', |
| 59 | + 'url' => 'localhost', |
| 60 | + 'isAJAX' => false, |
| 61 | + ]; |
| 62 | + |
| 63 | + // create 20 dummy debugbar json files |
| 64 | + for ($i = 0; $i < 20; $i++) { |
| 65 | + $path = str_replace($time, sprintf('%.6f', $time - self::STEP), $path); |
| 66 | + file_put_contents($path, json_encode($dummyData)); |
| 67 | + $time = sprintf('%.6f', $time - self::STEP); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function testSetFiles() |
| 72 | + { |
| 73 | + $time = $this->time; |
| 74 | + |
| 75 | + // test dir is now populated with json |
| 76 | + $this->createDummyDebugbarJson(); |
| 77 | + |
| 78 | + $activeRowTime = $time = sprintf('%.6f', $time - self::STEP); |
| 79 | + |
| 80 | + $history = new History(); |
| 81 | + $history->setFiles($time, 20); |
| 82 | + |
| 83 | + $this->assertArrayHasKey('files', $history->display()); |
| 84 | + $this->assertNotEmpty($history->display()['files'], 'Dummy Debugbar data is empty'); |
| 85 | + |
| 86 | + foreach ($history->display()['files'] as $request) { |
| 87 | + $this->assertSame($request['time'], sprintf('%.6f', $time)); |
| 88 | + $this->assertSame( |
| 89 | + $request['datetime'], |
| 90 | + DateTime::createFromFormat('U.u', $time)->format('Y-m-d H:i:s.u') |
| 91 | + ); |
| 92 | + $this->assertSame($request['active'], ($time === $activeRowTime)); |
| 93 | + |
| 94 | + $time = sprintf('%.6f', $time - self::STEP); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments