adrema/tests/Feature/Pdf/GenerateTest.php

255 lines
9.0 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;
2022-11-07 16:18:11 +01:00
use App\Letter\BillDocument;
use App\Letter\DocumentFactory;
2022-11-08 15:39:38 +01:00
use App\Letter\Letter;
2022-11-17 23:59:43 +01:00
use App\Letter\LetterSettings;
use App\Letter\Page;
2021-07-15 21:18:13 +02:00
use App\Member\Member;
use App\Nationality;
use App\Payment\Subscription;
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' => [
[
2022-11-07 16:18:11 +01:00
'factory' => fn (MemberFactory $member) => $member,
2021-07-15 21:18:13 +02:00
'payments' => [],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
2022-11-07 16:18:11 +01:00
'type' => BillDocument::class,
2021-07-15 21:18:13 +02:00
'filename' => null,
],
'bill_for_single_member_when_no_bill_received_yet' => [
'members' => [
[
2022-11-07 16:18:11 +01:00
'factory' => fn (MemberFactory $member) => $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' => [
2022-11-07 16:18:11 +01:00
fn (PaymentFactory $payment) => $payment
2021-07-15 21:18:13 +02:00
->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,
2022-11-07 16:18:11 +01:00
'type' => BillDocument::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' => [
[
2022-11-07 16:18:11 +01:00
'factory' => fn (MemberFactory $member) => $member
2021-07-17 17:13:16 +02:00
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
]),
'payments' => [
2022-11-07 16:18:11 +01:00
fn (PaymentFactory $payment) => $payment
2021-07-17 17:13:16 +02:00
->nr('A')->notPaid()->subscription('::subName::', 1500),
],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
2022-11-07 16:18:11 +01:00
'type' => BillDocument::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' => [
[
2022-11-07 16:18:11 +01:00
'factory' => fn (MemberFactory $member) => $member
2021-07-17 18:10:13 +02:00
->state([
'firstname' => '::firstname1::',
'lastname' => '::lastname::',
'address' => '::address::',
'zip' => '12345',
'location' => '::location::',
]),
'payments' => [
2022-11-07 16:18:11 +01:00
fn (PaymentFactory $payment) => $payment
2021-07-17 18:10:13 +02:00
->nr('::nr::')->notPaid()->subscription('::subName::', 1500),
],
],
[
2022-11-07 16:18:11 +01:00
'factory' => fn (MemberFactory $member) => $member
2021-07-17 18:10:13 +02:00
->state([
'firstname' => '::firstname2::',
'lastname' => '::lastname::',
'address' => '::address::',
'zip' => '12345',
'location' => '::location::',
]),
'payments' => [
2022-11-07 16:18:11 +01:00
fn (PaymentFactory $payment) => $payment
2021-07-17 18:10:13 +02:00
->nr('::nr2::')->notPaid()->subscription('::subName2::', 1600),
],
],
],
'urlCallable' => fn (Collection $members): int => $members->first()->id,
2022-11-07 16:18:11 +01:00
'type' => BillDocument::class,
2021-07-17 18:10:13 +02:00
'filename' => 'rechnung-fur-lastname.pdf',
'output' => [
'::nr::',
'::nr2::',
'::subName::',
'::subName2::',
],
],
2021-07-15 21:18:13 +02:00
];
}
2022-11-08 15:39:38 +01:00
/**
* @dataProvider generatorProvider
*
* @param class-string<Letter> $type
*/
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);
2022-11-07 16:18:11 +01:00
$repo = app(DocumentFactory::class)->fromSingleRequest($type, $member);
2021-07-16 00:12:19 +02:00
2022-03-11 20:19:17 +01:00
if (null === $filename) {
2021-07-17 15:16:29 +02:00
$this->assertNull($repo);
2021-07-16 00:12:19 +02:00
return;
}
2022-11-07 16:18:11 +01:00
$content = $repo->renderBody();
2021-07-17 15:16:29 +02:00
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();
2022-08-30 23:35:14 +02:00
$this->login()->init()->loginNami();
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
2022-03-11 20:19:17 +01:00
if (null === $filename) {
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'));
2022-03-11 20:19:17 +01:00
$this->assertEquals('inline; filename="'.$filename.'"', $response->headers->get('content-disposition'));
2021-07-16 00:12:19 +02:00
}
2022-11-17 23:59:43 +01:00
/**
* @testWith ["App\\Letter\\BillDocument"]
* ["App\\Letter\\RememberDocument"]
*/
public function testTheDocumentHasSettings(string $type): void
{
LetterSettings::fake([
'from_long' => 'langer Stammesname',
'from' => 'Stammeskurz',
'mobile' => '+49 176 55555',
'email' => 'max@muster.de',
'website' => 'https://example.com',
'address' => 'Musterstr 4',
'place' => 'Münster',
'zip' => '12345',
'iban' => 'DE444',
'bic' => 'SOLSSSSS',
]);
$member = Member::factory()->defaults()->create();
$document = new $type(collect([new Page(Member::get())]));
$document->assertHasContent('langer Stammesname');
$document->assertHasContent('Stammeskurz');
$document->assertHasContent('+49 176 55555');
$document->assertHasContent('max@muster.de');
$document->assertHasContent('https://example.com');
$document->assertHasContent('Musterstr 4');
$document->assertHasContent('Münster');
$document->assertHasContent('12345');
$document->assertHasContent('DE444');
$document->assertHasContent('SOLSSSSS');
}
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');
});
}
}