Skip to content

Add serialize without relationships #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/snooze.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
*/
'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),

/*
* Set true if you want delete notification when
* any model missing during unserialization
Expand Down
3 changes: 1 addition & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Models/Child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Thomasjohnkane\Snooze\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Child extends Model
{
protected $fillable = ['name', 'user_id'];

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
6 changes: 6 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
51 changes: 51 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Thomasjohnkane\Snooze\Tests;

use Illuminate\Database\ConnectionInterface;
use Mockery;
use Thomasjohnkane\Snooze\Serializer;
use Thomasjohnkane\Snooze\Tests\Models\User;

class SerializerTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
Mockery::close();
}

public function testSerializesModelWithoutRelationsWhenConfigIsTrue()
{
config(['snooze.doNotLoadRelationsOnSerialize' => 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'));
}
}
29 changes: 22 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Orchestra\Testbench\TestCase as Orchestra;
use Thomasjohnkane\Snooze\Facades\Snooze;
use Thomasjohnkane\Snooze\ServiceProvider;
use Thomasjohnkane\Snooze\Tests\Models\Child;
use Thomasjohnkane\Snooze\Tests\Models\User;

class TestCase extends Orchestra
Expand Down Expand Up @@ -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,
]);
}
}
}
}