adrema/tests/Feature/Contribution/StoreTest.php

200 lines
7.0 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;
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();
$country = Country::factory()->create();
2023-05-17 00:22:43 +02:00
$member1 = Member::factory()->defaults()->for(Gender::factory())->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']);
$member2 = Member::factory()->defaults()->for(Gender::factory())->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']);
2022-11-07 16:18:11 +01:00
$response = $this->call('GET', '/contribution-generate', [
2023-03-09 02:14:24 +01:00
'payload' => base64_encode(json_encode([
'country' => $country->id,
'dateFrom' => '1991-06-15',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
'members' => [$member1->id, $member2->id],
'type' => $type,
'zipLocation' => '42777 SG',
])),
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
2023-05-17 00:22:43 +02:00
public function testItCompilesContributionDocumentsViaApi(): void
{
$this->withoutExceptionHandling();
Tex::spy();
Gender::factory()->create(['name' => 'Weiblich']);
Gender::factory()->create(['name' => 'Männlich']);
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
* @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
* @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);
}
protected 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
}