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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Repository/HarFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function getIds(): array
public function loadJson(string $id): string
{
$path = $this->repositoryPath.'/'.$id;
if (!file_exists($path)) {
throw new \RuntimeException(\sprintf('%s does not exist', $path));
}

$contents = file_get_contents($path);

if (!$contents) {
Expand Down
131 changes: 131 additions & 0 deletions tests/src/Unit/Adapter/Psr7/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace Deviantintegral\Har\Tests\Unit\Adapter\Psr7;

use Deviantintegral\Har\Adapter\Psr7\Response;
use Deviantintegral\Har\Tests\Unit\HarTestBase;

/**
* @covers \Deviantintegral\Har\Adapter\Psr7\Response
* @covers \Deviantintegral\Har\Adapter\Psr7\MessageBase
*/
class ResponseTest extends HarTestBase
{
/**
* @var Response
*/
private $response;

protected function setUp(): void
{
$this->response = new Response(
$this->getHarFileRepository()->load(
'www.softwareishard.com-single-entry.har'
)->getLog()->getEntries()[0]->getResponse()
);
}

public function testGetStatusCode()
{
$this->assertEquals(200, $this->response->getStatusCode());
}

public function testWithStatus()
{
$withStatus = $this->response->withStatus(404, 'Not Found');
$this->assertEquals(404, $withStatus->getStatusCode());
$this->assertEquals('Not Found', $withStatus->getReasonPhrase());
}

public function testGetReasonPhrase()
{
$this->assertEquals('OK', $this->response->getReasonPhrase());
}

public function testGetHarResponse()
{
$harResponse = $this->response->getHarResponse();
$this->assertInstanceOf(\Deviantintegral\Har\Response::class, $harResponse);
$this->assertEquals(200, $harResponse->getStatus());
}

public function testWithHeader()
{
$withHeader = $this->response->withHeader('X-Test', 'value');
$this->assertEquals(['value'], $withHeader->getHeader('X-Test'));

$withArrayValue = $this->response->withHeader(
'X-Test',
['value1', 'value2']
);
$this->assertEquals(
['value1', 'value2'],
$withArrayValue->getHeader('X-Test')
);
}

public function testGetHeaderLine()
{
$withMultiple = $this->response->withAddedHeader('X-Custom', 'value1')
->withAddedHeader('X-Custom', 'value2');
$this->assertEquals('value1, value2', $withMultiple->getHeaderLine('X-Custom'));
}

public function testGetHeaderLineWhenHeaderNotPresent()
{
$this->assertEquals('', $this->response->getHeaderLine('X-NonExistent'));
}

public function testGetHeader()
{
$headers = $this->response->getHeader('Content-Type');
$this->assertIsArray($headers);
}

public function testHasHeader()
{
$this->assertTrue($this->response->hasHeader('Content-Type'));
$this->assertFalse($this->response->hasHeader('X-NonExistent'));
}

public function testGetHeaders()
{
$headers = $this->response->getHeaders();
$this->assertIsArray($headers);
$this->assertArrayHasKey('Content-Type', $headers);
}

public function testGetProtocolVersion()
{
$this->assertEquals('1.1', $this->response->getProtocolVersion());
}

public function testWithProtocolVersion()
{
$withProtocol = $this->response->withProtocolVersion('2.0');
$this->assertEquals('2.0', $withProtocol->getProtocolVersion());
}

public function testWithoutHeader()
{
$withoutHeader = $this->response->withoutHeader('Content-Type');
$this->assertFalse($withoutHeader->hasHeader('Content-Type'));
}

public function testWithAddedHeader()
{
$withAdded = $this->response->withAddedHeader('X-Custom', 'value1');
$this->assertEquals(['value1'], $withAdded->getHeader('X-Custom'));

$withMultiple = $withAdded->withAddedHeader('X-Custom', 'value2');
$this->assertEquals(['value1', 'value2'], $withMultiple->getHeader('X-Custom'));
}

public function testWithAddedHeaderArrayValue()
{
$withAdded = $this->response->withAddedHeader('X-Custom', ['value1', 'value2']);
$this->assertEquals(['value1', 'value2'], $withAdded->getHeader('X-Custom'));
}
}
50 changes: 50 additions & 0 deletions tests/src/Unit/BrowserTest.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\Browser;

/**
* @covers \Deviantintegral\Har\Browser
*/
class BrowserTest extends HarTestBase
{
public function testSerialize()
{
$serializer = $this->getSerializer();
$browser = (new Browser())
->setName('BrowserTest')
->setVersion('1.0')
->setComment('Test case');
$serialized = $serializer->serialize($browser, 'json');
$this->assertEquals(
[
'name' => 'BrowserTest',
'version' => '1.0',
'comment' => 'Test case',
],
json_decode($serialized, true)
);

$deserialized = $serializer->deserialize(
$serialized,
Browser::class,
'json'
);
$this->assertEquals($browser, $deserialized);
}

public function testGet()
{
$browser = (new Browser())
->setName('BrowserTest')
->setVersion('1.0')
->setComment('Test case');

$this->assertEquals('BrowserTest', $browser->getName());
$this->assertEquals('1.0', $browser->getVersion());
$this->assertEquals('Test case', $browser->getComment());
}
}
61 changes: 61 additions & 0 deletions tests/src/Unit/CacheStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Deviantintegral\Har\Tests\Unit;

use Deviantintegral\Har\CacheState;
use Deviantintegral\NullDateTime\ConcreteDateTime;

/**
* @covers \Deviantintegral\Har\CacheState
*/
class CacheStateTest extends HarTestBase
{
public function testSerialize()
{
$serializer = $this->getSerializer();
$expires = new ConcreteDateTime(
new \DateTime('2024-12-31T23:59:59Z')
);
$cacheState = (new CacheState())
->setLastAccess('2024-01-01T12:00:00Z')
->setETag('abc123')
->setHitCount(5)
->setExpires($expires)
->setComment('Test cache state');
$serialized = $serializer->serialize($cacheState, 'json');
$decoded = json_decode($serialized, true);

$this->assertEquals('2024-01-01T12:00:00Z', $decoded['lastAccess']);
$this->assertEquals('abc123', $decoded['eTag']);
$this->assertEquals(5, $decoded['hitCount']);
$this->assertArrayHasKey('expires', $decoded);
$this->assertEquals('Test cache state', $decoded['comment']);

$deserialized = $serializer->deserialize(
$serialized,
CacheState::class,
'json'
);
$this->assertEquals($cacheState, $deserialized);
}

public function testGet()
{
$cacheState = (new CacheState())
->setLastAccess('2024-01-01T12:00:00Z')
->setETag('abc123')
->setHitCount(10);

$this->assertEquals('2024-01-01T12:00:00Z', $cacheState->getLastAccess());
$this->assertEquals('abc123', $cacheState->getETag());
$this->assertEquals(10, $cacheState->getHitCount());
}

public function testDefaultHitCount()
{
$cacheState = new CacheState();
$this->assertEquals(0, $cacheState->getHitCount());
}
}
64 changes: 64 additions & 0 deletions tests/src/Unit/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace Deviantintegral\Har\Tests\Unit;

use Deviantintegral\Har\Cache;
use Deviantintegral\Har\Entry;
use Deviantintegral\Har\Initiator;
use Deviantintegral\Har\Repository\HarFileRepository;
use Deviantintegral\Har\Request;
use Deviantintegral\Har\Response;
use Deviantintegral\Har\Timings;

/**
* @covers \Deviantintegral\Har\Entry
Expand Down Expand Up @@ -47,4 +52,63 @@ public function testSerializationOfEntryWithAddedInitiatorOfTypeOther()

$this->assertArrayNotHasKey('_initiator', $actual['log']['entries'][1]);
}

public function testGetSetPageref()
{
$entry = (new Entry())->setPageref('page_1');
$this->assertEquals('page_1', $entry->getPageref());
}

public function testGetSetTime()
{
$entry = (new Entry())->setTime(123.45);
$this->assertEquals(123.45, $entry->getTime());
}

public function testGetSetRequest()
{
$request = new Request();
$entry = (new Entry())->setRequest($request);
$this->assertSame($request, $entry->getRequest());
}

public function testGetSetResponse()
{
$response = new Response();
$entry = (new Entry())->setResponse($response);
$this->assertSame($response, $entry->getResponse());
}

public function testGetSetCache()
{
$cache = new Cache();
$entry = (new Entry())->setCache($cache);
$this->assertSame($cache, $entry->getCache());
}

public function testGetSetTimings()
{
$timings = new Timings();
$entry = (new Entry())->setTimings($timings);
$this->assertSame($timings, $entry->getTimings());
}

public function testGetSetServerIPAddress()
{
$entry = (new Entry())->setServerIPAddress('192.168.1.1');
$this->assertEquals('192.168.1.1', $entry->getServerIPAddress());
}

public function testGetSetConnection()
{
$entry = (new Entry())->setConnection('12345');
$this->assertEquals('12345', $entry->getConnection());
}

public function testGetSetInitiator()
{
$initiator = (new Initiator())->setType('parser');
$entry = (new Entry())->setInitiator($initiator);
$this->assertSame($initiator, $entry->getInitiator());
}
}
Loading