diff --git a/lib/Controller/BookingController.php b/lib/Controller/BookingController.php index 6892e13f65..3e5fe441ce 100644 --- a/lib/Controller/BookingController.php +++ b/lib/Controller/BookingController.php @@ -25,7 +25,6 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Utility\ITimeFactory; -use OCP\DB\Exception; use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; @@ -94,9 +93,9 @@ public function getBookableSlots( ): JsonResponse { try { $tz = new DateTimeZone($timeZone); - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->error('Timezone invalid', ['exception' => $e]); - return JsonResponse::fail('Invalid time zone', Http::STATUS_UNPROCESSABLE_ENTITY); + return JsonResponse::fail('Invalid timezone', Http::STATUS_UNPROCESSABLE_ENTITY); } // Convert selected date to requesters selected timezone adjusted start and end of day in epoch $startTimeInTz = (new DateTime($dateSelected, $tz)) diff --git a/lib/Service/Appointments/BookingService.php b/lib/Service/Appointments/BookingService.php index 3ef93aa3a7..c69ffdfe38 100644 --- a/lib/Service/Appointments/BookingService.php +++ b/lib/Service/Appointments/BookingService.php @@ -20,7 +20,6 @@ use OCA\Calendar\Exception\ServiceException; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; -use OCP\DB\Exception; use OCP\DB\Exception as DbException; use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; @@ -140,8 +139,8 @@ public function book(AppointmentConfig $config, int $start, int $end, string $ti try { $tz = new DateTimeZone($timeZone); - } catch (Exception $e) { - throw new InvalidArgumentException('Could not make sense of the timezone', $e->getCode(), $e); + } catch (\Exception $e) { + throw new InvalidArgumentException('Could not make sense of the timezone', previous: $e); } $booking = new Booking(); @@ -156,7 +155,7 @@ public function book(AppointmentConfig $config, int $start, int $end, string $ti $booking->setTimezone($tz->getName()); try { $this->bookingMapper->insert($booking); - } catch (Exception $e) { + } catch (DbException $e) { throw new ServiceException('Could not create booking', 0, $e); } diff --git a/tests/php/unit/Controller/BookingControllerTest.php b/tests/php/unit/Controller/BookingControllerTest.php index bd21b0228c..57c54e19c2 100644 --- a/tests/php/unit/Controller/BookingControllerTest.php +++ b/tests/php/unit/Controller/BookingControllerTest.php @@ -10,7 +10,6 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use DateTime; -use Exception; use InvalidArgumentException; use OC\URLGenerator; use OCA\Calendar\Db\AppointmentConfig; @@ -166,9 +165,15 @@ public function testGetBookableSlotsInvalidTimezone(): void { ->with($apptConfg->getToken()); $this->bookingService->expects(self::never()) ->method('getAvailableSlots'); - $this->expectException(Exception::class); + $this->logger->expects(self::once()) + ->method('error') + ->with('Timezone invalid'); + + $response = $this->controller->getBookableSlots($apptConfg->getToken(), $selectedDate, 'Hook/Neverland'); - $this->controller->getBookableSlots($apptConfg->getToken(), $selectedDate, 'Hook/Neverland'); + self::assertInstanceOf(JsonResponse::class, $response); + self::assertSame(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + self::assertSame(['status' => 'fail', 'data' => 'Invalid timezone'], $response->getData()); } public function testGetBookableSlotsTimezoneIdentical(): void { diff --git a/tests/php/unit/Service/Appointments/BookingServiceTest.php b/tests/php/unit/Service/Appointments/BookingServiceTest.php index 9147a6cea7..b6280bbb44 100644 --- a/tests/php/unit/Service/Appointments/BookingServiceTest.php +++ b/tests/php/unit/Service/Appointments/BookingServiceTest.php @@ -10,7 +10,7 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use DateTimeImmutable; -use Exception; +use InvalidArgumentException; use OCA\Calendar\Db\AppointmentConfig; use OCA\Calendar\Db\Booking; use OCA\Calendar\Db\BookingMapper; @@ -160,7 +160,7 @@ public function testBookInvalidTimezone(): void { $this->random->expects(self::never()) ->method('generate'); - $this->expectException(Exception::class); + $this->expectExceptionObject(new InvalidArgumentException('Could not make sense of the timezone')); $this->service->book(new AppointmentConfig(), 4054546654, 44545454, 'Nighttime/DAYTIME!', 'Test', 'test@test.com', 'Test'); }