adrema/tests/Feature/Invoice/InvoiceSendActionTest.php

113 lines
4.5 KiB
PHP
Raw Normal View History

2022-11-07 16:18:11 +01:00
<?php
2023-04-18 22:08:45 +02:00
namespace Tests\Feature\Invoice;
2022-11-07 16:18:11 +01:00
2023-04-18 22:08:45 +02:00
use App\Invoice\Actions\InvoiceSendAction;
use App\Invoice\BillDocument;
2023-12-18 01:15:16 +01:00
use App\Invoice\BillKind;
use App\Invoice\Enums\InvoiceStatus;
use App\Invoice\InvoiceSettings;
2023-12-18 01:15:16 +01:00
use App\Invoice\Mails\BillMail;
2023-12-18 02:17:31 +01:00
use App\Invoice\Mails\RememberMail;
2023-12-18 01:15:16 +01:00
use App\Invoice\Models\Invoice;
use App\Invoice\Models\InvoicePosition;
2023-12-18 02:17:31 +01:00
use App\Invoice\RememberDocument;
2022-11-07 16:18:11 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-02-18 01:22:58 +01:00
use Illuminate\Support\Facades\Mail;
2022-11-07 16:18:11 +01:00
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
use Zoomyboy\Tex\Tex;
2023-04-18 22:08:45 +02:00
class InvoiceSendActionTest extends TestCase
2022-11-07 16:18:11 +01:00
{
use DatabaseTransactions;
2023-12-18 02:17:31 +01:00
public function testItSendsInvoices(): void
2022-11-07 16:18:11 +01:00
{
2023-11-30 23:54:16 +01:00
Mail::fake();
Tex::spy();
2023-02-18 01:22:58 +01:00
Storage::fake('temp');
2023-12-18 01:15:16 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
$invoice = Invoice::factory()
->to(ReceiverRequestFactory::new()->name('Familie Muster'))
2023-12-18 02:17:31 +01:00
->has(InvoicePosition::factory()->withMember(), 'positions')
2023-12-18 01:15:16 +01:00
->via(BillKind::EMAIL)
2023-12-18 01:56:58 +01:00
->create(['mail_email' => 'max@muster.de']);
2022-11-07 16:18:11 +01:00
2023-04-18 22:08:45 +02:00
InvoiceSendAction::run();
2022-11-07 16:18:11 +01:00
2023-12-18 01:56:58 +01:00
Mail::assertSent(BillMail::class, fn ($mail) => $mail->build() && $mail->hasTo('max@muster.de', 'Familie Muster') && Storage::disk('temp')->path('rechnung-fur-familie-muster.pdf') === $mail->filename && Storage::disk('temp')->exists('rechnung-fur-familie-muster.pdf'));
2023-12-18 01:15:16 +01:00
Tex::assertCompiled(BillDocument::class, fn ($document) => 'Familie Muster' === $document->toName);
$this->assertEquals(InvoiceStatus::SENT, $invoice->fresh()->status);
$this->assertEquals(now()->format('Y-m-d'), $invoice->fresh()->sent_at->format('Y-m-d'));
2022-11-07 16:18:11 +01:00
}
2023-12-18 02:17:31 +01:00
public function testItRemembersInvoices(): void
{
Mail::fake();
Tex::spy();
Storage::fake('temp');
$this->withoutExceptionHandling()->login()->loginNami();
$invoice = Invoice::factory()
->to(ReceiverRequestFactory::new()->name('Familie Muster'))
->has(InvoicePosition::factory()->withMember(), 'positions')
->via(BillKind::EMAIL)
->status(InvoiceStatus::SENT)
2023-12-19 02:00:42 +01:00
->create(['sent_at' => now()->subMonths(6), 'mail_email' => 'max@muster.de', 'last_remembered_at' => now()->subMonths(6)]);
2023-12-18 02:17:31 +01:00
InvoiceSendAction::run();
Mail::assertSent(RememberMail::class, fn ($mail) => $mail->build() && $mail->hasTo('max@muster.de', 'Familie Muster') && Storage::disk('temp')->path('zahlungserinnerung-fur-familie-muster.pdf') === $mail->filename && Storage::disk('temp')->exists('zahlungserinnerung-fur-familie-muster.pdf'));
Tex::assertCompiled(RememberDocument::class, fn ($document) => 'Familie Muster' === $document->toName);
$this->assertEquals(now()->format('Y-m-d'), $invoice->fresh()->last_remembered_at->format('Y-m-d'));
}
public function testItDoesntRememberWhenNotDue(): void
{
Mail::fake();
$this->withoutExceptionHandling()->login()->loginNami();
Invoice::factory()
->has(InvoicePosition::factory()->withMember(), 'positions')
->via(BillKind::EMAIL)
->status(InvoiceStatus::SENT)
->create(['sent_at' => now()->subMonths(6), 'last_remembered_at' => now()->subMonth()]);
InvoiceSendAction::run();
Mail::assertNotSent(RememberMail::class);
}
public function testItRemembersWhenWeeksSettingIsReached(): void
{
Mail::fake();
$this->withoutExceptionHandling()->login()->loginNami();
Invoice::factory()
->has(InvoicePosition::factory()->withMember(), 'positions')
->via(BillKind::EMAIL)
->status(InvoiceStatus::SENT)
->create(['sent_at' => now()->subYear(), 'last_remembered_at' => now()->subWeeks(5)->subDay()]);
app(InvoiceSettings::class)->fill(['rememberWeeks' => 6])->save();
InvoiceSendAction::run();
Mail::assertNotSent(RememberMail::class);
app(InvoiceSettings::class)->fill(['rememberWeeks' => 5])->save();
InvoiceSendAction::run();
Mail::assertSent(RememberMail::class);
}
2023-12-18 02:17:31 +01:00
public function testItDoesntSendPostInvoices(): void
{
Mail::fake();
$this->withoutExceptionHandling()->login()->loginNami();
Invoice::factory()
->has(InvoicePosition::factory()->withMember(), 'positions')
->via(BillKind::POST)
->create();
InvoiceSendAction::run();
Mail::assertNotSent(BillMail::class);
}
2022-11-07 16:18:11 +01:00
}