Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Add some basic PDF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasravnsborg committed Nov 9, 2018
1 parent e53a72d commit 913fa50
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ function generate_pdf() {

Find more information to `SetProtection()` here: https://mpdf.github.io/reference/mpdf-functions/setprotection.html

## Testing

To use the testing suite, you need some extensions and binaries for your local PHP. On macOS, you can install them like this:

```
pecl install imagick
brew install ghostscript
```

## License

Laravel PDF is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "Generate PDFs in Laravel with this mPDF wrapper.",
"keywords": ["mpdf", "pdf", "laravel"],
"license": "MIT",
"scripts": {
"test": "phpunit --colors=always"
},
"require": {
"php": ">=7.0",
"mpdf/mpdf": "^7.0"
Expand All @@ -12,6 +15,11 @@
"niklasravnsborg\\LaravelPdf\\": "src/LaravelPdf"
}
},
"autoload-dev": {
"psr-4": {
"niklasravnsborg\\LaravelPdf\\Test\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -21,5 +29,9 @@
"PDF": "niklasravnsborg\\LaravelPdf\\Facades\\Pdf"
}
}
},
"require-dev": {
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "^3.7"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
51 changes: 51 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace niklasravnsborg\LaravelPdf\Test;

use PDF;
use Imagick;

class PdfTest extends TestCase
{
public function testSimplePdfIsCorrect()
{
$pdf = PDF::loadHTML('<p>This gets tested!</p>');
$this->compareToSnapshot('simple', $pdf->output());
}

public function testExposifyPdfExposeIsCorrect()
{
$pdf = PDF::loadFile('tests/views/exposify-expose.html');
$this->compareToSnapshot('exposify', $pdf->output());
}

protected function compareToSnapshot($snapshotId, $data)
{
$snapshotFile = "tests/snapshots/{$snapshotId}.pdf";

// create snapshot if it doesn't exist
if (!file_exists($snapshotFile)) {
file_put_contents($snapshotFile, $data);
return;
}

$snapshot = file_get_contents($snapshotFile);
$this->assertPdfsLookTheSame($snapshot, $data);
}

public function assertPdfsLookTheSame($pdf1, $pdf2, $message = '')
{
$assertedImagick = new Imagick();
$assertedImagick->readImageBlob($pdf1);
$assertedImagick->resetIterator();
$assertedImagick = $assertedImagick->appendImages(true);
$testImagick = new Imagick();
$testImagick->readImageBlob($pdf2);
$testImagick->resetIterator();
$testImagick = $testImagick->appendImages(true);

$diff = $assertedImagick->compareImages($testImagick, 1);
$pdfsLookTheSame = 0.0 == $diff[1];
self::assertTrue($pdfsLookTheSame, 'Failed asserting that PDFs look the same.');
}
}
31 changes: 31 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace niklasravnsborg\LaravelPdf\Test;

use niklasravnsborg\LaravelPdf\Facades\Pdf;
use niklasravnsborg\LaravelPdf\PdfServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

class TestCase extends OrchestraTestCase {
/**
* Load package service provider
* @param \Illuminate\Foundation\Application $app
* @return lasselehtinen\MyPackage\MyPackageServiceProvider
*/
protected function getPackageProviders($app)
{
return [PdfServiceProvider::class];
}

/**
* Load package alias
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageAliases($app)
{
return [
'PDF' => Pdf::class,
];
}
}
Binary file added tests/snapshots/exposify.pdf
Binary file not shown.
Binary file added tests/snapshots/simple.pdf
Binary file not shown.
Loading

0 comments on commit 913fa50

Please sign in to comment.