From 909e51551180754718f0976e7fe30cd844649dd6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 14 Dec 2025 20:58:54 +0000 Subject: [PATCH 1/3] test: increase test coverage from 84% to 96% --- tests/src/Unit/Adapter/Psr7/ResponseTest.php | 131 ++++++++++++++++++ tests/src/Unit/BrowserTest.php | 50 +++++++ tests/src/Unit/CacheStateTest.php | 61 ++++++++ tests/src/Unit/EntryTest.php | 64 +++++++++ .../DateFormatInterfaceHandlerTest.php | 73 ++++++++++ .../Handler/TruncatingDateTimeHandlerTest.php | 95 +++++++++++++ tests/src/Unit/HarTest.php | 33 +++++ tests/src/Unit/LogTest.php | 85 ++++++++++++ tests/src/Unit/PageTest.php | 19 +++ .../Unit/Repository/HarFileRepositoryTest.php | 86 ++++++++++++ tests/src/Unit/RequestTest.php | 45 ++++++ tests/src/Unit/ResponseTest.php | 65 +++++++++ 12 files changed, 807 insertions(+) create mode 100644 tests/src/Unit/Adapter/Psr7/ResponseTest.php create mode 100644 tests/src/Unit/BrowserTest.php create mode 100644 tests/src/Unit/CacheStateTest.php create mode 100644 tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php create mode 100644 tests/src/Unit/Handler/TruncatingDateTimeHandlerTest.php create mode 100644 tests/src/Unit/LogTest.php create mode 100644 tests/src/Unit/Repository/HarFileRepositoryTest.php diff --git a/tests/src/Unit/Adapter/Psr7/ResponseTest.php b/tests/src/Unit/Adapter/Psr7/ResponseTest.php new file mode 100644 index 0000000..f822579 --- /dev/null +++ b/tests/src/Unit/Adapter/Psr7/ResponseTest.php @@ -0,0 +1,131 @@ +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')); + } +} diff --git a/tests/src/Unit/BrowserTest.php b/tests/src/Unit/BrowserTest.php new file mode 100644 index 0000000..398e120 --- /dev/null +++ b/tests/src/Unit/BrowserTest.php @@ -0,0 +1,50 @@ +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()); + } +} diff --git a/tests/src/Unit/CacheStateTest.php b/tests/src/Unit/CacheStateTest.php new file mode 100644 index 0000000..50414bb --- /dev/null +++ b/tests/src/Unit/CacheStateTest.php @@ -0,0 +1,61 @@ +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()); + } +} diff --git a/tests/src/Unit/EntryTest.php b/tests/src/Unit/EntryTest.php index d3d2132..48c90ca 100644 --- a/tests/src/Unit/EntryTest.php +++ b/tests/src/Unit/EntryTest.php @@ -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 @@ -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()); + } } diff --git a/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php b/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php new file mode 100644 index 0000000..85a8e64 --- /dev/null +++ b/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php @@ -0,0 +1,73 @@ +handler = new DateFormatInterfaceHandler(); + } + + public function testGetSubscribingMethods() + { + $methods = DateFormatInterfaceHandler::getSubscribingMethods(); + $this->assertIsArray($methods); + $this->assertNotEmpty($methods); + + // Check that all expected types are present + $types = []; + foreach ($methods as $method) { + $types[] = $method['type']; + } + + $this->assertContains('Deviantintegral\NullDateTime\DateTimeFormatInterface', $types); + $this->assertContains('Deviantintegral\NullDateTime\NullDateTime', $types); + $this->assertContains('Deviantintegral\NullDateTime\ConcreteDateTime', $types); + + // Check that serialization and deserialization are both configured + $directions = []; + foreach ($methods as $method) { + $directions[] = $method['direction']; + } + + $this->assertContains(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $directions); + $this->assertContains(GraphNavigatorInterface::DIRECTION_DESERIALIZATION, $directions); + } + + public function testConstructorWithDefaultParameters() + { + $handler = new DateFormatInterfaceHandler(); + $this->assertInstanceOf(DateFormatInterfaceHandler::class, $handler); + } + + public function testConstructorWithCustomParameters() + { + $handler = new DateFormatInterfaceHandler('Y-m-d', 'America/New_York'); + $this->assertInstanceOf(DateFormatInterfaceHandler::class, $handler); + } + + public function testSerializeNullDateTime() + { + $nullDateTime = new NullDateTime(); + + // Create a mock visitor that won't be called for NullDateTime + $visitor = $this->createMock(\JMS\Serializer\Visitor\SerializationVisitorInterface::class); + $context = $this->createMock(\JMS\Serializer\SerializationContext::class); + + $result = $this->handler->serializeDateTimeFormatInterface($visitor, $nullDateTime, [], $context); + $this->assertEquals('', $result); + } +} diff --git a/tests/src/Unit/Handler/TruncatingDateTimeHandlerTest.php b/tests/src/Unit/Handler/TruncatingDateTimeHandlerTest.php new file mode 100644 index 0000000..c290ba8 --- /dev/null +++ b/tests/src/Unit/Handler/TruncatingDateTimeHandlerTest.php @@ -0,0 +1,95 @@ +handler = new TruncatingDateTimeHandler(); + } + + public function testGetSubscribingMethods() + { + $methods = TruncatingDateTimeHandler::getSubscribingMethods(); + $this->assertIsArray($methods); + $this->assertNotEmpty($methods); + + // Check that all expected types are present + $types = []; + foreach ($methods as $method) { + $types[] = $method['type']; + } + + $this->assertContains('DateTime', $types); + $this->assertContains('DateTimeImmutable', $types); + $this->assertContains('DateInterval', $types); + $this->assertContains('DateTimeInterface', $types); + + // Check that both json and xml formats are configured + $formats = []; + foreach ($methods as $method) { + $formats[] = $method['format']; + } + + $this->assertContains('json', $formats); + $this->assertContains('xml', $formats); + } + + public function testConstructorWithDefaultParameters() + { + $handler = new TruncatingDateTimeHandler(); + $this->assertInstanceOf(TruncatingDateTimeHandler::class, $handler); + } + + public function testConstructorWithCustomParameters() + { + $handler = new TruncatingDateTimeHandler('Y-m-d', 'America/New_York'); + $this->assertInstanceOf(TruncatingDateTimeHandler::class, $handler); + } + + public function testTruncateMicrosecondsWithPlus() + { + $data = '2024-01-01T12:00:00.123456789+00:00'; + $result = $this->handler->truncateMicroseconds($data); + $this->assertEquals('2024-01-01T12:00:00.123456+00:00', $result); + } + + public function testTruncateMicrosecondsWithZ() + { + $data = '2024-01-01T12:00:00.123456789Z'; + $result = $this->handler->truncateMicroseconds($data); + $this->assertEquals('2024-01-01T12:00:00.123456Z', $result); + } + + public function testTruncateMicrosecondsWithUTC() + { + $data = '2024-01-01T12:00:00.123456789UTC'; + $result = $this->handler->truncateMicroseconds($data); + $this->assertEquals('2024-01-01T12:00:00.123456UTC', $result); + } + + public function testTruncateMicrosecondsWithShorterPrecision() + { + $data = '2024-01-01T12:00:00.123+00:00'; + $result = $this->handler->truncateMicroseconds($data); + $this->assertEquals('2024-01-01T12:00:00.123+00:00', $result); + } + + public function testTruncateMicrosecondsWithExactSixDigits() + { + $data = '2024-01-01T12:00:00.123456+00:00'; + $result = $this->handler->truncateMicroseconds($data); + $this->assertEquals('2024-01-01T12:00:00.123456+00:00', $result); + } +} diff --git a/tests/src/Unit/HarTest.php b/tests/src/Unit/HarTest.php index aec37ae..3c276bc 100644 --- a/tests/src/Unit/HarTest.php +++ b/tests/src/Unit/HarTest.php @@ -10,6 +10,9 @@ use Deviantintegral\Har\Serializer; use PHPUnit\Framework\Attributes\DataProvider; +/** + * @covers \Deviantintegral\Har\Har + */ class HarTest extends HarTestBase { /** @@ -37,6 +40,36 @@ public static function fixtureDataProvider() } } + public function testGetSetLog() + { + $log = new Log(); + $har = (new Har())->setLog($log); + $this->assertSame($log, $har->getLog()); + } + + public function testSplitLogEntries() + { + $repository = $this->getHarFileRepository(); + $har = $repository->load('www.softwareishard.com-multiple-entries.har'); + + $originalEntryCount = \count($har->getLog()->getEntries()); + $this->assertGreaterThan(1, $originalEntryCount); + + $splitHars = []; + foreach ($har->splitLogEntries() as $index => $splitHar) { + $splitHars[$index] = $splitHar; + } + + $this->assertCount($originalEntryCount, $splitHars); + + // Verify each split HAR has only one entry + foreach ($splitHars as $splitHar) { + $this->assertCount(1, $splitHar->getLog()->getEntries()); + // Verify it's a different instance (cloned) + $this->assertNotSame($har, $splitHar); + } + } + private function removeCustomFields(array &$a) { foreach ($a as &$value) { diff --git a/tests/src/Unit/LogTest.php b/tests/src/Unit/LogTest.php new file mode 100644 index 0000000..e1f378f --- /dev/null +++ b/tests/src/Unit/LogTest.php @@ -0,0 +1,85 @@ +getSerializer(); + + $creator = (new Creator()) + ->setName('TestCreator') + ->setVersion('1.0'); + + $browser = (new Browser()) + ->setName('TestBrowser') + ->setVersion('2.0'); + + $log = (new Log()) + ->setVersion('1.2') + ->setCreator($creator) + ->setBrowser($browser) + ->setPages([]) + ->setEntries([]) + ->setComment('Test log'); + + $serialized = $serializer->serialize($log, 'json'); + $decoded = json_decode($serialized, true); + + $this->assertEquals('1.2', $decoded['version']); + $this->assertEquals('TestCreator', $decoded['creator']['name']); + $this->assertEquals('TestBrowser', $decoded['browser']['name']); + $this->assertEquals('Test log', $decoded['comment']); + + $deserialized = $serializer->deserialize( + $serialized, + Log::class, + 'json' + ); + $this->assertEquals($log, $deserialized); + } + + public function testGettersAndSetters() + { + $creator = (new Creator()) + ->setName('TestCreator') + ->setVersion('1.0'); + + $browser = (new Browser()) + ->setName('TestBrowser') + ->setVersion('2.0'); + + $pages = []; + $entries = []; + + $log = (new Log()) + ->setVersion('1.2') + ->setCreator($creator) + ->setBrowser($browser) + ->setPages($pages) + ->setEntries($entries) + ->setComment('Test comment'); + + $this->assertEquals('1.2', $log->getVersion()); + $this->assertSame($creator, $log->getCreator()); + $this->assertSame($browser, $log->getBrowser()); + $this->assertEquals($pages, $log->getPages()); + $this->assertEquals($entries, $log->getEntries()); + $this->assertEquals('Test comment', $log->getComment()); + } + + public function testIso8601MicrosecondsConstant() + { + $this->assertEquals('Y-m-d\TH:i:s.uT', Log::ISO_8601_MICROSECONDS); + } +} diff --git a/tests/src/Unit/PageTest.php b/tests/src/Unit/PageTest.php index bcc478b..c186fef 100644 --- a/tests/src/Unit/PageTest.php +++ b/tests/src/Unit/PageTest.php @@ -42,4 +42,23 @@ public function testSerialize() $deserialized = $serializer->deserialize($serialized, Page::class, 'json'); $this->assertEquals($page, $deserialized); } + + public function testGetSetId() + { + $page = (new Page())->setId('page_123'); + $this->assertEquals('page_123', $page->getId()); + } + + public function testGetSetTitle() + { + $page = (new Page())->setTitle('Test Page Title'); + $this->assertEquals('Test Page Title', $page->getTitle()); + } + + public function testGetSetPageTimings() + { + $pageTimings = new PageTimings(); + $page = (new Page())->setPageTimings($pageTimings); + $this->assertSame($pageTimings, $page->getPageTimings()); + } } diff --git a/tests/src/Unit/Repository/HarFileRepositoryTest.php b/tests/src/Unit/Repository/HarFileRepositoryTest.php new file mode 100644 index 0000000..5f205d4 --- /dev/null +++ b/tests/src/Unit/Repository/HarFileRepositoryTest.php @@ -0,0 +1,86 @@ +repository = $this->getHarFileRepository(); + } + + public function testLoad() + { + $har = $this->repository->load('www.softwareishard.com-single-entry.har'); + $this->assertInstanceOf(Har::class, $har); + $this->assertNotNull($har->getLog()); + } + + public function testLoadJson() + { + $json = $this->repository->loadJson('www.softwareishard.com-single-entry.har'); + $this->assertIsString($json); + $this->assertNotEmpty($json); + $decoded = json_decode($json, true); + $this->assertIsArray($decoded); + $this->assertArrayHasKey('log', $decoded); + } + + public function testGetIds() + { + $ids = $this->repository->getIds(); + $this->assertIsArray($ids); + $this->assertNotEmpty($ids); + $this->assertContains('www.softwareishard.com-single-entry.har', $ids); + $this->assertContains('www.softwareishard.com-multiple-entries.har', $ids); + } + + public function testGetIdsSorted() + { + $ids = $this->repository->getIds(); + $sorted = $ids; + sort($sorted, \SORT_NATURAL); + $this->assertEquals($sorted, $ids); + } + + public function testLoadMultipleWithIds() + { + $ids = ['www.softwareishard.com-single-entry.har', 'www.softwareishard.com-multiple-entries.har']; + $generator = $this->repository->loadMultiple($ids); + $this->assertInstanceOf(\Generator::class, $generator); + + $hars = iterator_to_array($generator); + $this->assertCount(2, $hars); + $this->assertArrayHasKey('www.softwareishard.com-single-entry.har', $hars); + $this->assertInstanceOf(Har::class, $hars['www.softwareishard.com-single-entry.har']); + } + + public function testLoadMultipleWithoutIds() + { + $generator = $this->repository->loadMultiple(); + $this->assertInstanceOf(\Generator::class, $generator); + + $hars = iterator_to_array($generator); + $this->assertNotEmpty($hars); + foreach ($hars as $har) { + $this->assertInstanceOf(Har::class, $har); + } + } + + public function testLoadJsonThrowsExceptionForInvalidFile() + { + $this->expectException(\RuntimeException::class); + $this->repository->loadJson('non-existent-file.har'); + } +} diff --git a/tests/src/Unit/RequestTest.php b/tests/src/Unit/RequestTest.php index aff10b4..5ea31ea 100644 --- a/tests/src/Unit/RequestTest.php +++ b/tests/src/Unit/RequestTest.php @@ -114,4 +114,49 @@ public function testFromPsr7ServerRequest() // Verify body $this->assertEquals('name=value', $har_request->getPostData()->getText()); } + + public function testIsResponseCached() + { + $request = (new Request()) + ->setBodySize(0); + $this->assertTrue($request->isResponseCached()); + + $request->setBodySize(100); + $this->assertFalse($request->isResponseCached()); + } + + public function testHasPostData() + { + $request = new Request(); + $this->assertFalse($request->hasPostData()); + + $request->setPostData(new PostData()); + $this->assertTrue($request->hasPostData()); + } + + public function testGetSetUrl() + { + $uri = new Uri('https://www.example.com/path'); + $request = (new Request())->setUrl($uri); + $this->assertSame($uri, $request->getUrl()); + } + + public function testGetQueryString() + { + $request = new Request(); + $this->assertEquals([], $request->getQueryString()); + + $queryParams = [ + (new \Deviantintegral\Har\Params())->setName('foo')->setValue('bar'), + (new \Deviantintegral\Har\Params())->setName('baz')->setValue('qux'), + ]; + $request->setQueryString($queryParams); + $this->assertEquals($queryParams, $request->getQueryString()); + } + + public function testGetSetMethod() + { + $request = (new Request())->setMethod('POST'); + $this->assertEquals('POST', $request->getMethod()); + } } diff --git a/tests/src/Unit/ResponseTest.php b/tests/src/Unit/ResponseTest.php index 442cf7c..5132e39 100644 --- a/tests/src/Unit/ResponseTest.php +++ b/tests/src/Unit/ResponseTest.php @@ -4,9 +4,14 @@ namespace Deviantintegral\Har\Tests\Unit; +use Deviantintegral\Har\Content; use Deviantintegral\Har\Header; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Uri; +/** + * @covers \Deviantintegral\Har\Response + */ class ResponseTest extends HarTestBase { public function testFromPsr7() @@ -21,4 +26,64 @@ public function testFromPsr7() $this->assertEquals('HTTP/2.0', $response->getHttpVersion()); $this->assertEquals('Who needs reasons?', $response->getStatusText()); } + + public function testGetSetStatus() + { + $response = (new \Deviantintegral\Har\Response())->setStatus(404); + $this->assertEquals(404, $response->getStatus()); + } + + public function testGetSetStatusText() + { + $response = (new \Deviantintegral\Har\Response())->setStatusText('Not Found'); + $this->assertEquals('Not Found', $response->getStatusText()); + } + + public function testGetSetContent() + { + $content = (new Content())->setText('test content'); + $response = (new \Deviantintegral\Har\Response())->setContent($content); + $this->assertSame($content, $response->getContent()); + } + + public function testGetSetRedirectURL() + { + $uri = new Uri('https://www.example.com/redirect'); + $response = (new \Deviantintegral\Har\Response())->setRedirectURL($uri); + $this->assertSame($uri, $response->getRedirectURL()); + } + + public function testSerialize() + { + $serializer = $this->getSerializer(); + $content = (new Content())->setText('test'); + $uri = new Uri('https://www.example.com/redirect'); + + $response = (new \Deviantintegral\Har\Response()) + ->setStatus(301) + ->setStatusText('Moved Permanently') + ->setContent($content) + ->setRedirectURL($uri) + ->setBodySize(4) + ->setHttpVersion('HTTP/1.1') + ->setComment('Test redirect'); + + $serialized = $serializer->serialize($response, 'json'); + $decoded = json_decode($serialized, true); + + $this->assertEquals(301, $decoded['status']); + $this->assertEquals('Moved Permanently', $decoded['statusText']); + $this->assertEquals('https://www.example.com/redirect', $decoded['redirectURL']); + $this->assertEquals('Test redirect', $decoded['comment']); + + $deserialized = $serializer->deserialize( + $serialized, + \Deviantintegral\Har\Response::class, + 'json' + ); + $this->assertEquals($response->getStatus(), $deserialized->getStatus()); + $this->assertEquals($response->getStatusText(), $deserialized->getStatusText()); + $this->assertEquals((string) $response->getRedirectURL(), (string) $deserialized->getRedirectURL()); + $this->assertEquals($response->getComment(), $deserialized->getComment()); + } } From 1bdde3241e50745ea6f548c16df307e7f05d0592 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Sun, 14 Dec 2025 20:11:27 -0500 Subject: [PATCH 2/3] fix: check if a HAR repository exists before loading --- src/Repository/HarFileRepository.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Repository/HarFileRepository.php b/src/Repository/HarFileRepository.php index b873669..bf100d6 100644 --- a/src/Repository/HarFileRepository.php +++ b/src/Repository/HarFileRepository.php @@ -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) { From 01422bbb2a8635c2b083f9636bd77a2694fb69eb Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Sun, 14 Dec 2025 20:11:49 -0500 Subject: [PATCH 3/3] test: fix using mocks instead of stubs --- tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php b/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php index 85a8e64..c8308d5 100644 --- a/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php +++ b/tests/src/Unit/Handler/DateFormatInterfaceHandlerTest.php @@ -64,8 +64,8 @@ public function testSerializeNullDateTime() $nullDateTime = new NullDateTime(); // Create a mock visitor that won't be called for NullDateTime - $visitor = $this->createMock(\JMS\Serializer\Visitor\SerializationVisitorInterface::class); - $context = $this->createMock(\JMS\Serializer\SerializationContext::class); + $visitor = $this->createStub(\JMS\Serializer\Visitor\SerializationVisitorInterface::class); + $context = $this->createStub(\JMS\Serializer\SerializationContext::class); $result = $this->handler->serializeDateTimeFormatInterface($visitor, $nullDateTime, [], $context); $this->assertEquals('', $result);