Skip to content

Commit

Permalink
fix: l11, misc bugfixes (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: atymic <[email protected]>
  • Loading branch information
atymic authored Mar 20, 2024
1 parent 08ad48e commit 1fa2457
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 68 deletions.
16 changes: 6 additions & 10 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/composer.lock
/tests/temp
.phpunit.result.cache
.phpunit.cache
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="package">
<directory suffix="Test.php">tests</directory>
Expand All @@ -18,4 +13,9 @@
<env name="DB_USERNAME" value="testing"/>
<env name="DB_PASSWORD" value="testing"/>
</php>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
7 changes: 7 additions & 0 deletions src/Exception/UnserializeFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Thomasjohnkane\Snooze\Exception;

class UnserializeFailedException extends LaravelSnoozeException
{
}
35 changes: 12 additions & 23 deletions src/Models/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Thomasjohnkane\Snooze\Events\NotificationSent;
use Thomasjohnkane\Snooze\Exception\NotificationAlreadySentException;
use Thomasjohnkane\Snooze\Exception\NotificationCancelledException;
use Thomasjohnkane\Snooze\Exception\UnserializeFailedException;
use Thomasjohnkane\Snooze\Serializer;

class ScheduledNotification extends Model
Expand All @@ -17,27 +18,7 @@ class ScheduledNotification extends Model
/** @var Serializer */
protected $serializer;

protected $dates = [
'send_at',
'sent_at',
'rescheduled_at',
'cancelled_at',
];

protected $fillable = [
'target_id',
'target_type',
'target',
'notification_type',
'notification',
'send_at',
'sent_at',
'rescheduled',
'cancelled',
'created_at',
'updated_at',
'meta',
];
protected $guarded = [];

protected $attributes = [
'sent_at' => null,
Expand All @@ -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 = [])
Expand All @@ -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();
Expand Down
38 changes: 14 additions & 24 deletions src/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\AnonymousNotifiable;
Expand Down Expand Up @@ -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')) {
Expand All @@ -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,
]));
}

Expand Down Expand Up @@ -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,
];
})
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/SendCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
);
}
}

0 comments on commit 1fa2457

Please sign in to comment.