Add failure for course creation

This commit is contained in:
philipp lang 2021-11-19 23:14:18 +01:00
parent f053e7a49c
commit 3186b00a0d
3 changed files with 28 additions and 3 deletions

View File

@ -6,6 +6,8 @@ use App\Course\Models\Course;
use App\Member\Member; use App\Member\Member;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
use Zoomyboy\LaravelNami\NamiException;
class StoreRequest extends FormRequest class StoreRequest extends FormRequest
{ {
@ -36,12 +38,17 @@ class StoreRequest extends FormRequest
public function persist(Member $member): void public function persist(Member $member): void
{ {
$course = Course::findOrFail($this->input('course_id')); $course = Course::where('id', $this->input('course_id'))->firstOrFail();
$payload = array_merge( $payload = array_merge(
$this->only(['event_name', 'completed_at', 'course_id', 'organizer']), $this->only(['event_name', 'completed_at', 'course_id', 'organizer']),
['course_id' => $course->nami_id], ['course_id' => $course->nami_id],
); );
$namiId = auth()->user()->api()->createCourse($member->nami_id, $payload);
try {
$namiId = auth()->user()->api()->createCourse($member->nami_id, $payload);
} catch(NamiException $e) {
throw ValidationException::withMessages(['id' => 'Unbekannter Fehler']);
}
$member->courses()->attach( $member->courses()->attach(
$course, $course,

@ -1 +1 @@
Subproject commit 90d91088a983107de634e55b4e24146923c99bed Subproject commit 1238309006a18aa1de37bbaac74c3089c128c582

View File

@ -101,4 +101,22 @@ class StoreTest extends TestCase
]); ]);
} }
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']);
$this->assertDatabaseCount('course_member', 0);
}
} }