adrema/tests/Feature/Invoice/InvoiceStoreActionTest.php

129 lines
3.7 KiB
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace Tests\Feature\Invoice;
2023-12-16 11:18:00 +01:00
use App\Invoice\BillKind;
2023-12-13 00:35:39 +01:00
use App\Invoice\Enums\InvoiceStatus;
use App\Invoice\Models\Invoice;
use App\Member\Member;
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2023-12-13 00:35:39 +01:00
use Tests\TestCase;
class InvoiceStoreActionTest extends TestCase
{
use DatabaseTransactions;
public function testItCanCreateAnInvoice(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$member = Member::factory()->defaults()->create();
$response = $this->postJson(
route('invoice.store'),
InvoiceRequestFactory::new()
->to(ReceiverRequestFactory::new()->name('Familie Blabla')->address('Musterstr 44')->zip('22222')->location('Solingen'))
->status(InvoiceStatus::PAID)
2023-12-16 11:18:00 +01:00
->via(BillKind::POST)
2023-12-13 00:35:39 +01:00
->state([
'greeting' => 'Hallo Familie',
])
2023-12-17 00:45:03 +01:00
->position(InvoicePositionRequestFactory::new()->description('Beitrag Abc')->price(3250)->member($member))
2023-12-18 01:56:58 +01:00
->create(['mail_email' => 'a@b.de'])
2023-12-13 00:35:39 +01:00
);
$response->assertOk();
$this->assertDatabaseHas('invoices', [
'greeting' => 'Hallo Familie',
2023-12-16 11:18:00 +01:00
'via' => BillKind::POST->value,
2023-12-13 00:35:39 +01:00
'status' => InvoiceStatus::PAID->value,
2023-12-18 01:15:16 +01:00
'mail_email' => 'a@b.de',
2023-12-13 00:35:39 +01:00
]);
$invoice = Invoice::firstWhere('greeting', 'Hallo Familie');
$this->assertDatabaseHas('invoice_positions', [
'invoice_id' => $invoice->id,
'member_id' => $member->id,
'price' => 3250,
'description' => 'Beitrag Abc',
]);
$this->assertEquals([
'name' => 'Familie Blabla',
'address' => 'Musterstr 44',
'zip' => '22222',
'location' => 'Solingen',
], $invoice->to);
}
2024-09-21 17:46:07 +02:00
public static function validationDataProvider(): Generator
2023-12-13 00:35:39 +01:00
{
yield [
['to.address' => ''],
['to.address' => 'Adresse ist erforderlich.']
];
yield [
['to.name' => ''],
['to.name' => 'Name ist erforderlich.']
];
yield [
['to.location' => ''],
['to.location' => 'Ort ist erforderlich.']
];
yield [
['status' => ''],
['status' => 'Status ist erforderlich.']
];
yield [
['status' => 'lala'],
['status' => 'Der gewählte Wert für Status ist ungültig.']
];
yield [
['to.zip' => ''],
['to.zip' => 'PLZ ist erforderlich.']
];
2023-12-16 11:18:00 +01:00
yield [
['via' => ''],
['via' => 'Rechnungsweg ist erforderlich.']
];
yield [
['via' => 'lala'],
['via' => 'Der gewählte Wert für Rechnungsweg ist ungültig.']
];
2023-12-17 23:00:52 +01:00
yield [
['usage' => ''],
['usage' => 'Verwendungszweck ist erforderlich.']
];
2023-12-13 00:35:39 +01:00
}
/**
* @param array<string, mixed> $input
* @param array<string, string> $errors
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
2023-12-13 00:35:39 +01:00
public function testItValidatesInput(array $input, array $errors): void
{
$this->login()->loginNami();
$response = $this->postJson(
route('invoice.store'),
InvoiceRequestFactory::new()
->to(ReceiverRequestFactory::new())
->position(InvoicePositionRequestFactory::new()->member(Member::factory()->defaults()->create()))
2023-12-16 11:18:00 +01:00
->via(BillKind::POST)
2023-12-13 00:35:39 +01:00
->state($input)
->create()
);
$response->assertJsonValidationErrors($errors);
}
}