0

I want to test MarkAsRead function via routing, my route was Route::post('notifications/{id}', [NotificationController::class, 'markAsRead']); and it was in API routing of auth:sanctum

api route

Route::middleware('auth:sanctum')->group(function(){
    Route::get('notifications', [NotificationController::class, 'index']);
    Route::post('notifications/{id}', [NotificationController::class, 'markAsRead']);
});

MarkAsRead Function Test

public function test_notification_read(): void
    {
        $user = User::factory()->create([
            'email' => '[email protected]',
            'password' => bcrypt('password'),
        ]); 
        Sanctum::actingAs($user);
        $k=Categories::factory(6)->create();
        $book = Book::factory()->create();
        $user->notify(new NewBookNotification($book));
        $notification = $this->getJson('/api/notifications');
        dd($notification);
        $response = $this->postJson("/api/notifications/{$notification->data->id}");//this part was error
        $response->assertStatus(200)->assertJsonStructure([
            'success',
            'message',
            'data'  =>[
                    'id',
                    'data',
                    'read_at',
                    'created_at'
                    ]
        ]);
    }

API NotificationController

public function index(): JsonResponse
    {
        $user = \Auth::user();
        $notifications = $user->unreadNotifications;
        return $this->sendResponse(NotificationResource::collection($notifications), 'Notifikasi Telah diambil.');
    }
    public function markAsRead($id): JsonResponse
    {
        $user = \Auth::user();
        $notification = $user->notifications->where('id', $id)->first();
        if ($notification) {
            $notification->markAsRead();
            return $this->sendResponse(new NotificationResource($notification), 'Notifikasi Telah dibaca.');
        }
    }

Notification Resource

public function toArray(Request $request): array
    {
        $array = [
            'id' => $this->id,
            'data' => $this->data,
            'read_at' => $this->read_at,
            'created_at' => $this->created_at,
        ];
        return $array;
    }

because id of notification was uuid, I can't brute force it. Does anyone know alternartive to test notification markasread or how to make notification id can be read?

I already tried adding [0] next to $notification or $notification->data with no succeed. dd($notification) already show me notification is exist but I don't know how to extract the id into postJson route. So I either need to find a way to extract id from notification data table or find other way to test markasread. any recommendation?

1 Answer 1

0

Found the solutions it was just simple as using user to pull the notifications and specify the array.

$notification = $user->notifications;
$response = $this->postJson("/api/notifications/{$notification[0]->id}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.