Skip to content

Commit

Permalink
added badges in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Jun 9, 2020
1 parent 71f8509 commit 3142e0b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 104 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
6 changes: 6 additions & 0 deletions tests/Datasets/FileOutputData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

dataset('fileOutputData', [
['/tmp/report.json', '{'],
['/tmp/report.html', '<!--'],
]);
78 changes: 78 additions & 0 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

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 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;
}
123 changes: 19 additions & 104 deletions tests/Integration/LighthouseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<!--'],
];
}

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;
}

it('can run only one audit', function () {
$report = $this->lighthouse
->performance()
->audit('https://octoper.me');
->audit('http://example.com');

assertReportIncludesCategory($report, 'Performance');
assertReportDoesNotIncludeCategory($report, 'Progressive Web App');
Expand All @@ -118,21 +33,21 @@ function createLighthouseConfig($categories)
test('updates the config when a category is added or removed', function () {
$report = $this->lighthouse
->performance()
->audit('https://octoper.me');
->audit('http://example.com');

assertReportIncludesCategory($report, 'Performance');
assertReportDoesNotIncludeCategory($report, 'Accessibility');

$report = $this->lighthouse
->accessibility()
->audit('https://octoper.me');
->audit('http://example.com');

assertReportIncludesCategory($report, 'Performance');
assertReportIncludesCategory($report, 'Accessibility');

$report = $this->lighthouse
->accessibility(false)
->audit('https://octoper.me');
->audit('http://example.com');

assertReportIncludesCategory($report, 'Performance');
assertReportDoesNotIncludeCategory($report, 'Accessibility');
Expand All @@ -146,52 +61,52 @@ function createLighthouseConfig($categories)
->withConfig($configPath)
->accessibility()
->performance(false)
->audit('https://octoper.me');
->audit('http://example.com');

file_put_contents('/tmp/report', $report);

assertReportIncludesCategory($report, 'Performance');
assertReportDoesNotIncludeCategory($report, 'Accessibility');
});

test('throws_an_exception_when_the_audit_fails', function () {
test('throws an exception when the audit fails', function () {
$url = 'not-a-valid-url';

$this->lighthouse
->seo()
->audit($url);
})->throws(AuditFailedException::class);

// test('outputs_to_a_file', function () {
// $this->removeTempFile($outputPath);
test('outputs_to_a_file', function ($outputPath, $content) {
removeTempFile($outputPath);

// $this->lighthouse
// ->setOutput($outputPath)
// ->seo()
// ->audit('http://example.com');
$this->lighthouse
->setOutput($outputPath)
->seo()
->audit('http://example.com');

// $this->assertFileExists($outputPath);
// assertFileStartsWith($content, $outputPath);
// });
$this->assertFileExists($outputPath);
assertFileStartsWith($content, $outputPath);
})->with('fileOutputData');

test('outputs_both_json_and_html_reports_at_the_same_time', function () {
test('outputs both json and html reports at the same time', function () {
removeTempFile('/tmp/example.report.json');
removeTempFile('/tmp/example.report.html');

$this->lighthouse
->setOutput('/tmp/example', ['json', 'html'])
->seo()
->audit('https://octoper.me');
->audit('http://example.com');

assertFileExists('/tmp/example.report.html');
assertFileExists('/tmp/example.report.json');
});

test('passes_the_http_headers_to_the_requests', function () {
test('passes the http headers to the requests', function () {
$report = $this->lighthouse
->setHeaders(['Cookie' => 'monster:blue', 'Authorization' => 'Bearer: ring'])
->performance()
->audit('https://octoper.me');
->audit('http://example.com');

assertReportContainsHeader($report, 'Cookie', 'monster:blue');
assertReportContainsHeader($report, 'Authorization', 'Bearer: ring');
Expand Down

0 comments on commit 3142e0b

Please sign in to comment.