adrema/tests/Feature/Contribution/StoreTest.php

213 lines
7.5 KiB
PHP
Raw Normal View History

2022-11-07 16:18:11 +01:00
<?php
2022-11-17 21:47:45 +01:00
namespace Tests\Feature\Contribution;
2022-11-07 16:18:11 +01:00
use App\Contribution\Documents\ContributionDocument;
2023-09-07 12:04:13 +02:00
use App\Contribution\Documents\RdpNrwDocument;
use App\Contribution\Documents\CitySolingenDocument;
2022-11-07 16:18:11 +01:00
use App\Country;
2023-05-17 00:22:43 +02:00
use App\Gender;
2022-11-07 16:18:11 +01:00
use App\Member\Member;
use Generator;
2022-11-07 16:18:11 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-05-17 00:22:43 +02:00
use Laravel\Passport\Client;
use Laravel\Passport\Passport;
2024-10-14 21:21:19 +02:00
use Modules\Invoice\InvoiceSettings;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2023-05-17 00:22:43 +02:00
use Tests\RequestFactories\ContributionMemberApiRequestFactory;
use Tests\RequestFactories\ContributionRequestFactory;
2022-11-07 16:18:11 +01:00
use Tests\TestCase;
use Zoomyboy\Tex\Tex;
2022-11-17 21:47:45 +01:00
class StoreTest extends TestCase
2022-11-07 16:18:11 +01:00
{
use DatabaseTransactions;
/**
2023-09-07 12:04:13 +02:00
* @testWith ["App\\Contribution\\Documents\\CitySolingenDocument", ["Super tolles Lager", "Max Muster", "Jane Muster", "15.06.1991"]]
* ["App\\Contribution\\Documents\\RdpNrwDocument", ["Muster, Max", "Muster, Jane", "15.06.1991", "42777 SG"]]
* ["App\\Contribution\\Documents\\CityRemscheidDocument", ["Max", "Muster", "Jane"]]
* ["App\\Contribution\\Documents\\CityFrankfurtMainDocument", ["Max", "Muster", "Jane"]]
* ["App\\Contribution\\Documents\\BdkjHesse", ["Max", "Muster", "Jane"]]
2022-11-07 16:18:11 +01:00
*
* @param array<int, string> $bodyChecks
*/
2023-05-17 00:22:43 +02:00
public function testItCompilesContributionDocumentsViaRequest(string $type, array $bodyChecks): void
2022-11-07 16:18:11 +01:00
{
$this->withoutExceptionHandling();
Tex::spy();
$this->login()->loginNami();
2024-06-12 00:04:13 +02:00
$member1 = Member::factory()->defaults()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']);
$member2 = Member::factory()->defaults()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']);
2022-11-07 16:18:11 +01:00
$response = $this->call('GET', '/contribution-generate', [
'payload' => ContributionRequestFactory::new()->type($type)->state([
2023-03-09 02:14:24 +01:00
'dateFrom' => '1991-06-15',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
'members' => [$member1->id, $member2->id],
'type' => $type,
'zipLocation' => '42777 SG',
])->toBase64(),
2022-11-07 16:18:11 +01:00
]);
$response->assertSessionDoesntHaveErrors();
$response->assertOk();
Tex::assertCompiled($type, fn ($document) => $document->hasAllContent($bodyChecks));
}
2023-03-09 02:14:24 +01:00
public function testItCompilesGroupNameInSolingenDocument(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
Tex::spy();
InvoiceSettings::fake(['from_long' => 'Stamm BiPi']);
$this->call('GET', '/contribution-generate', [
'payload' => ContributionRequestFactory::new()->type(CitySolingenDocument::class)->toBase64(),
]);
Tex::assertCompiled(CitySolingenDocument::class, fn ($document) => $document->hasAllContent(['Stamm BiPi']));
}
2023-05-17 00:22:43 +02:00
public function testItCompilesContributionDocumentsViaApi(): void
{
$this->withoutExceptionHandling();
Tex::spy();
2024-06-12 00:04:13 +02:00
Gender::factory()->female()->create();
Gender::factory()->male()->create();
2023-05-17 00:22:43 +02:00
Passport::actingAsClient(Client::factory()->create(), ['contribution-generate']);
$country = Country::factory()->create();
2023-07-27 15:14:11 +02:00
Member::factory()->defaults()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']);
Member::factory()->defaults()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']);
2023-05-17 00:22:43 +02:00
$response = $this->postJson('/api/contribution-generate', [
'country' => $country->id,
'dateFrom' => '1991-06-15',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
2023-09-07 12:04:13 +02:00
'type' => CitySolingenDocument::class,
2023-05-17 00:22:43 +02:00
'zipLocation' => '42777 SG',
'member_data' => [
ContributionMemberApiRequestFactory::new()->create(),
ContributionMemberApiRequestFactory::new()->create(),
],
]);
$response->assertSessionDoesntHaveErrors();
$response->assertOk();
2023-09-07 12:04:13 +02:00
Tex::assertCompiled(CitySolingenDocument::class, fn ($document) => $document->hasAllContent(['Super']));
2023-05-17 00:22:43 +02:00
}
/**
* @testWith [""]
* ["aaaa"]
* ["YWFhCg=="]
*/
public function testInputShouldBeBase64EncodedJson(string $payload): void
2023-03-09 02:14:24 +01:00
{
$this->login()->loginNami();
$this->call('GET', '/contribution-generate', ['payload' => $payload])->assertSessionHasErrors('payload');
}
/**
* @param array<string, string> $input
* @param class-string<ContributionDocument> $documentClass
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
public function testItValidatesInput(array $input, string $documentClass, string $errorField): void
{
$this->login()->loginNami();
Country::factory()->create();
Member::factory()->defaults()->create();
2023-03-09 02:14:24 +01:00
$this->postJson('/contribution-validate', ContributionRequestFactory::new()->type($documentClass)->state($input)->create())
->assertJsonValidationErrors($errorField);
}
/**
* @param array<string, string> $input
* @param class-string<ContributionDocument> $documentClass
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
public function testItValidatesInputBeforeGeneration(array $input, string $documentClass, string $errorField): void
{
$this->login()->loginNami();
Country::factory()->create();
Member::factory()->defaults()->create();
$this->call('GET', '/contribution-generate', [
'payload' => ContributionRequestFactory::new()->type($documentClass)->state($input)->toBase64(),
])->assertSessionHasErrors($errorField);
}
2024-09-22 03:02:10 +02:00
public static function validationDataProvider(): Generator
{
yield [
['type' => 'aaa'],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'type',
];
yield [
['type' => ''],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'type',
];
yield [
['dateFrom' => ''],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'dateFrom',
];
yield [
['dateFrom' => '2022-01'],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'dateFrom',
];
yield [
['dateUntil' => ''],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'dateUntil',
];
yield [
['dateUntil' => '2022-01'],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'dateUntil',
];
yield [
['country' => -1],
2023-09-07 12:04:13 +02:00
RdpNrwDocument::class,
'country',
];
yield [
['country' => 'AAAA'],
2023-09-07 12:04:13 +02:00
RdpNrwDocument::class,
'country',
];
yield [
['members' => 'A'],
2023-09-07 12:04:13 +02:00
RdpNrwDocument::class,
'members',
];
yield [
['members' => [99999]],
2023-09-07 12:04:13 +02:00
RdpNrwDocument::class,
'members.0',
];
yield [
['members' => ['lalala']],
2023-09-07 12:04:13 +02:00
RdpNrwDocument::class,
'members.0',
];
yield [
['eventName' => ''],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'eventName',
];
yield [
['zipLocation' => ''],
2023-09-07 12:04:13 +02:00
CitySolingenDocument::class,
'zipLocation',
];
2023-03-09 02:14:24 +01:00
}
2022-11-07 16:18:11 +01:00
}