Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Jun 24, 2020
2 parents c158d91 + 65b93c3 commit 8f935c1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
64 changes: 41 additions & 23 deletions src/Lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,29 @@ public function __construct()

/**
* @param string $url
* @return string
*
* @throws AuditFailedException
*
* @return string
*/
public function audit(string $url): string
{
$process = Process::fromShellCommandline($this->getCommand($url));

$process->setTimeout($this->timeout)->run();

if (! $process->isSuccessful()) {
if (!$process->isSuccessful()) {
throw new AuditFailedException($url, $process->getErrorOutput());
}

return $process->getOutput();
}

/**
* Enable the accessibility audit
* Enable the accessibility audit.
*
* @param bool $enable
*
* @return $this
*/
public function accessibility(bool $enable = true): self
Expand All @@ -70,9 +73,10 @@ public function accessibility(bool $enable = true): self
}

/**
* Enable the best practices audit
* Enable the best practices audit.
*
* @param bool $enable
*
* @return $this
*/
public function bestPractices(bool $enable = true): self
Expand All @@ -83,9 +87,10 @@ public function bestPractices(bool $enable = true): self
}

/**
* Enable the best performance audit
* Enable the best performance audit.
*
* @param bool $enable
*
* @return $this
*/
public function performance(bool $enable = true): self
Expand All @@ -96,9 +101,10 @@ public function performance(bool $enable = true): self
}

/**
* Enable the progressive web app audit
* Enable the progressive web app audit.
*
* @param bool $enable
*
* @return $this
*/
public function pwa(bool $enable = true): self
Expand All @@ -109,9 +115,10 @@ public function pwa(bool $enable = true): self
}

/**
* Enable the search engine optimization audit
* Enable the search engine optimization audit.
*
* @param bool $enable
*
* @return $this
*/
public function seo(bool $enable = true): self
Expand All @@ -122,7 +129,7 @@ public function seo(bool $enable = true): self
}

/**
* Disables Device Emulator
* Disables Device Emulator.
*
* @return $this
*/
Expand All @@ -149,6 +156,7 @@ public function disableNetworkThrottling(): self

/**
* @param string $path
*
* @return $this
*/
public function withConfig(string $path): self
Expand All @@ -164,8 +172,9 @@ public function withConfig(string $path): self
}

/**
* @param string $path
* @param string $path
* @param null|string|array $format
*
* @return $this
*/
public function setOutput($path, $format = null): self
Expand All @@ -176,7 +185,7 @@ public function setOutput($path, $format = null): self
$format = $this->guessOutputFormatFromFile($path);
}

if (! is_array($format)) {
if (!is_array($format)) {
$format = [$format];
}

Expand All @@ -191,6 +200,7 @@ public function setOutput($path, $format = null): self

/**
* @param string $format
*
* @return $this
*/
public function setDefaultFormat(string $format): self
Expand All @@ -202,6 +212,7 @@ public function setDefaultFormat(string $format): self

/**
* @param string $path
*
* @return $this
*/
public function setNodePath(string $path): self
Expand All @@ -213,6 +224,7 @@ public function setNodePath(string $path): self

/**
* @param string $path
*
* @return $this
*/
public function setLighthousePath(string $path): self
Expand All @@ -224,6 +236,7 @@ public function setLighthousePath(string $path): self

/**
* @param string $path
*
* @return $this
*/
public function setChromePath(string $path): self
Expand All @@ -234,9 +247,10 @@ public function setChromePath(string $path): self
}

/**
* Set the flags to pass to the spawned Chrome instance
* Set the flags to pass to the spawned Chrome instance.
*
* @param array|string $flags
*
* @return $this
*/
public function setChromeFlags($flags): self
Expand Down Expand Up @@ -275,7 +289,8 @@ public function setTimeout($timeout): self

/**
* @param string $option
* @param mixed $value
* @param mixed $value
*
* @return $this
*/
public function setOption($option, $value = null): self
Expand All @@ -299,6 +314,7 @@ public function setOption($option, $value = null): self

/**
* @param string $url
*
* @return string
*/
public function getCommand($url): string
Expand All @@ -322,10 +338,11 @@ public function getCommand($url): string
}

/**
* Enable or disable a category
* Enable or disable a category.
*
* @param $category
* @param bool $enable
*
* @return $this
*/
protected function setCategory($category, $enable): self
Expand All @@ -344,7 +361,7 @@ protected function setCategory($category, $enable): self
}

/**
* Creates the config file used during the audit
* Creates the config file used during the audit.
*
* @return $this
*/
Expand All @@ -354,12 +371,12 @@ protected function buildConfig(): self
$this->withConfig(stream_get_meta_data($config)['uri']);
$this->config = $config;

$options = 'module.exports = ' . json_encode([
'extends' => 'lighthouse:default',
'settings' => [
'onlyCategories' => $this->categories,
],
]);
$options = 'module.exports = '.json_encode([
'extends' => 'lighthouse:default',
'settings' => [
'onlyCategories' => $this->categories,
],
]);

fwrite($config, $options);

Expand All @@ -368,7 +385,7 @@ protected function buildConfig(): self

/**
* Convert the options array to an array that can be used
* to construct the command arguments
* to construct the command arguments.
*
* @return array
*/
Expand All @@ -380,16 +397,17 @@ protected function processOptions(): array
}

/**
* Guesses the file format
* Guesses the file format.
*
* @param string $path
*
* @return string
*/
private function guessOutputFormatFromFile($path): string
{
$format = pathinfo($path, PATHINFO_EXTENSION);

if (! in_array($format, $this->availableFormats)) {
if (!in_array($format, $this->availableFormats)) {
$format = $this->defaultFormat;
}

Expand Down
18 changes: 9 additions & 9 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function assertReportContainsHeader($report, $name, $value)

$headers = $report['configSettings']['extraHeaders'];
test()->assertNotNull($headers, 'No extra headers found in report');
test()->assertArrayHasKey($name, $headers, "Header '$name' is missing from report. [" . implode($headers, ', ') . ']');
test()->assertArrayHasKey($name, $headers, "Header '$name' is missing from report. [".implode($headers, ', ').']');

return test()->assertEquals($value, $headers[$name]);
}
Expand All @@ -59,20 +59,20 @@ function assertFileStartsWith($prefix, $outputPath)

function createLighthouseConfig($categories)
{
if (! is_array($categories)) {
if (!is_array($categories)) {
$categories = [$categories];
}

$config = tmpfile();

$r = 'module.exports = ' . json_encode([
'extends' => 'lighthouse:default',
'settings' => [
'onlyCategories' => $categories,
],
]);
$r = 'module.exports = '.json_encode([
'extends' => 'lighthouse:default',
'settings' => [
'onlyCategories' => $categories,
],
]);

fwrite($config, $r);

return $config;
}
}
39 changes: 19 additions & 20 deletions tests/Unit/LighthouseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
'lighthouse',
'--output=json',
'--quiet',
"--config-path=/my/config",
"http://example.com",
'--config-path=/my/config',
'http://example.com',
"--chrome-flags='--headless --disable-gpu --no-sandbox'",
]), $command);
});
Expand All @@ -31,7 +31,6 @@
$this->assertStringContainsString('/my/node/binary', $command);
});


it('can_set_chrome_flags', function () {
$this->lighthouse->setChromeFlags('--my-flag');
$command = $this->lighthouse->getCommand('http://example.com');
Expand All @@ -47,7 +46,7 @@

$command = $this->lighthouse->getCommand('http://example.com');

$this->assertStringContainsString("--output-path=/tmp/report.json", $command);
$this->assertStringContainsString('--output-path=/tmp/report.json', $command);
});

it('can_disable_device_emulation', function () {
Expand Down Expand Up @@ -77,41 +76,41 @@
it('can guess the output format from the file extension', function () {
$this->lighthouse->setOutput('/tmp/report.json');
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=json", $command);
$this->assertStringNotContainsString("--output=html", $command);
$this->assertStringContainsString('--output=json', $command);
$this->assertStringNotContainsString('--output=html', $command);

$this->lighthouse->setOutput('/tmp/report.html');
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=html", $command);
$this->assertStringNotContainsString("--output=json", $command);
$this->assertStringContainsString('--output=html', $command);
$this->assertStringNotContainsString('--output=json', $command);

$this->lighthouse->setOutput('/tmp/report.md');
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=json", $command);
$this->assertStringNotContainsString("--output=html", $command);
$this->assertStringContainsString('--output=json', $command);
$this->assertStringNotContainsString('--output=html', $command);

$this->lighthouse->setOutput('/tmp/report');
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=json", $command);
$this->assertStringNotContainsString("--output=html", $command);
$this->assertStringContainsString('--output=json', $command);
$this->assertStringNotContainsString('--output=html', $command);
});

it('can_override_the_output_format', function () {
$this->lighthouse->setOutput('/tmp/report.json', 'html');
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=html", $command);
$this->assertStringNotContainsString("--output=json", $command);
$this->assertStringContainsString('--output=html', $command);
$this->assertStringNotContainsString('--output=json', $command);

$this->lighthouse->setOutput('/tmp/report.md', ['html', 'json']);
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=html", $command);
$this->assertStringContainsString("--output=json", $command);
$this->assertStringContainsString('--output=html', $command);
$this->assertStringContainsString('--output=json', $command);

$this->lighthouse->setOutput('/tmp/report.md', ['html', 'json', 'md']);
$command = $this->lighthouse->getCommand('http://example.com');
$this->assertStringContainsString("--output=html", $command);
$this->assertStringContainsString("--output=json", $command);
$this->assertStringNotContainsString("--output=md", $command);
$this->assertStringContainsString('--output=html', $command);
$this->assertStringContainsString('--output=json', $command);
$this->assertStringNotContainsString('--output=md', $command);
});

it('cannot_add_the_same_category_multiple_times', function ($category, $method = null) {
Expand All @@ -138,7 +137,7 @@
$lighthouse = new MockLighthouse();

$lighthouse->setHeaders([
'Cookie' => 'monster=blue',
'Cookie' => 'monster=blue',
'Authorization' => 'Bearer: ring',
]);

Expand Down

0 comments on commit 8f935c1

Please sign in to comment.