-
Notifications
You must be signed in to change notification settings - Fork 6
/
HarTest.php
71 lines (61 loc) · 2.11 KB
/
HarTest.php
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
<?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;
class HarTest extends HarTestBase
{
/**
* Tests deserializing and reserializing a complete HAR file.
*
* @dataProvider fixtureDataProvider
*/
public function testExportedFixture(string $id, Har $har)
{
$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));
}
public function fixtureDataProvider()
{
$repository = $this->getHarFileRepository();
foreach ($repository->loadMultiple() as $id => $har) {
yield [$id, $har];
}
}
private function removeCustomFields(array &$a)
{
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);
}
private function normalizeDateTime(array &$a)
{
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);
}
}
}
}