hasTable') ->once() ->with('admin_notification_subjects') ->andReturn(true); $userService = Mockery::mock(AdminNotificationUserService::class); $userService->shouldReceive('fetchAdminNotificationUsers') ->once() ->andReturn([ ['id' => 1, 'firstname' => 'A', 'lastname' => 'One', 'email' => 'a1@test.com'], ['id' => 2, 'firstname' => 'A', 'lastname' => 'Two', 'email' => 'a2@test.com'], ]); $row1 = (object) ['admin_id' => 1]; $row2 = (object) ['admin_id' => 2]; AdminNotificationSubject::shouldReceive('query->select->where->whereIn->get') ->once() ->andReturn(collect([$row1, $row2])); $service = new AdminPrintRecipientService($userService); $result = $service->data(); $this->assertTrue($result['tableReady']); $this->assertTrue($result['assigned'][1]); $this->assertTrue($result['assigned'][2]); } public function test_save_returns_error_when_table_missing(): void { DB::shouldReceive('getSchemaBuilder->hasTable') ->once() ->with('admin_notification_subjects') ->andReturn(false); $userService = Mockery::mock(AdminNotificationUserService::class); $service = new AdminPrintRecipientService($userService); $result = $service->save([ 1 => 1, ]); $this->assertFalse($result['success']); $this->assertSame(422, $result['status']); } public function test_save_updates_print_recipients(): void { DB::shouldReceive('getSchemaBuilder->hasTable') ->once() ->with('admin_notification_subjects') ->andReturn(true); $userService = Mockery::mock(AdminNotificationUserService::class); $userService->shouldReceive('fetchAdminNotificationUsers') ->once() ->andReturn([ ['id' => 1], ['id' => 2], ['id' => 3], ]); AdminNotificationSubject::shouldReceive('query->where->whereIn->pluck') ->once() ->andReturn(collect([1, 2])); AdminNotificationSubject::shouldReceive('query->where->whereIn->delete') ->once() ->andReturn(1); AdminNotificationSubject::shouldReceive('insert') ->once() ->with(Mockery::on(function ($batch) { return count($batch) === 1 && $batch[0]['admin_id'] === 3 && $batch[0]['subject'] === 'print_requests'; })) ->andReturn(true); $service = new AdminPrintRecipientService($userService); $result = $service->save([ 1 => 1, 3 => 1, ]); $this->assertTrue($result['success']); $this->assertSame(200, $result['status']); } }