adrema/tests/Feature/Invoice/DocumentFactoryTest.php

193 lines
6.4 KiB
PHP
Raw Normal View History

2022-12-06 21:25:47 +01:00
<?php
2023-04-18 22:08:45 +02:00
namespace Tests\Feature\Invoice;
use App\Invoice\BillDocument;
use App\Invoice\DocumentFactory;
use App\Invoice\InvoiceSettings;
use App\Invoice\Queries\InvoiceMemberQuery;
use App\Invoice\Queries\SingleMemberQuery;
use App\Invoice\RememberDocument;
2022-12-06 21:25:47 +01:00
use App\Member\Member;
use App\Payment\Payment;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-12-13 23:11:32 +01:00
use Tests\RequestFactories\Child;
2022-12-06 21:25:47 +01:00
use Tests\TestCase;
use Zoomyboy\Tex\Tex;
class DocumentFactoryTest extends TestCase
{
use DatabaseTransactions;
/**
2023-04-18 22:08:45 +02:00
* @testWith ["\\App\\Invoice\\BillDocument"]
* ["\\App\\Invoice\\RememberDocument"]
2022-12-06 21:25:47 +01:00
*/
public function testItDoesntReturnARepositoryWhenMemberDoesntHavePayments(): void
{
$member = Member::factory()->defaults()->create();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(BillDocument::class, $this->query($member));
$this->assertNull($invoice);
2022-12-06 21:25:47 +01:00
}
public function testItDisplaysMemberInformation(): void
{
$member = Member::factory()
->defaults()
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
'address' => '::street::',
'zip' => '::zip::',
'location' => '::location::',
])
2022-12-13 23:11:32 +01:00
->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', [
new Child('a', 1000),
new Child('a', 500),
]))
2022-12-06 21:25:47 +01:00
->create();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(BillDocument::class, $this->query($member));
2022-12-06 21:25:47 +01:00
2023-04-18 22:08:45 +02:00
$invoice->assertHasAllContent([
2022-12-06 21:25:47 +01:00
'Rechnung',
'15.00',
2022-12-14 00:23:03 +01:00
'::subName:: 1995 für ::firstname:: ::lastname::',
2022-12-06 21:25:47 +01:00
'Mitgliedsbeitrag für ::lastname::',
'Familie ::lastname::\\\\::street::\\\\::zip:: ::location::',
]);
}
2022-12-14 00:23:03 +01:00
public function testItDisplaysSplitPayments(): void
{
$member = Member::factory()
->defaults()
->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
])
->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', [
new Child('a', 1000),
new Child('b', 500),
], ['split' => true]))
->create();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(BillDocument::class, $this->query($member));
2022-12-14 00:23:03 +01:00
2023-04-18 22:08:45 +02:00
$invoice->assertHasAllContent([
2022-12-14 00:23:03 +01:00
'Rechnung',
'10.00',
'5.00',
'::subName:: (a) 1995 für ::firstname:: ::lastname::',
'::subName:: (b) 1995 für ::firstname:: ::lastname::',
'Mitgliedsbeitrag für ::lastname::',
]);
}
2022-12-06 21:25:47 +01:00
public function testBillSetsFilename(): void
{
$member = Member::factory()
->defaults()
->state(['lastname' => '::lastname::'])
2022-12-13 23:11:32 +01:00
->has(Payment::factory()->notPaid()->nr('1995'))
2022-12-06 21:25:47 +01:00
->create();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(BillDocument::class, $this->query($member));
2022-12-06 21:25:47 +01:00
2023-04-18 22:08:45 +02:00
$this->assertEquals('rechnung-fur-lastname.pdf', $invoice->compiledFilename());
2022-12-06 21:25:47 +01:00
}
public function testRememberSetsFilename(): void
{
$member = Member::factory()
->defaults()
->state(['lastname' => '::lastname::'])
->has(Payment::factory()->notPaid())
->create();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(RememberDocument::class, $this->query($member));
2022-12-06 21:25:47 +01:00
2023-04-18 22:08:45 +02:00
$this->assertEquals('zahlungserinnerung-fur-lastname.pdf', $invoice->compiledFilename());
2022-12-06 21:25:47 +01:00
}
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();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice(BillDocument::class, $this->query($firstMember));
2022-12-06 21:25:47 +01:00
2023-04-18 22:08:45 +02:00
$invoice->assertHasAllContent(['Max1', 'Max2', 'nr1', 'nr2']);
2022-12-06 21:25:47 +01:00
}
/**
2023-04-18 22:08:45 +02:00
* @testWith ["App\\Invoice\\BillDocument"]
* ["App\\Invoice\\RememberDocument"]
2022-12-06 21:25:47 +01:00
*/
public function testItDisplaysSettings(string $type): void
{
2023-04-18 22:08:45 +02:00
InvoiceSettings::fake([
2022-12-06 21:25:47 +01:00
'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();
2023-04-18 22:08:45 +02:00
$invoice = app(DocumentFactory::class)->singleInvoice($type, $this->query($member));
2022-12-06 21:25:47 +01:00
2023-04-18 22:08:45 +02:00
$invoice->assertHasAllContent([
2022-12-06 21:25:47 +01:00
'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'));
}
2022-12-07 00:40:53 +01:00
2023-04-18 22:08:45 +02:00
private function query(Member $member): InvoiceMemberQuery
2022-12-07 00:40:53 +01:00
{
return new SingleMemberQuery($member);
}
2022-12-06 21:25:47 +01:00
}