Skip to content

Commit

Permalink
Merge pull request #88 from lava83/fix/wrong-trait-initializer-in-readme
Browse files Browse the repository at this point in the history
Fix the wrong trait initializer in readme
  • Loading branch information
freekmurze authored Jul 22, 2021
2 parents cfbab0c + e388ba1 commit 1c1f294
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;

trait HasSchemalessAttributes
{
public function initializeHasSchemalessAttributesTrait()
public function initializeHasSchemalessAttributes()
{
$this->casts['extra_attributes'] = SchemalessAttributes::class;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Concerns/HasSchemalessAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\SchemalessAttributes\Tests\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;

/** @mixin \Illuminate\Database\Eloquent\Model */
trait HasSchemalessAttributes
{
public function initializeHasSchemalessAttributes()
{
$this->casts['schemaless_attributes'] = SchemalessAttributes::class;
}

public function scopeWithExtraAttributes(): Builder
{
return $this->schemaless_attributes->modelScope();
}
}
14 changes: 14 additions & 0 deletions tests/HasSchemalessAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

namespace Spatie\SchemalessAttributes\Tests;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

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 */
Expand Down Expand Up @@ -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';
Expand Down
15 changes: 15 additions & 0 deletions tests/TestModelUsedHasSchemalessAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\SchemalessAttributes\Tests;

use Illuminate\Database\Eloquent\Model;
use Spatie\SchemalessAttributes\Tests\Concerns\HasSchemalessAttributes;

class TestModelUsedHasSchemalessAttributesTrait extends Model
{
use HasSchemalessAttributes;

public $timestamps = false;

public $guarded = [];
}

0 comments on commit 1c1f294

Please sign in to comment.