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
8 changes: 8 additions & 0 deletions src/Har.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function setLog(Log $log): self
return $this;
}

/**
* Deep clone the Log object when cloning Har.
*/
public function __clone(): void
{
$this->log = clone $this->log;
}

/**
* Return a generator that returns cloned HARs with one per HAR entry.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,27 @@

return $this;
}

/**
* Deep clone all object properties when cloning Log.
*/
public function __clone(): void
{
if (isset($this->creator)) {
$this->creator = clone $this->creator;
}

if (isset($this->browser)) {
$this->browser = clone $this->browser;

Check warning on line 127 in src/Log.php

View workflow job for this annotation

GitHub Actions / Annotate

src/Log.php#L127

This line is not covered by a test
}

// Deep clone arrays of objects
if (isset($this->pages)) {
$this->pages = array_map(fn (Page $page) => clone $page, $this->pages);
}

if (isset($this->entries)) {
$this->entries = array_map(fn (Entry $entry) => clone $entry, $this->entries);
}
}
}
48 changes: 48 additions & 0 deletions tests/src/Unit/HarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,54 @@ public function testSplitLogEntries()
}
}

public function testCloneIsDeep()
{
$repository = $this->getHarFileRepository();
$har = $repository->load('www.softwareishard.com-multiple-entries.har');

$originalEntryCount = \count($har->getLog()->getEntries());
$this->assertGreaterThan(1, $originalEntryCount);

// Clone the HAR
$cloned = clone $har;

// Verify the clone has a different Log instance
$this->assertNotSame($har->getLog(), $cloned->getLog());

// Modify the cloned HAR's entries
$cloned->getLog()->setEntries([]);

// Verify the original HAR's entries are unchanged
$this->assertCount($originalEntryCount, $har->getLog()->getEntries());
$this->assertCount(0, $cloned->getLog()->getEntries());
}

public function testCloneBrowserIsDeep()
{
$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;

// Verify the Browser object is a different instance
$this->assertNotSame($har->getLog()->getBrowser(), $cloned->getLog()->getBrowser());

// 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());
$this->assertSame('modified-version', $cloned->getLog()->getBrowser()->getVersion());
}

private function removeCustomFields(array &$a)
{
foreach ($a as &$value) {
Expand Down