forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistoryTest.php
More file actions
98 lines (77 loc) · 2.58 KB
/
HistoryTest.php
File metadata and controls
98 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Debug\Toolbar\Collectors;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use DateTime;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class HistoryTest extends CIUnitTestCase
{
use StreamFilterTrait;
private const STEP = 0.000001;
private float $time;
protected function setUp(): void
{
parent::setUp();
$this->time = (float) sprintf('%.6F', microtime(true));
}
protected function tearDown(): void
{
command('debugbar:clear');
parent::tearDown();
}
private function createDummyDebugbarJson(): void
{
$time = $this->time;
$path = WRITEPATH . 'debugbar' . DIRECTORY_SEPARATOR . "debugbar_{$time}.json";
$dummyData = [
'vars' => [
'response' => [
'statusCode' => 200,
'contentType' => 'text/html; charset=UTF-8',
],
],
'method' => 'get',
'url' => 'localhost',
'isAJAX' => false,
];
// create 20 dummy debugbar json files
for ($i = 0; $i < 20; $i++) {
$path = str_replace((string) $time, sprintf('%.6F', $time - self::STEP), $path);
file_put_contents($path, json_encode($dummyData));
$time = sprintf('%.6F', $time - self::STEP);
}
}
public function testSetFiles(): void
{
$time = $this->time;
// test dir is now populated with json
$this->createDummyDebugbarJson();
$activeRowTime = $time = sprintf('%.6F', $time - self::STEP);
$history = new History();
$history->setFiles($time, 20);
$this->assertArrayHasKey('files', $history->display());
$this->assertNotEmpty($history->display()['files'], 'Dummy Debugbar data is empty');
foreach ($history->display()['files'] as $request) {
$this->assertSame($request['time'], sprintf('%.6F', $time));
$this->assertSame(
$request['datetime'],
DateTime::createFromFormat('U.u', $time)->format('Y-m-d H:i:s.u'),
);
$this->assertSame($request['active'], ($time === $activeRowTime));
$time = sprintf('%.6F', $time - self::STEP);
}
}
}