-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#19: Now transforming HAR in monitor.
- Loading branch information
1 parent
e1410e6
commit 382bbb8
Showing
8 changed files
with
1,198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JourneyMonitor\Monitor\JobRunner; | ||
|
||
class HarTransformer | ||
{ | ||
/** | ||
* @param string $originalHar The original HAR JSON structure | ||
* @param string[] $urls Array of URLs that defines where to split the HAR into multiple pages | ||
* @return string The transformed HAR JSON structure | ||
*/ | ||
public function splitIntoMultiplePages(string $originalHar, $urls) | ||
{ | ||
$harObject = json_decode($originalHar); | ||
$urls = $this->normalizeUrls($urls); | ||
|
||
$pageStartedDateTimes = []; | ||
$pageStartedDateTimes[] = $harObject->log->entries[0]->startedDateTime; | ||
$currentIndex = 0; | ||
|
||
foreach ($harObject->log->entries as $entry) { | ||
foreach ($urls as $index => $url) { | ||
if ($index > $currentIndex && $url !== $urls[$currentIndex] && $entry->request->url === $url) { | ||
$pageStartedDateTimes[] = $entry->startedDateTime; | ||
$currentIndex = $index; | ||
} | ||
} | ||
$entry->pageref = 'Request '. ($currentIndex + 1) . ': ' . $urls[$currentIndex]; | ||
} | ||
|
||
$pages = []; | ||
foreach ($pageStartedDateTimes as $index => $pageStartedDateTime) { | ||
$page = new \stdClass(); | ||
$page->id = 'Request '. ($index + 1) . ': ' . $urls[$index]; | ||
$page->startedDateTime = $pageStartedDateTime; | ||
$page->title = 'Request '. ($index + 1) . ': ' . $urls[$index]; | ||
$pageTimings = new \stdClass(); | ||
$pageTimings->comment = ''; | ||
$page->pageTimings = $pageTimings; | ||
$page->comment = ''; | ||
$pages[] = $page; | ||
} | ||
|
||
$harObject->log->pages = $pages; | ||
return json_encode($harObject); | ||
} | ||
|
||
private function normalizeUrls($urls) | ||
{ | ||
$normalizedUrls = []; | ||
foreach ($urls as $url) { | ||
if (strstr($url, '#')) { | ||
$url = substr($url, 0, strpos($url, '#')); | ||
} | ||
$normalizedUrls[] = $url; | ||
} | ||
return $normalizedUrls; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/JourneyMonitor/Monitor/JobRunner/HarTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace JourneyMonitor\Monitor\JobRunner; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
class HarTransformerTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testSplitIntoMultiplePages() | ||
{ | ||
$originalHar = file_get_contents(__DIR__ . '/../../../fixtures/testrun.original.har.json'); | ||
$transformedHar = file_get_contents(__DIR__ . '/../../../fixtures/testrun.transformed.har.json'); | ||
|
||
// Working around the fact that the JSON in the file is "beautified". | ||
$transformedHar = json_encode(json_decode($transformedHar)); | ||
|
||
$urls = [ | ||
0 => 'https://www.galeria-kaufhof.de/', | ||
1 => 'https://www.galeria-kaufhof.de/search?q=hose', | ||
2 => 'https://www.galeria-kaufhof.de/', | ||
]; | ||
|
||
$ht = new HarTransformer(); | ||
|
||
$this->assertEquals($transformedHar, $ht->splitIntoMultiplePages($originalHar, $urls)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.