From 3d93cb8c505cfc5973cfca966170eb972575d9c9 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 6 Mar 2018 04:35:04 +0100 Subject: [PATCH 1/2] Remove attribute filling from pivot model --- .../Database/Eloquent/Relations/Pivot.php | 2 +- tests/Database/DatabaseEloquentPivotTest.php | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Pivot.php b/src/Illuminate/Database/Eloquent/Relations/Pivot.php index 4782bc021779..5f7323ce47f7 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Pivot.php +++ b/src/Illuminate/Database/Eloquent/Relations/Pivot.php @@ -80,7 +80,7 @@ public static function fromAttributes(Model $parent, $attributes, $table, $exist */ public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = false) { - $instance = static::fromAttributes($parent, $attributes, $table, $exists); + $instance = static::fromAttributes($parent, [], $table, $exists); $instance->setRawAttributes($attributes, true); diff --git a/tests/Database/DatabaseEloquentPivotTest.php b/tests/Database/DatabaseEloquentPivotTest.php index 570239df5a50..b210382eb10a 100755 --- a/tests/Database/DatabaseEloquentPivotTest.php +++ b/tests/Database/DatabaseEloquentPivotTest.php @@ -37,14 +37,14 @@ public function testMutatorsAreCalledFromConstructor() $this->assertTrue($pivot->getMutatorCalled()); } - public function testFromRawAttributesDoesNotDoubleMutate() + public function testFromRawAttributesDoesNotMutate() { $parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]'); $parent->shouldReceive('getConnectionName')->once()->andReturn('connection'); - $pivot = DatabaseEloquentPivotTestJsonCastStub::fromRawAttributes($parent, ['foo' => json_encode(['name' => 'Taylor'])], 'table', true); + $pivot = DatabaseEloquentPivotTestMutatorStub::fromRawAttributes($parent, ['foo' => 'bar'], 'table', true); - $this->assertEquals(['name' => 'Taylor'], $pivot->foo); + $this->assertFalse($pivot->getMutatorCalled()); } public function testPropertiesUnchangedAreNotDirty() @@ -135,10 +135,3 @@ public function getMutatorCalled() return $this->mutatorCalled; } } - -class DatabaseEloquentPivotTestJsonCastStub extends \Illuminate\Database\Eloquent\Relations\Pivot -{ - protected $casts = [ - 'foo' => 'json', - ]; -} From 6635f56905e30290099def318b8549458c184120 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 6 Mar 2018 13:44:31 +0100 Subject: [PATCH 2/2] Re-add test --- tests/Database/DatabaseEloquentPivotTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Database/DatabaseEloquentPivotTest.php b/tests/Database/DatabaseEloquentPivotTest.php index b210382eb10a..fefabe85b999 100755 --- a/tests/Database/DatabaseEloquentPivotTest.php +++ b/tests/Database/DatabaseEloquentPivotTest.php @@ -37,6 +37,16 @@ public function testMutatorsAreCalledFromConstructor() $this->assertTrue($pivot->getMutatorCalled()); } + public function testFromRawAttributesDoesNotDoubleMutate() + { + $parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]'); + $parent->shouldReceive('getConnectionName')->once()->andReturn('connection'); + + $pivot = DatabaseEloquentPivotTestJsonCastStub::fromRawAttributes($parent, ['foo' => json_encode(['name' => 'Taylor'])], 'table', true); + + $this->assertEquals(['name' => 'Taylor'], $pivot->foo); + } + public function testFromRawAttributesDoesNotMutate() { $parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]'); @@ -135,3 +145,10 @@ public function getMutatorCalled() return $this->mutatorCalled; } } + +class DatabaseEloquentPivotTestJsonCastStub extends \Illuminate\Database\Eloquent\Relations\Pivot +{ + protected $casts = [ + 'foo' => 'json', + ]; +}