adrema/tests/Feature/Pdf/GenerateTest.php

153 lines
4.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\Payment;
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-15 21:18:13 +02:00
use Database\Factories\Member\MemberFactory;
use Database\Factories\Payment\PaymentFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
use Storage;
use Tests\TestCase;
use Tests\Traits\FakesTex;
class GenerateTest extends TestCase
{
use RefreshDatabase;
public function setUp(): void
{
parent::setUp();
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,
'filename' => 'rechnung-fur-firstname-lastname.pdf',
'output' => [
2021-07-17 15:16:29 +02:00
'Rechnung',
'15.00',
'Beitrag für 1995 (::subName::)',
'Familie ::lastname::\\\\::street::\\\\::zip:: ::location::',
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();
$this->login();
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();
$this->login();
$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');
});
}
}