Skip to content

Commit

Permalink
Merge pull request #68 from slashequip/feature/job-serialization
Browse files Browse the repository at this point in the history
Ensure models are serialized correctly within attributes
  • Loading branch information
lorisleiva authored Nov 20, 2020
2 parents d1a09d2 + 0c74d56 commit d2d65f1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Concerns/SerializesModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,50 @@ trait SerializesModels
{
use BaseSerializesModels {
__sleep as protected sleepFromBaseSerializesModels;
__wakeup as protected wakeupFromBaseSerializesModels;
__serialize as protected serializeFromBaseSerializesModels;
__unserialize as protected unserializeFromBaseSerializesModels;
}

public function __sleep()
{
$properties = $this->sleepFromBaseSerializesModels();

array_walk($this->attributes, function (&$value) {
$value = $this->getSerializedPropertyValue($value);
});

return array_values(array_diff($properties, [
'request', 'runningAs', 'actingAs', 'errorBag', 'validator',
'commandInstance', 'commandSignature', 'commandDescription',
'getAttributesFromConstructor',
]));
}

public function __wakeup()
{
$this->wakeupFromBaseSerializesModels();

array_walk($this->attributes, function (&$value) {
$value = $this->getRestoredPropertyValue($value);
});
}

public function __serialize()
{
array_walk($this->attributes, function (&$value) {
$value = $this->getSerializedPropertyValue($value);
});

return $this->serializeFromBaseSerializesModels();
}

public function __unserialize(array $values)
{
$this->unserializeFromBaseSerializesModels($values);

array_walk($this->attributes, function (&$value) {
$value = $this->getRestoredPropertyValue($value);
});
}
}
56 changes: 56 additions & 0 deletions tests/SerializesModelsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Lorisleiva\Actions\Tests;

use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use Lorisleiva\Actions\Tests\Actions\UpdateProfileDetails;
use Lorisleiva\Actions\Tests\Stubs\User;

class SerializesModelsTest extends TestCase
{
use SerializesAndRestoresModelIdentifiers;

protected function setUp(): void
{
parent::setUp();

$this->loadLaravelMigrations();
}

/** @test */
public function it_serializes_models_within_action_attributes()
{
$user = $this->createUser([
'name' => 'Action San',
]);

$action = new UpdateProfileDetails(['user' => $user, 'name' => 'Laravel San']);

$this->assertNotFalse(
strpos(serialize($action), serialize($this->getSerializedPropertyValue($user)))
);
}

/** @test */
public function it_unserializes_models_within_action_attributes()
{
$user = $this->createUser([
'name' => 'Action San',
]);

// We want to be sure we have fetched the user from the DB
// rather than straight up unserializing into the class.
$hasBeenRetrieved = false;
User::retrieved(function () use (&$hasBeenRetrieved) {
$hasBeenRetrieved = true;
});

$action = new UpdateProfileDetails(['user' => $user, 'name' => 'Laravel San']);
$rehydratedAction = unserialize(serialize($action));

$this->assertTrue(
$user->is($rehydratedAction->get('user'))
);
$this->assertTrue($hasBeenRetrieved);
}
}

0 comments on commit d2d65f1

Please sign in to comment.