diff --git a/README.md b/README.md index 9a8f026..91deef8 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ use Spatie\SchemalessAttributes\Casts\SchemalessAttributes; trait HasSchemalessAttributes { - public function initializeHasSchemalessAttributesTrait() + public function initializeHasSchemalessAttributes() { $this->casts['extra_attributes'] = SchemalessAttributes::class; } diff --git a/tests/Concerns/HasSchemalessAttributes.php b/tests/Concerns/HasSchemalessAttributes.php new file mode 100644 index 0000000..7a66cb7 --- /dev/null +++ b/tests/Concerns/HasSchemalessAttributes.php @@ -0,0 +1,20 @@ +casts['schemaless_attributes'] = SchemalessAttributes::class; + } + + public function scopeWithExtraAttributes(): Builder + { + return $this->schemaless_attributes->modelScope(); + } +} diff --git a/tests/HasSchemalessAttributesTest.php b/tests/HasSchemalessAttributesTest.php index 32832af..8879263 100644 --- a/tests/HasSchemalessAttributesTest.php +++ b/tests/HasSchemalessAttributesTest.php @@ -2,6 +2,7 @@ namespace Spatie\SchemalessAttributes\Tests; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; class HasSchemalessAttributesTest extends TestCase @@ -9,11 +10,16 @@ class HasSchemalessAttributesTest extends TestCase /** @var \Spatie\SchemalessAttributes\Tests\TestModel */ protected $testModel; + /** @var \Spatie\SchemalessAttributes\Tests\TestModel */ + protected $testModelUsedTrait; + public function setUp(): void { parent::setUp(); $this->testModel = TestModel::create(); + + $this->testModelUsedTrait = new TestModel(); } /** @test */ @@ -431,6 +437,14 @@ public function it_can_call_collection_method_only() ], $this->testModel->schemaless_attributes->toArray()); } + /** @test */ + public function an_schemaless_attribute_can_be_set_if_the_has_schemaless_atrributes_trait_is_used(): void + { + $this->testModelUsedTrait->schemaless_attributes->name = 'value'; + + $this->assertEquals('value', $this->testModelUsedTrait->schemaless_attributes->name); + } + protected function assertContainsModels(array $expectedModels, Collection $actualModels) { $assertionFailedMessage = 'Expected '.count($expectedModels).' models. Got '.$actualModels->count().' models'; diff --git a/tests/TestModelUsedHasSchemalessAttributesTrait.php b/tests/TestModelUsedHasSchemalessAttributesTrait.php new file mode 100644 index 0000000..dc385ee --- /dev/null +++ b/tests/TestModelUsedHasSchemalessAttributesTrait.php @@ -0,0 +1,15 @@ +