-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from slashequip/feature/job-serialization
Ensure models are serialized correctly within attributes
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |