-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHarTest.php
More file actions
138 lines (115 loc) · 4.21 KB
/
HarTest.php
File metadata and controls
138 lines (115 loc) · 4.21 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace Deviantintegral\Har\Tests\Unit;
use Deviantintegral\Har\Handler\TruncatingDateTimeHandler;
use Deviantintegral\Har\Har;
use Deviantintegral\Har\Log;
use Deviantintegral\Har\Serializer;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* @covers \Deviantintegral\Har\Har
*/
class HarTest extends HarTestBase
{
/**
* Tests deserializing and reserializing a complete HAR file.
*/
#[DataProvider('fixtureDataProvider')]
public function testExportedFixture(string $id, Har $har): void
{
$repository = $this->getHarFileRepository();
$file = $repository->loadJson($id);
$file = (new Serializer())->removeBOM($file);
$jsonDecode = json_decode($file, true);
$this->removeCustomFields($jsonDecode);
$this->normalizeDateTime($jsonDecode);
$serialized = $this->getSerializer()->serialize($har, 'json');
$this->assertEquals($jsonDecode, json_decode($serialized, true));
}
/**
* @return \Generator<int, array{0: string, 1: Har}>
*/
public static function fixtureDataProvider(): \Generator
{
$repository = new \Deviantintegral\Har\Repository\HarFileRepository(__DIR__.'/../../fixtures');
foreach ($repository->loadMultiple() as $id => $har) {
yield [$id, $har];
}
}
public function testSplitLogEntries(): void
{
$repository = $this->getHarFileRepository();
$har = $repository->load('www.softwareishard.com-multiple-entries.har');
$splitHars = [];
foreach ($har->splitLogEntries() as $index => $splitHar) {
$splitHars[$index] = $splitHar;
}
// Verify it's a different instance (cloned)
foreach ($splitHars as $splitHar) {
$this->assertNotSame($har, $splitHar);
}
}
public function testSetLog(): void
{
$creator = new \Deviantintegral\Har\Creator();
$creator->setName('TestCreator');
$creator->setVersion('1.0');
$log = (new Log())
->setVersion('1.2')
->setCreator($creator)
->setEntries([]);
$har = (new Har())->setLog($log);
$this->assertSame($log, $har->getLog());
$this->assertEquals('1.2', $har->getLog()->getVersion());
}
public function testCloneBrowserIsDeep(): void
{
$repository = $this->getHarFileRepository();
$har = $repository->load('www.softwareishard.com-multiple-entries.har');
// Set up a browser object for testing (HAR files may not include browser data)
$browser = new \Deviantintegral\Har\Browser();
$browser->setVersion('1.0.0');
$har->getLog()->setBrowser($browser);
$originalBrowserVersion = $har->getLog()->getBrowser()->getVersion();
// Clone the HAR
$cloned = clone $har;
// Modify the cloned HAR's browser version
$cloned->getLog()->getBrowser()->setVersion('modified-version');
// Verify the original HAR's browser version is unchanged
$this->assertSame($originalBrowserVersion, $har->getLog()->getBrowser()->getVersion());
}
/**
* @param array<mixed> $a
*/
private function removeCustomFields(array &$a): void
{
foreach ($a as &$value) {
if (\is_array($value)) {
$this->removeCustomFields($value);
}
}
$a = array_filter($a, function ($key): bool {
return !\is_string($key) || !str_starts_with($key, '_');
}, \ARRAY_FILTER_USE_KEY);
}
/**
* @param array<mixed> $a
*/
private function normalizeDateTime(array &$a): void
{
foreach ($a as &$value) {
if (\is_array($value)) {
$this->normalizeDateTime($value);
}
}
$keys = ['startedDateTime', 'expires'];
$handler = new TruncatingDateTimeHandler();
foreach ($a as $key => &$value) {
if (\in_array($key, $keys, true) && !empty($value)) {
$value = $handler->truncateMicroseconds($value);
$date = \DateTime::createFromFormat(Log::ISO_8601_MICROSECONDS, $value);
$value = $date->format(Log::ISO_8601_MICROSECONDS);
}
}
}
}