From 3a8f4262896eb64fe4506851bf0c97332536d73f Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 15 Oct 2024 11:04:27 -0700 Subject: [PATCH 1/3] Mutate original values --- src/Database/Model.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Database/Model.php b/src/Database/Model.php index 118a836..bc67dac 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -54,6 +54,30 @@ public function attributesToArray() return $attributes; } + /** + * {@inheritdoc} + */ + public function getOriginal($key = null, $default = null) + { + $original = parent::getOriginal($key, $default); + + if ($key) { + if ($this->hasMutator($key)) { + return $this->unserializeAttribute($key, $original); + } + + return $original; + } + + foreach ($original as $attribute => $value) { + if ($this->hasMutator($attribute)) { + $original[$attribute] = $this->unserializeAttribute($attribute, $value); + } + } + + return $original; + } + /** * Define a many-to-many relationship. * From 06274986a413b247139231465a9e368131dedcd2 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 15 Oct 2024 12:26:27 -0700 Subject: [PATCH 2/3] Add `getOriginal` mutator test --- .../Unit/Database/Traits/HasMutatorsTest.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/Unit/Database/Traits/HasMutatorsTest.php b/tests/Unit/Database/Traits/HasMutatorsTest.php index 26f3343..2d4a50f 100644 --- a/tests/Unit/Database/Traits/HasMutatorsTest.php +++ b/tests/Unit/Database/Traits/HasMutatorsTest.php @@ -52,6 +52,49 @@ public function testUnserializeAttribute() $this->assertEquals('serialized_attribute', $model->id); } + public function testGetOriginal() + { + $uuid = 'cf98906e-9074-11e7-9c8e-437b4bab8527'; + $mutator = M::mock(MutatorContract::class) + ->shouldReceive('get') + ->with('test_mutator') + ->andReturnSelf() + ->once() + ->shouldReceive('unserializeAttribute') + ->with('unserialized_attribute') + ->andReturn('serialized_attribute') + ->once() + ->getMock(); + + app()['mutator'] = $mutator; + + $model = new SampleModel(); + $original = $model->getOriginal(); + + $this->assertIsArray($original); + $this->assertEquals('serialized_attribute', $original['id']); + } + + public function testGetOriginalProperty() + { + $uuid = 'cf98906e-9074-11e7-9c8e-437b4bab8527'; + $mutator = M::mock(MutatorContract::class) + ->shouldReceive('get') + ->with('test_mutator') + ->andReturnSelf() + ->once() + ->shouldReceive('unserializeAttribute') + ->with('unserialized_attribute') + ->andReturn('serialized_attribute') + ->once() + ->getMock(); + + app()['mutator'] = $mutator; + + $model = new SampleModel(); + $this->assertEquals('serialized_attribute', $model->getOriginal('id')); + } + public function testGetMutators() { $this->assertEquals(['id' => 'test_mutator'], (new SampleModel())->getMutators()); From 3384db10536de1891dff7125017c570223094269 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Tue, 15 Oct 2024 12:55:54 -0700 Subject: [PATCH 3/3] Remove unused variable --- tests/Unit/Database/Traits/HasMutatorsTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Unit/Database/Traits/HasMutatorsTest.php b/tests/Unit/Database/Traits/HasMutatorsTest.php index 2d4a50f..731ba43 100644 --- a/tests/Unit/Database/Traits/HasMutatorsTest.php +++ b/tests/Unit/Database/Traits/HasMutatorsTest.php @@ -31,7 +31,6 @@ public function testSerializeAttribute() public function testUnserializeAttribute() { - $uuid = 'cf98906e-9074-11e7-9c8e-437b4bab8527'; $mutator = M::mock(MutatorContract::class) ->shouldReceive('get') ->with('test_mutator') @@ -54,7 +53,6 @@ public function testUnserializeAttribute() public function testGetOriginal() { - $uuid = 'cf98906e-9074-11e7-9c8e-437b4bab8527'; $mutator = M::mock(MutatorContract::class) ->shouldReceive('get') ->with('test_mutator') @@ -77,7 +75,6 @@ public function testGetOriginal() public function testGetOriginalProperty() { - $uuid = 'cf98906e-9074-11e7-9c8e-437b4bab8527'; $mutator = M::mock(MutatorContract::class) ->shouldReceive('get') ->with('test_mutator')