diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index e6490e2..cc0aca6 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,16 +11,12 @@ jobs: strategy: max-parallel: 15 matrix: - laravel-version: ['11.0', ^8.0, ^9.0, ^10.0] - php-versions: ['8.0', '8.1', '8.2'] + laravel-version: [^10.0, ^11.0] + php-versions: ['8.1', '8.2', '8.3'] exclude: - - laravel-version: ^8.0 - php-version: 8.2 - - laravel-version: ^10.0 - php-version: 8.0 - - laravel-version: '11.0' - php-versions: '8.0' - - laravel-version: '11.0' + - laravel-version: '^10.0' + php-versions: '8.1' + - laravel-version: '^11.0' php-versions: '8.1' name: PHP ${{ matrix.php-versions }} on ${{ matrix.laravel-version }} @@ -38,7 +34,7 @@ jobs: - name: Install dependencies run: | - composer require --no-update --no-interaction "illuminate/support:${{ matrix.laravel-version }}" satooshi/php-coveralls + composer require --no-update --no-interaction "illuminate/support:${{ matrix.laravel-version }}" php-coveralls/php-coveralls composer update --no-interaction --prefer-dist --no-suggest - name: Lint composer.json diff --git a/.gitignore b/.gitignore index e98468b..aecf24a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /composer.lock /tests/temp .phpunit.result.cache +.phpunit.cache diff --git a/composer.json b/composer.json index c370f9b..ff7291c 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,12 @@ } ], "require": { - "php": "^8.0 | ^8.1", - "illuminate/support": "^8.0 | ^9.0 | ^10.0 | ^11.0" + "php": "^8.1", + "illuminate/support": "^10.0 | ^11.0" }, "require-dev": { - "orchestra/testbench": "^7.0 |^8.0 | ^9.0", - "phpunit/phpunit": "^9.0 | ^10.0", + "orchestra/testbench": "^8.0 | ^9.0", + "phpunit/phpunit": "^10.0 | ^11.0", "timacdonald/log-fake": "^2" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index 3f293de..0af331c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,10 +1,5 @@ - - - - src - - + tests @@ -18,4 +13,9 @@ + + + src + + diff --git a/src/Exception/UnserializeFailedException.php b/src/Exception/UnserializeFailedException.php new file mode 100644 index 0000000..2653273 --- /dev/null +++ b/src/Exception/UnserializeFailedException.php @@ -0,0 +1,7 @@ + null, @@ -47,6 +28,10 @@ class ScheduledNotification extends Model protected $casts = [ 'meta' => 'array', + 'send_at' => 'immutable_datetime', + 'sent_at' => 'immutable_datetime', + 'rescheduled_at' => 'immutable_datetime', + 'cancelled_at' => 'immutable_datetime', ]; public function __construct(array $attributes = []) @@ -67,8 +52,12 @@ public function send(): void throw new NotificationAlreadySentException('Cannot Send. Notification already sent.', 1); } - $notifiable = $this->serializer->unserialize($this->target); - $notification = $this->serializer->unserialize($this->notification); + try { + $notifiable = $this->serializer->unserialize($this->target); + $notification = $this->serializer->unserialize($this->notification); + } catch (\Exception $exception) { + throw new UnserializeFailedException(sprintf('Cannot Send. Unserialize Failed. (%s)', $exception->getMessage()), 2, $exception); + } if ($this->shouldInterrupt($notification, $notifiable)) { $this->cancel(); diff --git a/src/ScheduledNotification.php b/src/ScheduledNotification.php index 7a89392..5c7fcca 100644 --- a/src/ScheduledNotification.php +++ b/src/ScheduledNotification.php @@ -4,7 +4,6 @@ use Carbon\Carbon; use Carbon\CarbonImmutable; -use Carbon\CarbonInterface; use DateTimeInterface; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\AnonymousNotifiable; @@ -43,7 +42,7 @@ public static function create( ): self { if ($sendAt <= Carbon::now()->subMinute()) { throw new SchedulingFailedException(sprintf('`send_at` must not be in the past: %s', - $sendAt->format(DATE_ISO8601))); + $sendAt->format(DATE_ATOM))); } if (! method_exists($notifiable, 'notify')) { @@ -57,13 +56,13 @@ public static function create( $targetType = $notifiable instanceof AnonymousNotifiable ? AnonymousNotifiable::class : get_class($notifiable); return new self($modelClass::create([ - 'target_id' => $targetId, - 'target_type' => $targetType, + 'target_id' => $targetId, + 'target_type' => $targetType, 'notification_type' => get_class($notification), - 'target' => $serializer->serialize($notifiable), - 'notification' => $serializer->serialize($notification), - 'send_at' => $sendAt, - 'meta' => $meta, + 'target' => $serializer->serialize($notifiable), + 'notification' => $serializer->serialize($notification), + 'send_at' => $sendAt, + 'meta' => $meta, ])); } @@ -158,7 +157,7 @@ public static function cancelAnonymousNotificationsByChannel(string $channel, st ->get() ->map(function (ScheduledNotificationModel $model) use ($serializer) { return [ - 'id' => $model->id, + 'id' => $model->id, 'routes' => $serializer->unserialize($model->target)->routes, ]; }) @@ -253,41 +252,32 @@ public function getTargetId() return $this->scheduleNotificationModel->target_id; } - public function getSentAt() + public function getSentAt(): Carbon|CarbonImmutable|null { return $this->scheduleNotificationModel->sent_at; } - public function getCancelledAt() + public function getCancelledAt(): Carbon|CarbonImmutable|null { return $this->scheduleNotificationModel->cancelled_at; } - public function getRescheduledAt() + public function getRescheduledAt(): Carbon|CarbonImmutable|null { return $this->scheduleNotificationModel->rescheduled_at; } - /** - * @return Carbon|CarbonImmutable - */ - public function getSendAt(): CarbonInterface + public function getSendAt(): Carbon|CarbonImmutable { return $this->scheduleNotificationModel->send_at; } - /** - * @return Carbon|CarbonImmutable - */ - public function getCreatedAt(): CarbonInterface + public function getCreatedAt(): Carbon|CarbonImmutable { return $this->scheduleNotificationModel->created_at; } - /** - * @return Carbon|CarbonImmutable - */ - public function getUpdatedAt(): CarbonInterface + public function getUpdatedAt(): Carbon|CarbonImmutable { return $this->scheduleNotificationModel->updated_at; } diff --git a/tests/SendCommandTest.php b/tests/SendCommandTest.php index e52d8a4..0802bee 100644 --- a/tests/SendCommandTest.php +++ b/tests/SendCommandTest.php @@ -75,7 +75,7 @@ public function testItCatchesFailedScheduledNotifications() ->assertExitCode(0); Log::assertLogged(fn (LogEntry $log) => $log->level === 'error' - && $log->message === 'unserialize(): Error at offset 0 of 10 bytes' + && $log->message === 'Cannot Send. Unserialize Failed. (unserialize(): Error at offset 0 of 10 bytes)' ); } }