Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 28, 2023
1 parent 1f860ef commit f729fa1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
37 changes: 32 additions & 5 deletions src/FakePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ class FakePdf extends Pdf
/** @var array<int, \Spatie\LaravelPdf\Pdf> */
protected array $respondedWithPdf = [];

public function assertViewIs(string $viewName): self
/** @var array<int, \Spatie\LaravelPdf\Pdf> */
protected array $savedPdfs = [];

public function assertViewIs(string $viewName): void
{
Assert::assertEquals($viewName, $this->viewName);
foreach ($this->savedPdfs as $savedPdf) {
if ($savedPdf['pdf']->viewName === $viewName) {
$this->markAssertionPassed();

return $this;
return;
}
}

Assert::fail("Did not save a PDF that uses view `{$viewName}`");
}

public function toResponse($request): Response
Expand All @@ -29,16 +38,34 @@ public function assertRespondedWithPdf(Closure $expectations): void
{
Assert::assertNotEmpty($this->respondedWithPdf);

foreach($this->respondedWithPdf as $pdf) {
foreach ($this->respondedWithPdf as $pdf) {

$result = $expectations($pdf);

if ($result === true) {
$this->markAssertionPassed();

return;
}

}

Assert::fail('Did not respond with a PDF that matched the expectations');
}

public function save(string $path): self
{
$this->getBrowsershot();

$this->savedPdfs[] = [
'pdf' => $this,
'path' => $path,
];

return $this;
}

protected function markAssertionPassed(): void
{
Assert::assertTrue(true);
}
}
15 changes: 11 additions & 4 deletions tests/FakePdfTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?php

use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\ExpectationFailedException;
use Spatie\LaravelPdf\Facades\Pdf;
use function \Spatie\LaravelPdf\Support\pdf;

it('can determine the view that was used', function () {
beforeEach(function () {
Pdf::fake();
});

Pdf::view('test');
it('can determine the view that was used', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertViewIs('test');
});

it('can determine the data that was passed to the view', function () {
Pdf::fake();
it('can determine the view that was not used', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertViewIs('this-view-does-not-exist');
})->fails();

it('can determine the data that was passed to the view', function () {
Route::get('pdf', function() {
return pdf('test')->inline();
});
Expand Down
7 changes: 7 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExpectationFailedException;
use Spatie\Image\Image;
use Spatie\LaravelPdf\Tests\TestCase;
use Spatie\TemporaryDirectory\TemporaryDirectory;
Expand Down Expand Up @@ -84,3 +86,8 @@ function convertPdfToImage(string $pdfPath): string

return $imagePath;
}

function fails()
{
test()->expectException(AssertionFailedError::class);
}

0 comments on commit f729fa1

Please sign in to comment.