Mod Pdf generateTest

This commit is contained in:
philipp lang 2022-12-06 21:25:47 +01:00
parent 530b3fa3c3
commit 7ee4fe5166
3 changed files with 156 additions and 255 deletions

@ -1 +1 @@
Subproject commit 68d4f38e319da9d1353d59a614723f3de14bc50f
Subproject commit f075f67526bf0650a04fd2d3dd60ebbd74d43675

View File

@ -0,0 +1,155 @@
<?php
namespace Tests\Feature\Letter;
use App\Letter\BillDocument;
use App\Letter\DocumentFactory;
use App\Letter\LetterSettings;
use App\Letter\RememberDocument;
use App\Member\Member;
use App\Payment\Payment;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use Zoomyboy\Tex\Tex;
class DocumentFactoryTest extends TestCase
{
use DatabaseTransactions;
/**
* @testWith ["\\App\\Letter\\BillDocument"]
* ["\\App\\Letter\\RememberDocument"]
*/
public function testItDoesntReturnARepositoryWhenMemberDoesntHavePayments(): void
{
$member = Member::factory()->defaults()->create();
$letter = app(DocumentFactory::class)->fromSingleRequest(BillDocument::class, $member);
$this->assertNull($letter);
}
public function testItDisplaysMemberInformation(): void
{
$member = Member::factory()
->defaults()
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
'address' => '::street::',
'zip' => '::zip::',
'location' => '::location::',
])
->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', 1500))
->create();
$letter = app(DocumentFactory::class)->fromSingleRequest(BillDocument::class, $member);
$letter->assertHasAllContent([
'Rechnung',
'15.00',
'Beitrag 1995 für ::firstname:: ::lastname:: (::subName::)',
'Mitgliedsbeitrag für ::lastname::',
'Familie ::lastname::\\\\::street::\\\\::zip:: ::location::',
]);
}
public function testBillSetsFilename(): void
{
$member = Member::factory()
->defaults()
->state(['lastname' => '::lastname::'])
->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', 1500))
->create();
$letter = app(DocumentFactory::class)->fromSingleRequest(BillDocument::class, $member);
$this->assertEquals('rechnung-fur-lastname.pdf', $letter->compiledFilename());
}
public function testRememberSetsFilename(): void
{
$member = Member::factory()
->defaults()
->state(['lastname' => '::lastname::'])
->has(Payment::factory()->notPaid())
->create();
$letter = app(DocumentFactory::class)->fromSingleRequest(RememberDocument::class, $member);
$this->assertEquals('zahlungserinnerung-fur-lastname.pdf', $letter->compiledFilename());
}
public function testItCreatesOneFileForFamilyMembers(): void
{
$firstMember = Member::factory()
->defaults()
->state(['firstname' => 'Max1', 'lastname' => '::lastname::', 'address' => '::address::', 'zip' => '12345', 'location' => '::location::'])
->has(Payment::factory()->notPaid()->nr('nr1'))
->create();
Member::factory()
->defaults()
->state(['firstname' => 'Max2', 'lastname' => '::lastname::', 'address' => '::address::', 'zip' => '12345', 'location' => '::location::'])
->has(Payment::factory()->notPaid()->nr('nr2'))
->create();
$letter = app(DocumentFactory::class)->fromSingleRequest(BillDocument::class, $firstMember);
$letter->assertHasAllContent(['Max1', 'Max2', 'nr1', 'nr2']);
}
/**
* @testWith ["App\\Letter\\BillDocument"]
* ["App\\Letter\\RememberDocument"]
*/
public function testItDisplaysSettings(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()
->has(Payment::factory()->notPaid()->nr('nr2'))
->create();
$letter = app(DocumentFactory::class)->fromSingleRequest($type, $member);
$letter->assertHasAllContent([
'langer Stammesname',
'Stammeskurz',
'+49 176 55555',
'max@muster.de',
'https://example.com',
'Musterstr 4',
'Münster',
'12345',
'DE444',
'SOLSSSSS',
]);
}
public function testItGeneratesAPdf(): void
{
Tex::fake();
$member = Member::factory()
->defaults()
->has(Payment::factory()->notPaid())
->create(['lastname' => 'lastname']);
$this->withoutExceptionHandling();
$this->login()->init()->loginNami();
$response = $this->call('GET', "/member/{$member->id}/pdf", [
'type' => BillDocument::class,
]);
$this->assertEquals('application/pdf', $response->headers->get('content-type'));
$this->assertEquals('inline; filename="rechnung-fur-lastname.pdf"', $response->headers->get('content-disposition'));
}
}

View File

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