Testing PDFs | laravel-pdf | Spatie

 SPATIE

  Laravel PDF
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf](https://spatie.be/docs/laravel-pdf/v2)  Basic-usage  Testing PDFs

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-pdf/v2) [v1](https://spatie.be/docs/laravel-pdf/v1)

- [ Introduction ](https://spatie.be/docs/laravel-pdf/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf/v2/questions-issues)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf/v2/alternatives)
- [ Changelog ](https://spatie.be/docs/laravel-pdf/v2/changelog)
- [ About us ](https://spatie.be/docs/laravel-pdf/v2/about-us)

Basic usage
-----------

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/saving-pdfs-to-disks)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/setting-defaults)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-pdf/v2/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf/v2/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-cloudflare-driver)
- [ Using the DOMPDF driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-dompdf-driver)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf/v2/drivers/generating-pdfs-on-aws-lambda)
- [ Using the Gotenberg driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-gotenberg-driver)
- [ Using the WeasyPrint driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-weasyprint-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-pdf/v2/drivers/custom-drivers)

Advanced usage
--------------

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/extending-with-macros)

 Testing PDFs
============

###  On this page

1. [ assertSaved ](#content-assertsaved)
2. [ assertRespondedWithPdf ](#content-assertrespondedwithpdf)
3. [ Simple assertion methods ](#content-simple-assertion-methods)
4. [ Queued PDF assertions ](#content-queued-pdf-assertions)

In your test, you can call the `fake()` method on the `Pdf` facade to fake the PDF generation. Because the PDF generation is faked, your tests will run much faster.

```
// in your test

use Spatie\LaravelPdf\Facades\Pdf;

beforeEach(function () {
    Pdf::fake();
});
```

assertSaved
-----------------------------------------------------------------------------------------

You can use the `assertSaved` method to assert that a PDF was saved with specific properties. You should pass it a callable which will received an instance of `Spatie\LaravelPdf\PdfBuilder`. If the callable returns `true`, the assertion will pass.

```
use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\PdfBuilder;

Pdf::assertSaved(function (PdfBuilder $pdf) {
    return $pdf->downloadName === 'invoice.pdf'
        && str_contains($pdf->html, 'Your total for April is $10.00');
});
```

If you want to assert that a PDF was saved to a specific path, you accept the path as a second parameter of the callable.

```
use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\PdfBuilder;

Pdf::assertSaved(function (PdfBuilder $pdf, string $path) {
    return $path === storage_path('invoices/invoice.pdf');
});
```

assertRespondedWithPdf
--------------------------------------------------------------------------------------------------------------------------

The `assertRespondedWithPdf` method can be used to assert that a PDF was generated and returned as a response.

Imagine you have this route:

```
use Spatie\LaravelPdf\Facades\Pdf;

Route::get('download-invoice', function () {
    return pdf('pdf.invoice')->download('invoice-for-april-2022.pdf');
});
```

In your test for this route you can use the `assertRespondedWithPdf` to make sure that a PDF was generated and returned as a download. You can even make assertions on the content of the PDF.

```
use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\PdfBuilder;

it('can download an invoice', function () {
    $this
        ->get('download-invoice')
        ->assertOk();

    Pdf::assertRespondedWithPdf(function (PdfBuilder $pdf) {
        return $pdf->downloadName === 'invoice-for-april-2022.pdf'
            && $pdf->isDownload()
            && str_contains($pdf->html, 'Your total for April is $10.00');
    });
});
```

### Asserting metadata

You can assert that a PDF was generated with specific metadata by inspecting the `metadata` property on the `PdfBuilder` instance.

```
use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\PdfBuilder;

Pdf::assertSaved(function (PdfBuilder $pdf) {
    return $pdf->metadata->title === 'Invoice #123'
        && $pdf->metadata->author === 'Acme Corp';
});
```

Simple assertion methods
--------------------------------------------------------------------------------------------------------------------------------

Beside the methods listed above, there are a few simple assertion methods that can be used to assert that a PDF was generated. They are meant to test code that generated a single PDF. The assertions will pass if any of the generated PDFs match the assertion.

If your code generates multiple PDFs, it's better to use the `assertSaved` method.

### assertViewIs

You can use the `assertViewIs` method to assert that a PDF was generated using a specific view.

```
Pdf::assertViewIs('pdf.invoice');
```

### assertSee

You can use the `assertSee` method to assert that a PDF was generated that contains a given string.

```
Pdf::assertSee('Your total for April is $10.00');
```

You can pass an array of strings to assert that all of them are present in the PDF.

```
Pdf::assertSee([
    'Your total for April is $10.00',
    'Your total for May is $20.00',
]);
```

### assertViewHas

You can use the `assertViewHas` method to assert that a PDF was generated that was passed a specific key in its view data.

```
Pdf::assertViewHas('invoice');
```

As a second parameter you can pass the expected value.

```
Pdf::assertViewHas('invoice', $invoice);
```

### assertSaved

You can use the `assertSaved` method to assert that a PDF was saved to the specified path.

```
Pdf::assertSaved(storage_path('invoices/invoice.pdf'));
```

Queued PDF assertions
-----------------------------------------------------------------------------------------------------------------------

### assertQueued

You can use the `assertQueued` method to assert that a PDF was queued for generation. You can pass a string path or a callable.

```
Pdf::assertQueued('invoice.pdf');
```

With a callable for more detailed assertions:

```
use Spatie\LaravelPdf\PdfBuilder;

Pdf::assertQueued(function (PdfBuilder $pdf, string $path) {
    return $path === 'invoice.pdf' && $pdf->contains('Total');
});
```

### assertNotQueued

You can use the `assertNotQueued` method to assert that no PDFs were queued, or that a specific path was not queued.

```
// Assert nothing was queued
Pdf::assertNotQueued();

// Assert a specific path was not queued
Pdf::assertNotQueued('other.pdf');
```
