adrema/tests/Feature/Pdf/GenerateTest.php

217 lines
7.8 KiB
PHP
Raw Normal View History

2021-07-15 21:18:13 +02:00
<?php
namespace Tests\Feature\Pdf;
use App\Country;
use App\Fee;
use App\Group;
use App\Member\Member;
use App\Nationality;
use App\Payment\Subscription;
use App\Pdf\BillType;
2021-07-16 00:12:19 +02:00
use App\Pdf\PdfGenerator;
use App\Pdf\PdfRepositoryFactory;
2021-07-17 17:13:16 +02:00
use Carbon\Carbon;
2021-07-15 21:18:13 +02:00
use Database\Factories\Member\MemberFactory;
use Database\Factories\Payment\PaymentFactory;
2022-02-12 01:08:56 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2021-07-15 21:18:13 +02:00
use Illuminate\Support\Collection;
use Storage;
use Tests\TestCase;
class GenerateTest extends TestCase
{
2022-02-12 01:08:56 +01:00
use DatabaseTransactions;
2021-07-15 21:18:13 +02:00
public function setUp(): void
{
parent::setUp();
2021-07-17 17:13:16 +02:00
Carbon::setTestNow(Carbon::parse('2021-04-15 00:00:00'));
2021-07-15 21:18:13 +02:00
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
2021-07-17 15:16:29 +02:00
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
'address' => '::street::',
'zip' => '::zip::',
'location' => '::location::',
]),
2021-07-15 21:18:13 +02:00
'payments' => [
fn (PaymentFactory $payment): PaymentFactory => $payment
->notPaid()
->nr('1995')
2021-07-17 15:16:29 +02:00
->subscription('::subName::', 1500),
2021-07-15 21:18:13 +02:00
],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
'type' => BillType::class,
2021-07-17 17:29:59 +02:00
'filename' => 'rechnung-fur-lastname.pdf',
2021-07-15 21:18:13 +02:00
'output' => [
2021-07-17 15:16:29 +02:00
'Rechnung',
'15.00',
2021-07-17 17:21:30 +02:00
'Beitrag 1995 für ::firstname:: ::lastname:: (::subName::)',
2021-07-17 17:29:59 +02:00
'Mitgliedsbeitrag für ::lastname::',
2021-07-17 15:16:29 +02:00
'Familie ::lastname::\\\\::street::\\\\::zip:: ::location::',
2021-07-15 21:18:13 +02:00
],
],
2021-07-17 17:13:16 +02:00
'bill_has_deadline' => [
'members' => [
[
'factory' => fn (MemberFactory $member): MemberFactory => $member
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
]),
'payments' => [
fn (PaymentFactory $payment): PaymentFactory => $payment
->nr('A')->notPaid()->subscription('::subName::', 1500),
],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
'type' => BillType::class,
2021-07-17 17:29:59 +02:00
'filename' => 'rechnung-fur-lastname.pdf',
2021-07-17 17:13:16 +02:00
'output' => [
'29.04.2021',
],
],
2021-07-17 18:10:13 +02:00
'families' => [
'members' => [
[
'factory' => fn (MemberFactory $member): MemberFactory => $member
->state([
'firstname' => '::firstname1::',
'lastname' => '::lastname::',
'address' => '::address::',
'zip' => '12345',
'location' => '::location::',
]),
'payments' => [
fn (PaymentFactory $payment): PaymentFactory => $payment
->nr('::nr::')->notPaid()->subscription('::subName::', 1500),
],
],
[
'factory' => fn (MemberFactory $member): MemberFactory => $member
->state([
'firstname' => '::firstname2::',
'lastname' => '::lastname::',
'address' => '::address::',
'zip' => '12345',
'location' => '::location::',
]),
'payments' => [
fn (PaymentFactory $payment): PaymentFactory => $payment
->nr('::nr2::')->notPaid()->subscription('::subName2::', 1600),
],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
'type' => BillType::class,
'filename' => 'rechnung-fur-lastname.pdf',
'output' => [
'::nr::',
'::nr2::',
'::subName::',
'::subName2::',
],
],
2021-07-15 21:18:13 +02:00
];
}
/** @dataProvider generatorProvider */
2021-07-17 15:16:29 +02:00
public function testItGeneratesTheLayout(
2021-07-15 21:18:13 +02:00
array $members,
callable $urlCallable,
string $type,
?string $filename = null,
?array $output = null
): void {
$this->withoutExceptionHandling();
2021-11-23 01:02:18 +01:00
$this->login()->init();
2021-07-16 00:12:19 +02:00
$members = $this->setupMembers($members);
2021-07-15 21:18:13 +02:00
2021-07-16 00:12:19 +02:00
$urlId = call_user_func($urlCallable, $members);
2021-07-17 15:16:29 +02:00
$member = Member::find($urlId);
$repo = app(PdfRepositoryFactory::class)->fromSingleRequest($type, $member);
2021-07-16 00:12:19 +02:00
if ($filename === null) {
2021-07-17 15:16:29 +02:00
$this->assertNull($repo);
2021-07-16 00:12:19 +02:00
return;
}
2021-07-17 15:16:29 +02:00
$content = app(PdfGenerator::class)->setRepository($repo)->compileView();
foreach ($output as $out) {
$this->assertStringContainsString($out, $content);
}
2021-07-16 00:12:19 +02:00
}
/** @dataProvider generatorProvider */
2021-07-17 15:16:29 +02:00
public function testItGeneratesAPdf(
2021-07-16 00:12:19 +02:00
array $members,
callable $urlCallable,
string $type,
?string $filename = null,
?array $output = null
): void {
$this->withoutExceptionHandling();
2021-11-23 01:02:18 +01:00
$this->login()->init();
2021-07-16 00:12:19 +02:00
$members = $this->setupMembers($members);
$urlId = call_user_func($urlCallable, $members);
2021-07-17 15:16:29 +02:00
$response = $this->call('GET', "/member/{$urlId}/pdf", [
'type' => $type,
]);
2021-07-16 00:12:19 +02:00
if ($filename === null) {
2021-07-17 15:16:29 +02:00
$response->assertStatus(204);
2021-07-16 00:12:19 +02:00
return;
}
2021-07-17 15:16:29 +02:00
$this->assertEquals('application/pdf', $response->headers->get('content-type'));
$this->assertEquals('inline; filename="' . $filename . '"', $response->headers->get('content-disposition'));
2021-07-16 00:12:19 +02:00
}
private function setupMembers(array $members): Collection
{
return collect($members)->map(function (array $member): Member {
2021-07-15 21:18:13 +02:00
$memberFactory = Member::factory()
->for(Nationality::factory())
->for(Subscription::factory()->for(Fee::factory()))
2021-07-17 16:11:17 +02:00
->withPayments(data_get($member, 'payments', []))
2021-07-15 21:18:13 +02:00
->forCountry(Country::find(5))
->for(Group::factory());
$memberModel = call_user_func($member['factory'], $memberFactory)->create();
return $memberModel->load('payments');
});
}
}