-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for disallowing class morphs (#7110)
- Loading branch information
1 parent
9625e73
commit c0a9eb3
Showing
4 changed files
with
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Database\Exception; | ||
|
||
use RuntimeException; | ||
|
||
class ClassMorphViolationException extends RuntimeException | ||
{ | ||
/** | ||
* The name of the affected Eloquent model. | ||
*/ | ||
public string $model; | ||
|
||
/** | ||
* Create a new exception instance. | ||
*/ | ||
public function __construct(object $model) | ||
{ | ||
$class = get_class($model); | ||
|
||
parent::__construct("No morph map defined for model [{$class}]."); | ||
|
||
$this->model = $class; | ||
} | ||
} |
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
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace HyperfTest\Database; | ||
|
||
use Hyperf\Database\Exception\ClassMorphViolationException; | ||
use Hyperf\Database\Model\Model; | ||
use Hyperf\Database\Model\Relations\Pivot; | ||
use Hyperf\Database\Model\Relations\Relation; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class DatabaseStrictMorphsTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Relation::requireMorphMap(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
Relation::morphMap([], false); | ||
Relation::requireMorphMap(false); | ||
} | ||
|
||
public function testStrictModeThrowsAnExceptionOnClassMap() | ||
{ | ||
$this->expectException(ClassMorphViolationException::class); | ||
|
||
$model = new TestModel(); | ||
|
||
$model->getMorphClass(); | ||
} | ||
|
||
public function testStrictModeDoesNotThrowExceptionWhenMorphMap() | ||
{ | ||
$model = new TestModel(); | ||
|
||
Relation::morphMap([ | ||
'foo' => TestModel::class, | ||
]); | ||
|
||
$morphName = $model->getMorphClass(); | ||
$this->assertSame('foo', $morphName); | ||
} | ||
|
||
public function testMapsCanBeEnforcedInOneMethod() | ||
{ | ||
$model = new TestModel(); | ||
|
||
Relation::requireMorphMap(false); | ||
|
||
Relation::enforceMorphMap([ | ||
'test' => TestModel::class, | ||
]); | ||
|
||
$morphName = $model->getMorphClass(); | ||
$this->assertSame('test', $morphName); | ||
} | ||
|
||
public function testMapIgnoreGenericPivotClass() | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
$pivotModel = new Pivot(); | ||
|
||
$pivotModel->getMorphClass(); | ||
} | ||
|
||
public function testMapCanBeEnforcedToCustomPivotClass() | ||
{ | ||
$this->expectException(ClassMorphViolationException::class); | ||
|
||
$pivotModel = new TestPivotModel(); | ||
|
||
$pivotModel->getMorphClass(); | ||
} | ||
} | ||
|
||
class TestModel extends Model | ||
{ | ||
} | ||
|
||
class TestPivotModel extends Pivot | ||
{ | ||
} |