Skip to content

Commit

Permalink
add assertDontSee test method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-Cowin committed Oct 28, 2024
1 parent 8c6d394 commit 1cd34c7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/FakePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert;

class FakePdfBuilder extends PdfBuilder
Expand Down Expand Up @@ -105,27 +106,24 @@ public function assertSaved(string|callable $path): void

public function assertSee(string|array $text): void
{
if (is_string($text)) {
$text = [$text];
}
$values = Arr::wrap($text);

foreach ($this->savedPdfs as $savedPdf) {
foreach ($text as $singleText) {
if (! str_contains($savedPdf['pdf']->html, $singleText)) {
break 2; // jump out of the inner foreach loop
}
foreach ($values as $value) {
foreach ($this->savedPdfs as $savedPdf) {
Assert::assertStringContainsString((string) $value, $savedPdf['pdf']->html);
}

$this->markAssertionPassed();

return;
}
}

$texts = collect($text)
->wrap('`')
->join(',', ', and ');
public function assertDontSee(string|array $text): void
{
$values = Arr::wrap($text);

Assert::fail("Did not save a PDF that contains {$texts}");
foreach ($values as $value) {
foreach ($this->savedPdfs as $savedPdf) {
Assert::assertStringNotContainsString((string) $value, $savedPdf['pdf']->html);
}
}
}

public function assertRespondedWithPdf(Closure $expectations): void
Expand Down
41 changes: 41 additions & 0 deletions tests/FakePdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,47 @@
]);
})->fails();

it('can determine that the pdf content does not contain a unexpected string', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertDontSee('this-string-does-not-exist');
});

it('can determine that the pdf content contains a expected string', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertDontSee('test');
})->fails();

it('can determine that the pdf content does not contain multiple unexpected strings', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertDontSee([
'this-string-is-not-present-in-the-pdf',
'this-string-is-not-present-in-the-pdf-as-well',
'this-string-is-not-present-in-the-pdf-either',
]);
});

it('can determine that the pdf content contains multiple expected strings', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertDontSee([
'This',
'test',
]);
})->fails();

it('can determine that the pdf content contain an unexpected string between expected strings', function () {
Pdf::view('test')->save('my-custom-name.pdf');

Pdf::assertDontSee([
'this',
'this-string-is-not-present-in-the-pdf',
'test',
]);
})->fails();

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

Expand Down

0 comments on commit 1cd34c7

Please sign in to comment.