diff --git a/src/PdfBuilder.php b/src/PdfBuilder.php index 580753d..309a066 100755 --- a/src/PdfBuilder.php +++ b/src/PdfBuilder.php @@ -137,7 +137,7 @@ public function footerHtml(string $html): self public function download(?string $downloadName = null): self { - $this->name($downloadName ?? 'download'); + $this->downloadName ?? $this->name($downloadName ?? 'download'); $this->addHeaders([ 'Content-Type' => 'application/pdf', diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index f323505..3dbc18f 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -24,3 +24,43 @@ ->viewName->toBe('foo') ->viewData->toBe(['bar' => 'bax']); }); + +test('the `pdf` function name accepts a name parameter and sets downloadName', function () { + Pdf::fake(); + + expect(pdf('foo', ['bar' => 'bax'])->name('baz.pdf')) + ->toBeInstanceOf(FakePdfBuilder::class) + ->viewName->toBe('foo') + ->viewData->toBe(['bar' => 'bax']) + ->downloadName->toBe('baz.pdf'); +}); + +test('the `pdf` function download accepts a name parameter and sets downloadName', function () { + Pdf::fake(); + + expect(pdf('foo', ['bar' => 'bax'])->download('baz.pdf')) + ->toBeInstanceOf(FakePdfBuilder::class) + ->viewName->toBe('foo') + ->viewData->toBe(['bar' => 'bax']) + ->downloadName->toBe('baz.pdf'); +}); + +test('the `pdf` function name accepts a name parameter and sets downloadName while calling download', function () { + Pdf::fake(); + + expect(pdf('foo', ['bar' => 'bax'])->name('baz.pdf')->download()) + ->toBeInstanceOf(FakePdfBuilder::class) + ->viewName->toBe('foo') + ->viewData->toBe(['bar' => 'bax']) + ->downloadName->toBe('baz.pdf'); +}); + +test('the `pdf` function download assigns the default to downloadName when no name is specified', function () { + Pdf::fake(); + + expect(pdf('foo', ['bar' => 'bax'])->download()) + ->toBeInstanceOf(FakePdfBuilder::class) + ->viewName->toBe('foo') + ->viewData->toBe(['bar' => 'bax']) + ->downloadName->toBe('download.pdf'); +});