diff --git a/README.md b/README.md index 7f7bace..1b94c10 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Lighthouse +![Packagist Version](https://img.shields.io/packagist/v/octoper/lighthouse-php) +![Packagist Downloads](https://img.shields.io/packagist/dm/octoper/lighthouse-php) +![License](https://img.shields.io/github/license/octoper/lighthouse-php) + + **This repository is a fork of** [dzava/lighthouse-php](https://github.com/dzava/lighthouse-php) **repository** This package provides a PHP interface for [Google Lighthouse](https://github.com/GoogleChrome/lighthouse). diff --git a/tests/Datasets/FileOutputData.php b/tests/Datasets/FileOutputData.php new file mode 100644 index 0000000..28e998a --- /dev/null +++ b/tests/Datasets/FileOutputData.php @@ -0,0 +1,6 @@ +assertArraySubset($expectedCategory, $categories); + } else { + test()->assertContains($expectedCategory, $categories); + } +} + +function assertReportDoesNotIncludeCategory($report, $expectedCategory) +{ + $report = json_decode($report, true); + $categories = array_map(function ($category) { + return $category['title']; + }, $report['categories']); + + return test()->assertNotContains($expectedCategory, $categories); +} + +function assertReportContainsHeader($report, $name, $value) +{ + $report = json_decode($report, true); + + $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, ', ') . ']'); + + return test()->assertEquals($value, $headers[$name]); +} + +function removeTempFile($path) +{ + if (file_exists($path)) { + unlink($path); + } + + return test(); +} + +function assertFileStartsWith($prefix, $outputPath) +{ + test()->assertStringStartsWith( + $prefix, + file_get_contents($outputPath), + "Failed asserting that the file '$outputPath' starts with '$prefix'" + ); + + return test(); +} + +function createLighthouseConfig($categories) +{ + if (! is_array($categories)) { + $categories = [$categories]; + } + + $config = tmpfile(); + + $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/Integration/LighthouseTest.php b/tests/Integration/LighthouseTest.php index 89586e4..bf462a4 100644 --- a/tests/Integration/LighthouseTest.php +++ b/tests/Integration/LighthouseTest.php @@ -7,95 +7,10 @@ $this->lighthouse = (new Lighthouse())->setLighthousePath('./node_modules/.bin/lighthouse'); }); -function assertReportIncludesCategory($report, $expectedCategory) -{ - $report = json_decode($report, true); - $categories = array_map(function ($category) { - return $category['title']; - }, $report['categories']); - - if (is_array($expectedCategory)) { - sort($expectedCategory); - sort($categories); - test()->assertArraySubset($expectedCategory, $categories); - } else { - test()->assertContains($expectedCategory, $categories); - } -} - -function assertReportDoesNotIncludeCategory($report, $expectedCategory) -{ - $report = json_decode($report, true); - $categories = array_map(function ($category) { - return $category['title']; - }, $report['categories']); - - return test()->assertNotContains($expectedCategory, $categories); -} - -function assertReportContainsHeader($report, $name, $value) -{ - $report = json_decode($report, true); - - $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, ', ') . ']'); - - return test()->assertEquals($value, $headers[$name]); -} - -function removeTempFile($path) -{ - if (file_exists($path)) { - unlink($path); - } - - return test(); -} - -function assertFileStartsWith($prefix, $outputPath) -{ - test()->assertStringStartsWith( - $prefix, - file_get_contents($outputPath), - "Failed asserting that the file '$outputPath' starts with '$prefix'" - ); - - return test(); -} - -function fileOutputDataProvider() -{ - return [ - ['/tmp/report.json', '{'], - ['/tmp/report.html', '