Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ final class Entry
*/
private $connection;

/**
* Detailed info about the request.
*
* @var \Deviantintegral\Har\Initiator
* @Serializer\Type("Deviantintegral\Har\Initiator")
*/
private $_initiator;

public function getPageref(): string
{
return $this->pageref;
Expand Down Expand Up @@ -217,4 +225,24 @@ public function setConnection(string $connection): self

return $this;
}

public function getInitiator(): ?Initiator
{
return $this->_initiator;
}

public function hasInitiator(): bool
{
return null !== $this->_initiator;
}

/**
* @return Entry
*/
public function setInitiator(Initiator $_initiator): self
{
$this->_initiator = $_initiator;

return $this;
}
}
98 changes: 98 additions & 0 deletions src/Initiator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Deviantintegral\Har;

use JMS\Serializer\Annotation as Serializer;

/**
* @see https://developers.google.com/web/tools/chrome-devtools/network/reference#requests
*/
final class Initiator
{
/**
* @var string
* @Serializer\Type("string")
*
* parser:
* Chrome's HTML parser.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this truly just Chrome, or any Blink-derived browser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected values are from https://developers.google.com/web/tools/chrome-devtools/network/reference#requests
You are absolutely correct, all blink based browsers with devtools would use these values.

* redirect:
* An HTTP redirect.
* script:
* A JavaScript function.
* other:
* Some other process or action, such as navigating to a page via a link
* or entering a URL in the address bar.
*/
private $type;

/**
* URL of the entry that initiated this request.
*
* @var \Psr\Http\Message\UriInterface
* @Serializer\Type("Psr\Http\Message\UriInterface")
*/
private $url;

/**
* Line number that initiated this request.
*
* @var int
* @Serializer\Type("integer")
*/
private $lineNumber;

public function getType(): ?string
{
return $this->type;
}

/**
* @return Initiator
*/
public function setType(string $type): self
{
$this->type = $type;

return $this;
}

public function getUrl(): ?\Psr\Http\Message\UriInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added nullability infos here.

{
return $this->url;
}

public function hasUrl(): bool
{
return null !== $this->url;
}

/**
* @return Initiator
*/
public function setUrl(\Psr\Http\Message\UriInterface $url): self
{
$this->url = $url;

return $this;
}

public function getLineNumber(): ?int
{
return $this->lineNumber;
}

public function hasLineNumber(): bool
{
return null !== $this->lineNumber;
}

/**
* @return Initiator
*/
public function setLineNumber(int $lineNumber): self
{
$this->lineNumber = $lineNumber;

return $this;
}
}
50 changes: 50 additions & 0 deletions tests/src/Unit/EntryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Deviantintegral\Har\Tests\Unit;

use Deviantintegral\Har\Initiator;
use Deviantintegral\Har\Repository\HarFileRepository;

/**
* @covers \Deviantintegral\Har\Entry
*/
class EntryTest extends HarTestBase
{
private $repository;

protected function setUp(): void
{
// Initialize a repository of HAR files, with IDs being the file names.
$this->repository = new HarFileRepository(__DIR__.'/../../fixtures');
}

public function testHasInitiator()
{
// Load a HAR file into an object.
$har = $this->repository->load('www.softwareishard.com-empty-login.har');
$first = $har->getLog()->getEntries()[0];
$this->assertFalse($first->hasInitiator());
$first->setInitiator((new Initiator())->setType('other'));
$this->assertTrue($first->hasInitiator());
}

public function testSerializationOfEntryWithAddedInitiatorOfTypeOther()
{
// Load a HAR file into an object.
$id = 'www.softwareishard.com-empty-login.har';
$har = $this->repository->load($id);
$first = $har->getLog()->getEntries()[0];
$first->setInitiator((new Initiator())->setType('other'));

$serialized = $this->getSerializer()->serialize($har, 'json');
$actual = json_decode($serialized, true);
$this->assertIsArray($actual['log']['entries'][0]['_initiator']);
$this->assertEquals('other', $actual['log']['entries'][0]['_initiator']['type']);
$this->assertArrayNotHasKey('url', $actual['log']['entries'][0]['_initiator']);
$this->assertArrayNotHasKey('lineNumber', $actual['log']['entries'][0]['_initiator']);

$this->assertArrayNotHasKey('_initiator', $actual['log']['entries'][1]);
}
}
59 changes: 59 additions & 0 deletions tests/src/Unit/InitiatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Deviantintegral\Har\Tests\Unit;

use Deviantintegral\Har\Initiator;
use GuzzleHttp\Psr7\Uri;

/**
* @covers \Deviantintegral\Har\Initiator
*/
class InitiatorTest extends HarTestBase
{
public function testInitiatorWithTypeOther()
{
$serializer = $this->getSerializer();

$initiator = (new Initiator())
->setType('other');
$serialized = $serializer->serialize($initiator, 'json');
$this->assertEquals(
['type' => 'other'],
json_decode($serialized, true)
);

$this->assertDeserialize($serialized, Initiator::class, $initiator);

$this->assertFalse($initiator->hasUrl());
$this->assertNull($initiator->getUrl());
$this->assertFalse($initiator->hasLineNumber());
$this->assertNull($initiator->getLineNumber());
}

public function testInitiatorWithTypeParser()
{
$serializer = $this->getSerializer();

$initiator = (new Initiator())
->setType('parser')
->setUrl(new Uri('https://www.php.net/'))
->setLineNumber(42);
$serialized = $serializer->serialize($initiator, 'json');
$this->assertEquals(
[
'type' => 'parser',
'url' => 'https://www.php.net/',
'lineNumber' => '42',
],
json_decode($serialized, true)
);

$this->assertDeserialize($serialized, Initiator::class, $initiator);
$this->assertTrue($initiator->hasLineNumber());
$this->assertIsNumeric($initiator->getLineNumber());
$this->assertTrue($initiator->hasUrl());
$this->assertNotNull($initiator->getUrl());
}
}