From a2a549cc95f7ed2ea108b7d324aa791ac468a53f Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Sun, 21 Jun 2020 15:34:26 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Lighthouse.php | 64 ++++++++++++++++++++++------------- tests/Helpers.php | 18 +++++----- tests/Unit/LighthouseTest.php | 39 +++++++++++---------- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/src/Lighthouse.php b/src/Lighthouse.php index c167009..57e5f2d 100644 --- a/src/Lighthouse.php +++ b/src/Lighthouse.php @@ -40,8 +40,10 @@ public function __construct() /** * @param string $url - * @return string + * * @throws AuditFailedException + * + * @return string */ public function audit(string $url): string { @@ -49,7 +51,7 @@ public function audit(string $url): string $process->setTimeout($this->timeout)->run(); - if (! $process->isSuccessful()) { + if (!$process->isSuccessful()) { throw new AuditFailedException($url, $process->getErrorOutput()); } @@ -57,9 +59,10 @@ public function audit(string $url): string } /** - * Enable the accessibility audit + * Enable the accessibility audit. * * @param bool $enable + * * @return $this */ public function accessibility(bool $enable = true): self @@ -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 @@ -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 @@ -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 @@ -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 @@ -122,7 +129,7 @@ public function seo(bool $enable = true): self } /** - * Disables Device Emulator + * Disables Device Emulator. * * @return $this */ @@ -149,6 +156,7 @@ public function disableNetworkThrottling(): self /** * @param string $path + * * @return $this */ public function withConfig(string $path): self @@ -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 @@ -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]; } @@ -191,6 +200,7 @@ public function setOutput($path, $format = null): self /** * @param string $format + * * @return $this */ public function setDefaultFormat(string $format): self @@ -202,6 +212,7 @@ public function setDefaultFormat(string $format): self /** * @param string $path + * * @return $this */ public function setNodePath(string $path): self @@ -213,6 +224,7 @@ public function setNodePath(string $path): self /** * @param string $path + * * @return $this */ public function setLighthousePath(string $path): self @@ -224,6 +236,7 @@ public function setLighthousePath(string $path): self /** * @param string $path + * * @return $this */ public function setChromePath(string $path): self @@ -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 @@ -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 @@ -299,6 +314,7 @@ public function setOption($option, $value = null): self /** * @param string $url + * * @return string */ public function getCommand($url): string @@ -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 @@ -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 */ @@ -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); @@ -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 */ @@ -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; } diff --git a/tests/Helpers.php b/tests/Helpers.php index 7a1a734..847cd3c 100644 --- a/tests/Helpers.php +++ b/tests/Helpers.php @@ -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]); } @@ -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; -} \ No newline at end of file +} diff --git a/tests/Unit/LighthouseTest.php b/tests/Unit/LighthouseTest.php index d156c5d..0157b38 100644 --- a/tests/Unit/LighthouseTest.php +++ b/tests/Unit/LighthouseTest.php @@ -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); }); @@ -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'); @@ -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 () { @@ -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) { @@ -138,7 +137,7 @@ $lighthouse = new MockLighthouse(); $lighthouse->setHeaders([ - 'Cookie' => 'monster=blue', + 'Cookie' => 'monster=blue', 'Authorization' => 'Bearer: ring', ]);