2021-11-19 22:58:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Course;
|
|
|
|
|
|
|
|
use App\Course\Models\Course;
|
|
|
|
use App\Member\Member;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Zoomyboy\LaravelNami\Backend\FakeBackend;
|
|
|
|
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
|
|
|
|
|
|
|
class StoreTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array{payload: array<string, mixed>, errors: array<string, mixed>}>
|
|
|
|
*/
|
|
|
|
public function validationDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'course_id_missing' => [
|
|
|
|
'payload' => ['course_id' => null],
|
|
|
|
'errors' => ['course_id' => 'Baustein ist erforderlich.'],
|
|
|
|
],
|
|
|
|
'course_id_invalid' => [
|
|
|
|
'payload' => ['course_id' => 999],
|
|
|
|
'errors' => ['course_id' => 'Baustein ist nicht vorhanden.'],
|
|
|
|
],
|
|
|
|
'completed_at_required' => [
|
|
|
|
'payload' => ['completed_at' => ''],
|
|
|
|
'errors' => ['completed_at' => 'Datum ist erforderlich.'],
|
|
|
|
],
|
|
|
|
'completed_at_not_date' => [
|
|
|
|
'payload' => ['completed_at' => '123'],
|
|
|
|
'errors' => ['completed_at' => 'Datum muss ein gültiges Datum sein.'],
|
|
|
|
],
|
|
|
|
'event_name_required' => [
|
|
|
|
'payload' => ['event_name' => ''],
|
|
|
|
'errors' => ['event_name' => 'Veranstaltung ist erforderlich.'],
|
|
|
|
],
|
|
|
|
'organizer' => [
|
|
|
|
'payload' => ['organizer' => ''],
|
|
|
|
'errors' => ['organizer' => 'Veranstalter ist erforderlich.'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, string> $payload
|
|
|
|
* @param array<string, string> $errors
|
|
|
|
* @dataProvider validationDataProvider
|
|
|
|
*/
|
|
|
|
public function testItValidatesInput(array $payload, array $errors): void
|
|
|
|
{
|
|
|
|
$this->login()->init();
|
|
|
|
$member = Member::factory()->defaults()->inNami(123)->createOne();
|
|
|
|
$course = Course::factory()->inNami(456)->createOne();
|
|
|
|
|
|
|
|
$response = $this->post("/member/{$member->id}/course", array_merge([
|
|
|
|
'course_id' => $course->id,
|
|
|
|
'completed_at' => '2021-01-02',
|
|
|
|
'event_name' => '::event::',
|
|
|
|
'organizer' => '::org::',
|
|
|
|
], $payload));
|
|
|
|
|
|
|
|
$response->assertSessionHasErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCreatesACourse(): void
|
|
|
|
{
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
$this->login()->init();
|
|
|
|
$member = Member::factory()->defaults()->inNami(123)->createOne();
|
|
|
|
$course = Course::factory()->inNami(456)->createOne();
|
|
|
|
app(CourseFake::class)->createsSuccessful(123, 999);
|
|
|
|
|
2021-11-23 01:02:18 +01:00
|
|
|
$this->post("/member/{$member->id}/course", [
|
2021-11-19 22:58:27 +01:00
|
|
|
'course_id' => $course->id,
|
|
|
|
'completed_at' => '2021-01-02',
|
|
|
|
'event_name' => '::event::',
|
|
|
|
'organizer' => '::org::',
|
|
|
|
]);
|
|
|
|
|
2021-11-20 00:48:42 +01:00
|
|
|
$this->assertDatabaseHas('course_members', [
|
2021-11-19 22:58:27 +01:00
|
|
|
'member_id' => $member->id,
|
|
|
|
'course_id' => $course->id,
|
|
|
|
'completed_at' => '2021-01-02',
|
|
|
|
'event_name' => '::event::',
|
|
|
|
'organizer' => '::org::',
|
|
|
|
'nami_id' => 999,
|
|
|
|
]);
|
|
|
|
app(CourseFake::class)->assertCreated(123, [
|
|
|
|
'bausteinId' => 456,
|
|
|
|
'veranstalter' => '::org::',
|
2021-11-19 23:03:12 +01:00
|
|
|
'vstgName' => '::event::',
|
2021-11-19 22:58:27 +01:00
|
|
|
'vstgTag' => '2021-01-02T00:00:00',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-11-19 23:14:18 +01:00
|
|
|
public function testItReceivesUnknownErrors(): void
|
|
|
|
{
|
|
|
|
$this->login()->init();
|
|
|
|
$member = Member::factory()->defaults()->inNami(123)->createOne();
|
|
|
|
$course = Course::factory()->inNami(456)->createOne();
|
|
|
|
app(CourseFake::class)->doesntCreateWithError(123);
|
|
|
|
|
|
|
|
$response = $this->post("/member/{$member->id}/course", [
|
|
|
|
'course_id' => $course->id,
|
|
|
|
'completed_at' => '2021-01-02',
|
|
|
|
'event_name' => '::event::',
|
|
|
|
'organizer' => '::org::',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertSessionHasErrors(['id' => 'Unbekannter Fehler']);
|
2021-11-20 00:48:42 +01:00
|
|
|
$this->assertDatabaseCount('course_members', 0);
|
2021-11-19 23:14:18 +01:00
|
|
|
}
|
|
|
|
|
2021-11-19 22:58:27 +01:00
|
|
|
}
|