Skip to content
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

[9.x] Change how Laravel handles strict morph map with pivot classes #41304

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -732,6 +733,10 @@ public function getMorphClass()
return array_search(static::class, $morphMap, true);
}

if (static::class === Pivot::class) {
return static::class;
}

if (Relation::requiresMorphMap()) {
throw new ClassMorphViolationException($this);
}
Expand Down
27 changes: 24 additions & 3 deletions tests/Database/DatabaseEloquentStrictMorphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\ClassMorphViolationException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\Relation;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,14 +21,14 @@ public function testStrictModeThrowsAnExceptionOnClassMap()
{
$this->expectException(ClassMorphViolationException::class);

$model = TestModel::make();
$model = new TestModel;

$model->getMorphClass();
}

public function testStrictModeDoesNotThrowExceptionWhenMorphMap()
{
$model = TestModel::make();
$model = new TestModel;

Relation::morphMap([
'test' => TestModel::class,
Expand All @@ -39,7 +40,7 @@ public function testStrictModeDoesNotThrowExceptionWhenMorphMap()

public function testMapsCanBeEnforcedInOneMethod()
{
$model = TestModel::make();
$model = new TestModel;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

faced issue while running test on single file:

image

Change to new TestModel fixed that.


Relation::requireMorphMap(false);

Expand All @@ -51,6 +52,22 @@ public function testMapsCanBeEnforcedInOneMethod()
$this->assertSame('test', $morphName);
}

public function testMapIgnoreGenericPivotClass()
{
$pivotModel = new Pivot();

$pivotModel->getMorphClass();
}

public function testMapCanBeEnforcedToCustomPivotClass()
{
$this->expectException(ClassMorphViolationException::class);

$pivotModel = new TestPivotModel();

$pivotModel->getMorphClass();
}

protected function tearDown(): void
{
parent::tearDown();
Expand All @@ -63,3 +80,7 @@ protected function tearDown(): void
class TestModel extends Model
{
}

class TestPivotModel extends Pivot
{
}