From b389fae4b161415786685db4399064044befc0a6 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 15 Jul 2021 21:18:13 +0200 Subject: [PATCH] fixup! Add PDF engine --- tests/Feature/Pdf/GenerateTest.php | 121 +++++++++++++++++++++++++++++ tests/Traits/FakesTex.php | 18 +++++ 2 files changed, 139 insertions(+) create mode 100644 tests/Feature/Pdf/GenerateTest.php create mode 100644 tests/Traits/FakesTex.php diff --git a/tests/Feature/Pdf/GenerateTest.php b/tests/Feature/Pdf/GenerateTest.php new file mode 100644 index 00000000..24c924cd --- /dev/null +++ b/tests/Feature/Pdf/GenerateTest.php @@ -0,0 +1,121 @@ +fakeTex(); + Storage::fake('temp'); + } + + public function generatorProvider(): array + { + return [ + 'no_pdf_when_no_bill' => [ + 'members' => [ + [ + 'factory' => fn (MemberFactory $member): MemberFactory => $member, + 'payments' => [], + ], + ], + 'urlCallable' => fn (Collection $members): int => $members->first()->id, + 'type' => BillType::class, + 'filename' => null, + ], + 'bill_for_single_member_when_no_bill_received_yet' => [ + 'members' => [ + [ + 'factory' => fn (MemberFactory $member): MemberFactory => $member + ->state(['firstname' => '::firstname::', 'lastname' => '::lastname::']), + 'payments' => [ + fn (PaymentFactory $payment): PaymentFactory => $payment + ->notPaid() + ->nr('1995') + ->subscription('::subName::', 1200), + ], + ], + ], + 'urlCallable' => fn (Collection $members): int => $members->first()->id, + 'type' => BillType::class, + 'filename' => 'rechnung-fur-firstname-lastname.pdf', + 'output' => [ + '12,00 €', + 'Familie ::lastname::', + ], + ], + ]; + } + + /** @dataProvider generatorProvider */ + public function testItGeneratesAPdf( + array $members, + callable $urlCallable, + string $type, + ?string $filename = null, + ?array $output = null + ): void { + $this->withoutExceptionHandling(); + $this->login(); + + $members = collect($members)->map(function (array $member): Member { + $memberFactory = Member::factory() + ->for(Nationality::factory()) + ->for(Subscription::factory()->for(Fee::factory())) + ->forCountry(Country::find(5)) + ->for(Group::factory()); + $memberModel = call_user_func($member['factory'], $memberFactory)->create(); + + foreach (data_get($member, 'payments', []) as $payment) { + $paymentFactory = Payment::factory()->for($memberModel); + $paymentFactory = call_user_func($payment, $paymentFactory); + $paymentFactory->create(); + } + + return $memberModel->load('payments'); + }); + + $urlId = call_user_func($urlCallable, $members); + $response = $this->post("/member/{$urlId}/pdf", [ + 'type' => $type, + ]); + + if ($filename === null) { + $response->assertStatus(204); + $this->assertTexCount(0); + + return; + } + + $this->assertEquals('application/pdf', $response->headers->get('content-type')); + $this->assertTrue('attachment; filename="' . $filename . '"', $response->headers->get('content-disposition')); + + foreach ($output as $out) { + $this->assertTexGeneratedWith($out); + } + } + +} diff --git a/tests/Traits/FakesTex.php b/tests/Traits/FakesTex.php new file mode 100644 index 00000000..e30ed8d4 --- /dev/null +++ b/tests/Traits/FakesTex.php @@ -0,0 +1,18 @@ +