adrema/tests/Feature/Course/StoreTest.php

137 lines
4.6 KiB
PHP
Raw Normal View History

2021-11-19 22:58:27 +01:00
<?php
namespace Tests\Feature\Course;
use App\Course\Models\Course;
use App\Member\Member;
2022-02-12 01:08:56 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2021-11-19 22:58:27 +01:00
use Tests\TestCase;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
class StoreTest extends TestCase
{
2022-02-12 01:08:56 +01:00
use DatabaseTransactions;
2021-11-19 22:58:27 +01:00
/**
* @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
{
2022-02-19 15:18:24 +01:00
$this->login();
$member = Member::factory()->defaults()->createOne();
$course = Course::factory()->createOne();
2021-11-19 22:58:27 +01:00
$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
{
2022-02-19 15:18:24 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2021-11-19 22:58:27 +01:00
$member = Member::factory()->defaults()->inNami(123)->createOne();
$course = Course::factory()->inNami(456)->createOne();
2022-02-23 00:01:03 +01:00
app(CourseFake::class)->createsSuccessfully(123, 999);
2021-11-19 22:58:27 +01:00
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',
]);
}
2022-02-19 15:18:24 +01:00
public function testItThrowsErrorWhenLoginIsWrong(): void
{
$this->login()->failedNami();
$member = Member::factory()->defaults()->inNami(123)->createOne();
$course = Course::factory()->inNami(456)->createOne();
$response = $this->post("/member/{$member->id}/course", [
'course_id' => $course->id,
'completed_at' => '2021-01-02',
'event_name' => '::event::',
'organizer' => '::org::',
]);
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
$this->assertDatabaseMissing('course_members', [
'member_id' => $member->id,
]);
}
2021-11-19 23:14:18 +01:00
public function testItReceivesUnknownErrors(): void
{
2022-02-19 15:18:24 +01:00
$this->login()->loginNami();
2021-11-19 23:14:18 +01:00
$member = Member::factory()->defaults()->inNami(123)->createOne();
$course = Course::factory()->inNami(456)->createOne();
2022-02-23 00:01:03 +01:00
app(CourseFake::class)->failsCreating(123);
2021-11-19 23:14:18 +01:00
$response = $this->post("/member/{$member->id}/course", [
'course_id' => $course->id,
'completed_at' => '2021-01-02',
'event_name' => '::event::',
'organizer' => '::org::',
]);
2022-03-11 20:19:17 +01:00
2021-11-19 23:14:18 +01:00
$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
}