Skip to content

Commit

Permalink
Update used AWS Lambda Layers (#113)
Browse files Browse the repository at this point in the history
* Update Layer Versions

* Add assertPdfsAreSimilar

* Update PDF Reference File

* Update Test

* Don't run PDF comparison on GitHub Actions
  • Loading branch information
stefanzweifel authored Feb 17, 2024
1 parent 081c391 commit ae65392
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
coverage: pcov
ini-values: pcov.directory=src

- name: Fix Imagick Policy for PDFs
run: |
sudo sed -i 's/<policy domain="coder" rights="none" pattern="PDF" \/>/<policy domain="coder" rights="read|write" pattern="PDF" \/>/g' /etc/ImageMagick-6/policy.xml
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
Expand All @@ -60,7 +64,7 @@ jobs:
SIDECAR_EXECUTION_ROLE: ${{secrets.SIDECAR_EXECUTION_ROLE}}

- name: Execute tests
run: vendor/bin/pest --coverage
run: vendor/bin/pest --coverage --exclude-group=pdf-comparison
env:
SIDECAR_ACCESS_KEY_ID: ${{secrets.SIDECAR_ACCESS_KEY_ID}}
SIDECAR_SECRET_ACCESS_KEY: ${{secrets.SIDECAR_SECRET_ACCESS_KEY}}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"ext-imagick": "*",
"larastan/larastan": "^1.0|^2.0",
"laravel/pint": "^1.13",
"league/flysystem-aws-s3-v3": "^1.0|^2.0|^3.0",
Expand Down
4 changes: 2 additions & 2 deletions config/sidecar-browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
* @see https://github.com/stefanzweifel/sidecar-browsershot-layer
*/
'layers' => [
// "arn:aws:lambda:us-east-1:821527532446:layer:sidecar-browsershot-layer:1",
// "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:37",
// "arn:aws:lambda:us-east-1:821527532446:layer:sidecar-browsershot-layer:2",
// "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:42",
],

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Functions/BrowsershotFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ public function layers()
$region = config('sidecar.aws_region');

if ($region === 'ap-northeast-2') {
$chromeAwsLambdaVersion = 36;
$chromeAwsLambdaVersion = 41;
} else {
$chromeAwsLambdaVersion = 37;
$chromeAwsLambdaVersion = 42;
}

// Add Layers that each contain `puppeteer-core` and `@sparticuz/chromium`
// https://github.com/stefanzweifel/sidecar-browsershot-layer
// https://github.com/shelfio/chrome-aws-lambda-layer
return [
"arn:aws:lambda:{$region}:821527532446:layer:sidecar-browsershot-layer:1",
"arn:aws:lambda:{$region}:821527532446:layer:sidecar-browsershot-layer:2",
"arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:{$chromeAwsLambdaVersion}",
];
}
Expand Down
5 changes: 3 additions & 2 deletions tests/BrowsershotLambdaReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
it('generates reference hello-world-with-smileys.pdf', function () {
$pdf = BrowsershotLambda::html('<h1 style="font-weight: 400;">Hello world!! 👋🦫🫠</h1>')->pdf();
$pdfWithCorrectDates = $this->updateCreationDateAndModDateOfPdf($pdf);

$reference = file_get_contents(__DIR__.'/references/hello-world-with-smileys.pdf');

$this->assertEquals($reference, $pdfWithCorrectDates);
});
$this->assertPdfsAreSimilar($reference, $pdfWithCorrectDates, threshold: 200);
})->group('pdf-comparison');
24 changes: 24 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Hammerstone\Sidecar\Providers\SidecarServiceProvider;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Imagick;
use Orchestra\Testbench\TestCase as Orchestra;
use PHPUnit\Framework\Assert;
use Wnx\SidecarBrowsershot\Functions\BrowsershotFunction;
use Wnx\SidecarBrowsershot\SidecarBrowsershotServiceProvider;

Expand Down Expand Up @@ -51,4 +53,26 @@ protected function updateCreationDateAndModDateOfPdf(string $pdf): string
"/ModDate (D:20230101000000+00'00')>>\n",
], $pdf, limit: 1);
}

public function assertPdfsAreSimilar(string $expected, string $actual, float $threshold = 0): void
{
$expectedPdf = new Imagick();
$expectedPdf->readImageBlob($expected);
$expectedPdf->resetIterator();
$expectedPdf = $expectedPdf->appendImages(true);

$actualPdf = new Imagick();
$actualPdf->readImageBlob($actual);
$actualPdf->resetIterator();
$actualPdf = $actualPdf->appendImages(true);

$diff = $expectedPdf->compareImages($actualPdf, imagick::METRIC_ABSOLUTEERRORMETRIC);

$diffValue = $diff[1];

// Assert that the difference is less than or equal to the threshold
Assert::assertLessThanOrEqual($threshold, $diffValue, sprintf(
'The PDFs are not similar (Difference: %d)', $diffValue
));
}
}
Binary file modified tests/references/hello-world-with-smileys.pdf
Binary file not shown.
Binary file modified tests/references/hello-world-with-smileys.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ae65392

Please sign in to comment.