Skip to content

Commit

Permalink
Merge pull request #9 from Riley19280/model-traits
Browse files Browse the repository at this point in the history
Add Trait information to model info
  • Loading branch information
freekmurze authored Sep 15, 2022
2 parents e432520 + 6abf4ab commit 1fb6ddb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpunit/phpunit": "^9.5",
"spatie/pest-plugin-snapshots": "^1.1"}
,
"spatie/pest-plugin-snapshots": "^1.1",
"symfony/serializer": "^6"
},
"autoload": {
"psr-4": {
"Spatie\\ModelInfo\\": "src"
Expand Down
7 changes: 7 additions & 0 deletions src/ModelInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static function forModel(string|Model|ReflectionClass $model): self
$model->getConnection()->getTablePrefix().$model->getTable(),
RelationFinder::forModel($model),
AttributeFinder::forModel($model),
self::getTraits($model),
self::getExtraModelInfo($model),
);
}
Expand All @@ -61,6 +62,7 @@ public function __construct(
public string $tableName,
public Collection $relations,
public Collection $attributes,
public Collection $traits,
public mixed $extra = null,
) {
}
Expand All @@ -74,6 +76,11 @@ protected static function getExtraModelInfo(Model $model): mixed
return null;
}

protected static function getTraits(Model $model): Collection
{
return collect(array_values(class_uses($model)));
}

public function toArray(): array
{
$properties = get_object_vars($this);
Expand Down
4 changes: 3 additions & 1 deletion tests/ModelFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\ModelInfo\Tests\TestSupport\Models\Nested\Model\NestedModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\RelationTestModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\TestModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\TraitTestModel;

it('can discover all models in a directory', function () {
$models = ModelFinder::all(
Expand All @@ -15,12 +16,13 @@
"Spatie\ModelInfo\Tests",
);

expect($models)->toHaveCount(4);
expect($models)->toHaveCount(5);

expect($models->toArray())->toEqualCanonicalizing([
NestedModel::class,
ExtraModelInfoModel::class,
RelationTestModel::class,
TestModel::class,
TraitTestModel::class,
]);
});
10 changes: 9 additions & 1 deletion tests/ModelInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Spatie\ModelInfo\ModelInfo;
use Spatie\ModelInfo\Tests\TestSupport\Models\ExtraModelInfoModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\RelationTestModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\TraitTestModel;
use Spatie\ModelInfo\Tests\TestSupport\Traits\TestTrait;

it('can get meta information about a model', function () {
$modelInfo = ModelInfo::forModel(RelationTestModel::class);
Expand All @@ -23,7 +25,7 @@
"Spatie\ModelInfo\Tests",
);

expect($modelInfo)->toHaveCount(4);
expect($modelInfo)->toHaveCount(5);
expect($modelInfo->first())->toBeInstanceOf(ModelInfo::class);
});

Expand All @@ -33,6 +35,12 @@
expect($modelInfo->extra)->toBe('extra info');
});

it('can get traits from a model', function () {
$modelInfo = ModelInfo::forModel(TraitTestModel::class);

expect($modelInfo->traits->first())->toBe(TestTrait::class);
});

it('can get a specific attribute', function () {
$modelInfo = ModelInfo::forModel(RelationTestModel::class);

Expand Down
11 changes: 11 additions & 0 deletions tests/TestSupport/Models/TraitTestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\ModelInfo\Tests\TestSupport\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelInfo\Tests\TestSupport\Traits\TestTrait;

class TraitTestModel extends Model
{
use TestTrait;
}
7 changes: 7 additions & 0 deletions tests/TestSupport/Traits/TestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Spatie\ModelInfo\Tests\TestSupport\Traits;

trait TestTrait
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ attributes:
- { name: name, phpType: string, type: string, increments: false, nullable: false, default: null, primary: false, unique: false, fillable: false, appended: null, cast: null, virtual: false }
- { name: created_at, phpType: DateTime, type: datetime, increments: false, nullable: true, default: null, primary: false, unique: false, fillable: false, appended: null, cast: datetime, virtual: false }
- { name: updated_at, phpType: DateTime, type: datetime, increments: false, nullable: true, default: null, primary: false, unique: false, fillable: false, appended: null, cast: datetime, virtual: false }
traits: { }
extra: null

0 comments on commit 1fb6ddb

Please sign in to comment.