adrema/tests/Feature/Course/UpdateTest.php

135 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2021-11-20 00:48:42 +01:00
<?php
namespace Tests\Feature\Course;
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
use App\Member\Member;
2022-02-12 01:08:56 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2021-11-20 00:48:42 +01:00
use Tests\TestCase;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
class UpdateTest extends TestCase
{
2022-02-12 01:08:56 +01:00
use DatabaseTransactions;
2021-11-20 00:48:42 +01:00
/**
* @return array<string, array{payload: array<string, mixed>, errors: array<string, mixed>}>
*/
2024-09-21 22:46:38 +02:00
public static function validationDataProvider(): array
2021-11-20 00:48:42 +01:00
{
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
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
2021-11-20 00:48:42 +01:00
public function testItValidatesInput(array $payload, array $errors): void
{
2022-02-19 18:06:07 +01:00
$this->login()->loginNami();
2021-11-20 00:48:42 +01:00
$member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->for(Course::factory()), 'courses')->createOne();
$newCourse = Course::factory()->inNami(789)->create();
2023-10-18 01:11:36 +02:00
$response = $this->patch("/course/{$member->courses->first()->id}", array_merge([
2021-11-20 00:48:42 +01:00
'course_id' => $newCourse->id,
'completed_at' => '1999-02-03',
'event_name' => '::newevent::',
'organizer' => '::org::',
], $payload));
2022-02-19 18:06:07 +01:00
$this->assertErrors($errors, $response);
2021-11-20 00:48:42 +01:00
}
public function testItUpdatesACourse(): void
{
$this->withoutExceptionHandling();
2022-02-19 18:06:07 +01:00
$this->login()->loginNami();
2022-02-23 00:01:03 +01:00
app(CourseFake::class)->updatesSuccessfully(123, 999);
2021-11-20 00:48:42 +01:00
$member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->inNami(999)->for(Course::factory()), 'courses')->createOne();
$newCourse = Course::factory()->inNami(789)->create();
2023-10-18 01:11:36 +02:00
$this->patch("/course/{$member->courses->first()->id}", array_merge([
2021-11-20 00:48:42 +01:00
'course_id' => $newCourse->id,
'completed_at' => '1999-02-03',
'event_name' => '::newevent::',
'organizer' => '::neworg::',
]));
$this->assertDatabaseHas('course_members', [
'member_id' => $member->id,
'course_id' => $newCourse->id,
'event_name' => '::newevent::',
'organizer' => '::neworg::',
'completed_at' => '1999-02-03',
'nami_id' => 999,
]);
app(CourseFake::class)->assertUpdated(123, 999, [
'bausteinId' => 789,
'veranstalter' => '::neworg::',
'vstgName' => '::newevent::',
'vstgTag' => '1999-02-03T00:00:00',
]);
}
2022-02-19 18:06:07 +01:00
public function testItThrowsErrorWhenLoginIsWrong(): void
{
2023-02-17 18:57:11 +01:00
$this->login()->withNamiSettings();
2022-02-19 18:06:07 +01:00
$member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->inNami(999)->for(Course::factory()), 'courses')->createOne();
$newCourse = Course::factory()->inNami(789)->create();
2023-10-18 01:11:36 +02:00
$response = $this->patch("/course/{$member->courses->first()->id}", array_merge([
2022-02-19 18:06:07 +01:00
'course_id' => $newCourse->id,
'completed_at' => '1999-02-03',
'event_name' => '::newevent::',
'organizer' => '::neworg::',
]));
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
}
2021-11-20 00:48:42 +01:00
public function testItReceivesUnknownErrors(): void
{
2022-02-19 18:06:07 +01:00
$this->login()->loginNami();
2022-02-23 00:01:03 +01:00
app(CourseFake::class)->failsUpdating(123, 999);
2021-11-20 00:54:10 +01:00
$member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->inNami(999)->for(Course::factory()), 'courses')->createOne();
$newCourse = Course::factory()->inNami(789)->create();
2021-11-20 00:48:42 +01:00
2023-10-18 01:11:36 +02:00
$response = $this->patch("/course/{$member->courses->first()->id}", [
2021-11-20 00:54:10 +01:00
'course_id' => $newCourse->id,
2021-11-20 00:48:42 +01:00
'completed_at' => '2021-01-02',
'event_name' => '::event::',
'organizer' => '::org::',
]);
2021-11-23 01:02:18 +01:00
2021-11-20 00:48:42 +01:00
$response->assertSessionHasErrors(['id' => 'Unbekannter Fehler']);
}
}