From 6ced0e46ac38d2543cf501f7553fd48c731cfabf Mon Sep 17 00:00:00 2001 From: Adam <56907039+adamjsturge@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:43:36 -0800 Subject: [PATCH 1/4] Add Serializer Option for No Relationships --- config/snooze.php | 5 ++++ src/Serializer.php | 3 +-- tests/Models/Child.php | 16 +++++++++++++ tests/Models/User.php | 6 +++++ tests/SerializerTest.php | 52 ++++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 35 +++++++++++++++++++-------- 6 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 tests/Models/Child.php create mode 100644 tests/SerializerTest.php diff --git a/config/snooze.php b/config/snooze.php index c3b5b6f..b28b4f6 100644 --- a/config/snooze.php +++ b/config/snooze.php @@ -56,4 +56,9 @@ * Should snooze automatically register the migrations */ 'registerMigrations' => env('SCHEDULED_NOTIFICATIONS_REGISTER_MIGRATIONS', true), + + /* + * Will not save loaded relationships to the database when serializing + */ + 'doNotLoadRelationsOnSerialize' => env('DO_NOT_LOAD_RELATIONS_ON_SERIALIZE', false) ]; diff --git a/src/Serializer.php b/src/Serializer.php index a910544..bd85a79 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -23,8 +23,7 @@ public function __construct(ConnectionInterface $connection) public function serialize(object $notifiable): string { - $result = serialize($this->getSerializedPropertyValue(clone $notifiable)); - + $result = serialize($this->getSerializedPropertyValue(clone $notifiable, withRelations: ! (config('snooze.doNotLoadRelationsOnSerialize', false) === true))); if ($this->connection instanceof PostgresConnection && Str::contains($result, "\0")) { $result = base64_encode($result); } diff --git a/tests/Models/Child.php b/tests/Models/Child.php new file mode 100644 index 0000000..c27985b --- /dev/null +++ b/tests/Models/Child.php @@ -0,0 +1,16 @@ +belongsTo(User::class); + } +} diff --git a/tests/Models/User.php b/tests/Models/User.php index 99646c8..5672e93 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -5,6 +5,7 @@ use Illuminate\Auth\Authenticatable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Notifications\Notifiable; use Thomasjohnkane\Snooze\Traits\SnoozeNotifiable; @@ -13,4 +14,9 @@ class User extends Model implements AuthenticatableContract use Authenticatable, Notifiable, SnoozeNotifiable; protected $fillable = ['name', 'email', 'password']; + + public function children(): HasMany + { + return $this->hasMany(Child::class); + } } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php new file mode 100644 index 0000000..0e30baf --- /dev/null +++ b/tests/SerializerTest.php @@ -0,0 +1,52 @@ + true]); + $connection = Mockery::mock(ConnectionInterface::class); + $serializer = new Serializer($connection); + + $user = User::with('children')->find(1); + + $serialized = $serializer->serialize($user); + $unserialized = $serializer->unserialize($serialized); + + $this->assertEquals($user->id, $unserialized->id); + $this->assertEquals($user->name, $unserialized->name); + $this->assertFalse($unserialized->relationLoaded('children')); + } + + public function testSerializesModelWithRelationsWhenConfigIsFalse() + { + config(['snooze.doNotLoadRelationsOnSerialize' => false]); + $connection = Mockery::mock(ConnectionInterface::class); + $serializer = new Serializer($connection); + + $user = User::with('children')->find(1); + + $serialized = $serializer->serialize($user); + $unserialized = $serializer->unserialize($serialized); + + $this->assertEquals($user->id, $unserialized->id); + $this->assertEquals($user->name, $unserialized->name); + $this->assertTrue($unserialized->relationLoaded('children')); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 969b3a1..23b9281 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,6 +10,7 @@ use Thomasjohnkane\Snooze\Facades\Snooze; use Thomasjohnkane\Snooze\ServiceProvider; use Thomasjohnkane\Snooze\Tests\Models\User; +use Thomasjohnkane\Snooze\Tests\Models\Child; class TestCase extends Orchestra { @@ -35,7 +36,7 @@ protected function getPackageAliases($app) public function getTempDirectory($suffix = '') { - return __DIR__.DIRECTORY_SEPARATOR.'temp'.($suffix == '' ? '' : DIRECTORY_SEPARATOR.$suffix); + return __DIR__ . DIRECTORY_SEPARATOR . 'temp' . ($suffix == '' ? '' : DIRECTORY_SEPARATOR . $suffix); } protected function initializeDirectory($directory) @@ -56,7 +57,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ 'driver' => 'sqlite', - 'database' => $this->getTempDirectory().'/database.sqlite', + 'database' => $this->getTempDirectory() . '/database.sqlite', 'prefix' => '', ]); @@ -68,7 +69,7 @@ protected function getEnvironmentSetUp($app) */ protected function setUpDatabase($app) { - file_put_contents($this->getTempDirectory().'/database.sqlite', null); + file_put_contents($this->getTempDirectory() . '/database.sqlite', null); $this->artisan('migrate')->run(); @@ -81,14 +82,28 @@ protected function setUpDatabase($app) $table->timestamps(); }); + $app['db']->connection()->getSchemaBuilder()->create('children', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->unsignedInteger('user_id'); + $table->foreign('user_id')->references('id')->on('users'); + $table->timestamps(); + }); + foreach (range(1, 5) as $index) { - User::create( - [ - 'name' => "user{$index}", - 'email' => "user{$index}@example.com", - 'password' => "password{$index}", - ] - ); + $user = User::create([ + 'name' => "user{$index}", + 'email' => "user{$index}@example.com", + 'password' => "password{$index}", + ]); + + // Create two children for each user + foreach (range(1, 2) as $childIndex) { + Child::create([ + 'name' => "child{$childIndex}_user{$index}", + 'user_id' => $user->id, + ]); + } } } } From 261085845b8dd42fe6426a43561dde65f69846ee Mon Sep 17 00:00:00 2001 From: Adam <56907039+adamjsturge@users.noreply.github.com> Date: Wed, 12 Feb 2025 13:28:34 -0800 Subject: [PATCH 2/4] Fix Lint Issues --- tests/SerializerTest.php | 2 +- tests/TestCase.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 0e30baf..a094dcb 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -6,8 +6,8 @@ use Illuminate\Database\ConnectionInterface; use Illuminate\Database\PostgresConnection; -use Thomasjohnkane\Snooze\Serializer; use Mockery; +use Thomasjohnkane\Snooze\Serializer; use Thomasjohnkane\Snooze\Tests\Models\User; class SerializerTest extends TestCase diff --git a/tests/TestCase.php b/tests/TestCase.php index 23b9281..7cebe6d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -36,7 +36,7 @@ protected function getPackageAliases($app) public function getTempDirectory($suffix = '') { - return __DIR__ . DIRECTORY_SEPARATOR . 'temp' . ($suffix == '' ? '' : DIRECTORY_SEPARATOR . $suffix); + return __DIR__.DIRECTORY_SEPARATOR.'temp'.($suffix == '' ? '' : DIRECTORY_SEPARATOR.$suffix); } protected function initializeDirectory($directory) @@ -57,7 +57,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ 'driver' => 'sqlite', - 'database' => $this->getTempDirectory() . '/database.sqlite', + 'database' => $this->getTempDirectory().'/database.sqlite', 'prefix' => '', ]); @@ -69,7 +69,7 @@ protected function getEnvironmentSetUp($app) */ protected function setUpDatabase($app) { - file_put_contents($this->getTempDirectory() . '/database.sqlite', null); + file_put_contents($this->getTempDirectory().'/database.sqlite', null); $this->artisan('migrate')->run(); From cddd90c7689899b4000c025a03c0ed6bdfe751d1 Mon Sep 17 00:00:00 2001 From: Adam <56907039+adamjsturge@users.noreply.github.com> Date: Wed, 12 Feb 2025 13:31:45 -0800 Subject: [PATCH 3/4] Config Error Fix --- config/snooze.php | 2 +- tests/TestCase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/snooze.php b/config/snooze.php index ea8792b..f6b31a4 100644 --- a/config/snooze.php +++ b/config/snooze.php @@ -60,7 +60,7 @@ /* * Will not save loaded relationships to the database when serializing */ - 'doNotLoadRelationsOnSerialize' => env('DO_NOT_LOAD_RELATIONS_ON_SERIALIZE', false) + 'doNotLoadRelationsOnSerialize' => env('DO_NOT_LOAD_RELATIONS_ON_SERIALIZE', false), /* * Set true if you want delete notification when diff --git a/tests/TestCase.php b/tests/TestCase.php index 7cebe6d..010c610 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,8 +9,8 @@ use Orchestra\Testbench\TestCase as Orchestra; use Thomasjohnkane\Snooze\Facades\Snooze; use Thomasjohnkane\Snooze\ServiceProvider; -use Thomasjohnkane\Snooze\Tests\Models\User; use Thomasjohnkane\Snooze\Tests\Models\Child; +use Thomasjohnkane\Snooze\Tests\Models\User; class TestCase extends Orchestra { From d596fd3ec9fc5933b17947215b69cae7b6c3774a Mon Sep 17 00:00:00 2001 From: Adam <56907039+adamjsturge@users.noreply.github.com> Date: Wed, 12 Feb 2025 13:33:04 -0800 Subject: [PATCH 4/4] Last Lint fix I swear --- tests/SerializerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index a094dcb..100b1c8 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -5,7 +5,6 @@ namespace Thomasjohnkane\Snooze\Tests; use Illuminate\Database\ConnectionInterface; -use Illuminate\Database\PostgresConnection; use Mockery; use Thomasjohnkane\Snooze\Serializer; use Thomasjohnkane\Snooze\Tests\Models\User;