From 43f68b5765d32ba4ac7ce3df04ea207214559cc1 Mon Sep 17 00:00:00 2001
From: philipp lang <philipp@aweos.de>
Date: Sat, 19 Feb 2022 22:50:06 +0100
Subject: [PATCH] Add: Output errors to console when nami error occurs

---
 .../Commands/NamiInitializeCommand.php        |  8 +++++++-
 packages/laravel-nami                         |  2 +-
 tests/Feature/Course/StoreTest.php            |  2 +-
 tests/Feature/Initialize/InitializeTest.php   | 20 ++++++++++++++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/app/Console/Commands/NamiInitializeCommand.php b/app/Console/Commands/NamiInitializeCommand.php
index 55c80e2f..7079add7 100644
--- a/app/Console/Commands/NamiInitializeCommand.php
+++ b/app/Console/Commands/NamiInitializeCommand.php
@@ -5,6 +5,7 @@ namespace App\Console\Commands;
 use App\Initialize\Initializer;
 use Illuminate\Console\Command;
 use Zoomyboy\LaravelNami\Nami;
+use Zoomyboy\LaravelNami\NamiException;
 
 class NamiInitializeCommand extends Command
 {
@@ -39,7 +40,12 @@ class NamiInitializeCommand extends Command
      */
     public function handle()
     {
-        app(Initializer::class)->run();
+        try {
+            app(Initializer::class)->run();
+        } catch (NamiException $e) {
+            $e->outputToConsole($this);
+            return 1;
+        }
 
         return 0;
     }
diff --git a/packages/laravel-nami b/packages/laravel-nami
index 8515b1fc..423fb27f 160000
--- a/packages/laravel-nami
+++ b/packages/laravel-nami
@@ -1 +1 @@
-Subproject commit 8515b1fc05999c4b91579934b7da287e0d60fb06
+Subproject commit 423fb27f51e322b42fa3b2a01b1bafd886f514fe
diff --git a/tests/Feature/Course/StoreTest.php b/tests/Feature/Course/StoreTest.php
index 7257cc52..51ae3352 100644
--- a/tests/Feature/Course/StoreTest.php
+++ b/tests/Feature/Course/StoreTest.php
@@ -124,7 +124,7 @@ class StoreTest extends TestCase
         $this->login()->loginNami();
         $member = Member::factory()->defaults()->inNami(123)->createOne();
         $course = Course::factory()->inNami(456)->createOne();
-        app(CourseFake::class)->doesntCreateWithError(123);
+        app(CourseFake::class)->createFailed(123);
 
         $response = $this->post("/member/{$member->id}/course", [
             'course_id' => $course->id,
diff --git a/tests/Feature/Initialize/InitializeTest.php b/tests/Feature/Initialize/InitializeTest.php
index 54977933..7a25f6a9 100644
--- a/tests/Feature/Initialize/InitializeTest.php
+++ b/tests/Feature/Initialize/InitializeTest.php
@@ -3,6 +3,7 @@
 namespace Tests\Feature\Initialize;
 
 use App\Activity;
+use App\Console\Commands\NamiInitializeCommand;
 use App\Country;
 use App\Course\Models\Course;
 use App\Gender;
@@ -18,6 +19,8 @@ use Illuminate\Support\Facades\Http;
 use Tests\TestCase;
 use Zoomyboy\LaravelNami\Backend\FakeBackend;
 use Zoomyboy\LaravelNami\Fakes\GroupFake;
+use Zoomyboy\LaravelNami\Fakes\MemberFake;
+use Zoomyboy\LaravelNami\Fakes\SearchFake;
 
 class InitializeTest extends TestCase
 {
@@ -128,7 +131,7 @@ class InitializeTest extends TestCase
         $this->initializeProvider();
         GeneralSettings::fake(['allowed_nami_accounts' => [123]]);
 
-        Artisan::call('nami:initialize');
+        Artisan::call(NamiInitializeCommand::class);
 
         $this->assertDatabaseHas('regions', [
             'name' => 'nrw',
@@ -413,6 +416,21 @@ class InitializeTest extends TestCase
         $this->assertDatabaseCount('members', $num);
     }
 
+    public function testRenderErrorInConsoleWhenUsingArtisan(): void
+    {
+        $this->withoutExceptionHandling()->login()->loginNami();
+        $this->initializeProvider(function($backend) {
+            app(SearchFake::class)->fetchFails($page = 1, $start = 0, 'search error');
+        });
+        $this->login();
+
+        $command = $this->artisan(NamiInitializeCommand::class);
+
+        $command->assertFailed();
+        $command->expectsOutput('response: {"success":false,"message":"search error"}');
+        $command->expectsOutput('Search failed');
+    }
+
     /**
      * @param array<string, mixed> $overwrites
      * @return array<string, mixed>