diff --git a/.github/workflows/duster-lint.yml b/.github/workflows/duster-lint.yml new file mode 100644 index 0000000..305e492 --- /dev/null +++ b/.github/workflows/duster-lint.yml @@ -0,0 +1,17 @@ +name: Duster Lint + +on: + push: + branches: [ main ] + pull_request: + +jobs: + duster: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: "Duster Lint" + uses: tighten/duster-action@v2 + with: + args: lint diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3dd482b..990ce21 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,6 +44,7 @@ jobs: - name: Install Dependencies run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" --no-interaction --no-update + composer remove "tightenco/duster" --dev --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - name: Execute Tests run: ./vendor/bin/phpunit --testdox diff --git a/composer.json b/composer.json index 173b0d6..8d38d7f 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ }, "require-dev": { "orchestra/testbench": "^7.0||^8.0", - "phpunit/phpunit": "^9.5.10||^10.0" + "phpunit/phpunit": "^9.5.10||^10.0", + "tightenco/duster": "^2.7" }, "autoload": { "psr-4": { @@ -35,5 +36,9 @@ "Parental\\Tests\\": "tests/", "Database\\Factories\\": "tests/factories/" } + }, + "scripts": { + "lint": "vendor/bin/duster lint", + "fix": "vendor/bin/duster fix" } } diff --git a/src/HasChildren.php b/src/HasChildren.php index 77c8ff7..aa5c31e 100644 --- a/src/HasChildren.php +++ b/src/HasChildren.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Str; +use UnitEnum; trait HasChildren { @@ -24,8 +25,7 @@ trait HasChildren * Register a model event with the dispatcher. * * @param string $event - * @param Closure|string $callback - * @return void + * @param Closure|string $callback */ protected static function registerModelEvent($event, $callback): void { @@ -46,16 +46,13 @@ protected static function registerModelEvent($event, $callback): void } } - /** - * @return bool - */ protected static function parentIsBooting(): bool { if (! isset(self::$parentBootMethods)) { self::$parentBootMethods[] = 'boot'; foreach (class_uses_recursive(self::class) as $trait) { - self::$parentBootMethods[] = 'boot'.class_basename($trait); + self::$parentBootMethods[] = 'boot' . class_basename($trait); } self::$parentBootMethods = array_flip(self::$parentBootMethods); @@ -131,14 +128,13 @@ public function newFromBuilder($attributes = [], $connection = null): self * @param string|null $foreignKey * @param string|null $ownerKey * @param string|null $relation - * @return BelongsTo */ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null): BelongsTo { $instance = $this->newRelatedInstance($related); if (is_null($foreignKey) && $instance->hasParent) { - $foreignKey = Str::snake($instance->getClassNameForRelationships()).'_'.$instance->getKeyName(); + $foreignKey = Str::snake($instance->getClassNameForRelationships()) . '_' . $instance->getKeyName(); } if (is_null($relation)) { @@ -154,7 +150,6 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat * @param string $related * @param string|null $foreignKey * @param string|null $localKey - * @return HasMany */ public function hasMany($related, $foreignKey = null, $localKey = null): HasMany { @@ -171,7 +166,6 @@ public function hasMany($related, $foreignKey = null, $localKey = null): HasMany * @param string|null $parentKey * @param string|null $relatedKey * @param string|null $relation - * @return BelongsToMany */ public function belongsToMany( $related, $table = null, @@ -198,45 +192,25 @@ public function belongsToMany( ); } - /** - * @return string - */ public function getClassNameForRelationships(): string { return class_basename($this); } - /** - * @return string - */ public function getInheritanceColumn(): string { return property_exists($this, 'childColumn') ? $this->childColumn : 'type'; } /** - * @param array $attributes - * @return mixed - */ - protected function getChildModel(array $attributes) - { - $className = $this->classFromAlias( - $attributes[$this->getInheritanceColumn()] - ); - - return new $className((array) $attributes); - } - - /** - * @param mixed $aliasOrClass - * @return string + * @param mixed $aliasOrClass */ public function classFromAlias($aliasOrClass): string { $childTypes = $this->getChildTypes(); // Handling Enum casting for `type` column - if ($aliasOrClass instanceof \UnitEnum) { + if ($aliasOrClass instanceof UnitEnum) { $aliasOrClass = $aliasOrClass->value; } @@ -247,10 +221,6 @@ public function classFromAlias($aliasOrClass): string return $aliasOrClass; } - /** - * @param string $className - * @return string - */ public function classToAlias(string $className): string { $childTypes = $this->getChildTypes(); @@ -262,9 +232,6 @@ public function classToAlias(string $className): string return $className; } - /** - * @return array - */ public function getChildTypes(): array { if (method_exists($this, 'childTypes')) { @@ -277,4 +244,16 @@ public function getChildTypes(): array return []; } + + /** + * @return mixed + */ + protected function getChildModel(array $attributes) + { + $className = $this->classFromAlias( + $attributes[$this->getInheritanceColumn()] + ); + + return new $className((array) $attributes); + } } diff --git a/src/HasParent.php b/src/HasParent.php index 64549f2..94cbfc0 100644 --- a/src/HasParent.php +++ b/src/HasParent.php @@ -3,9 +3,9 @@ namespace Parental; use Illuminate\Database\Eloquent\Model; -use ReflectionClass; -use Illuminate\Support\Str; use Illuminate\Events\Dispatcher; +use Illuminate\Support\Str; +use ReflectionClass; use ReflectionException; trait HasParent @@ -16,14 +16,13 @@ trait HasParent public $hasParent = true; /** - * @return void * @throws ReflectionException */ public static function bootHasParent(): void { // This adds support for using Parental with standalone Eloquent, outside a normal Laravel app. if (static::getEventDispatcher() === null) { - static::setEventDispatcher(new Dispatcher()); + static::setEventDispatcher(new Dispatcher); } static::creating(function ($model) { @@ -38,21 +37,17 @@ public static function bootHasParent(): void $instance = new static; if ($instance->parentHasHasChildrenTrait()) { - $query->where($query->getModel()->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance))); + $query->where($query->getModel()->getTable() . '.' . $instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance))); } }); } - /** - * @return bool - */ public function parentHasHasChildrenTrait(): bool { return $this->hasChildren ?? false; } /** - * @return string * @throws ReflectionException */ public function getTable(): string @@ -65,18 +60,17 @@ public function getTable(): string } /** - * @return string * @throws ReflectionException */ public function getForeignKey(): string { - return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey; + return Str::snake(class_basename($this->getParentClass())) . '_' . $this->primaryKey; } /** - * @param string $related - * @param null|Model $instance - * @return string + * @param string $related + * @param null|Model $instance + * * @throws ReflectionException */ public function joiningTable($related, $instance = null): string @@ -96,7 +90,6 @@ public function joiningTable($related, $instance = null): string } /** - * @return string * @throws ReflectionException */ public function getClassNameForRelationships(): string @@ -107,7 +100,6 @@ public function getClassNameForRelationships(): string /** * Get the class name for polymorphic relations. * - * @return string * @throws ReflectionException */ public function getMorphClass(): string @@ -116,11 +108,10 @@ public function getMorphClass(): string return (new $parentClass)->getMorphClass(); } - + /** * Get the class name for poly-type collections * - * @return string * @throws ReflectionException */ public function getQueueableClassName(): string @@ -128,10 +119,27 @@ public function getQueueableClassName(): string return $this->getParentClass(); } + /** + * Merge the fillable attributes for the model with those of its Parent Class + * + * @return array + */ + public function getFillable() + { + $parentClass = $this->getParentClass(); + + if ((new ReflectionClass($parentClass))->isAbstract()) { + + return $this->fillable; + } + $parentFillable = (new $parentClass)->getFillable(); + + return array_unique(array_merge($parentFillable, $this->fillable)); + } + /** * Get the class name for Parent Class. * - * @return string * @throws ReflectionException */ protected function getParentClass(): string diff --git a/src/Providers/NovaResourceProvider.php b/src/Providers/NovaResourceProvider.php index 87f9381..b43d743 100644 --- a/src/Providers/NovaResourceProvider.php +++ b/src/Providers/NovaResourceProvider.php @@ -9,9 +9,6 @@ class NovaResourceProvider extends ServiceProvider { - /** - * @return void - */ public function boot(): void { if (class_exists(Nova::class)) { @@ -21,9 +18,6 @@ public function boot(): void } } - /** - * @return void - */ protected function setNovaResources(): void { $map = []; diff --git a/tests/Features/AuthUserMethodReturnsChildModel.php b/tests/Features/AuthUserMethodReturnsChildModel.php index ac23453..0d90606 100644 --- a/tests/Features/AuthUserMethodReturnsChildModel.php +++ b/tests/Features/AuthUserMethodReturnsChildModel.php @@ -2,15 +2,12 @@ namespace Parental\Tests\Features; -use Parental\Tests\Models\Car; -use Parental\Tests\Models\Plane; -use Parental\Tests\Models\Vehicle; use Parental\Tests\TestCase; class AuthUserMethodReturnsChildModel extends TestCase { /** @test */ - function auth_user_returns_child_model_if_it_exists() + public function auth_user_returns_child_model_if_it_exists() { Admin::create(); User::create(); diff --git a/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php new file mode 100644 index 0000000..10e2f3b --- /dev/null +++ b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php @@ -0,0 +1,24 @@ + 'Scaling Laravel', + 'industry' => 'Technology', + 'skill_level' => 'Advanced', + ]); + + $event = Event::first(); + + $this->assertEquals($event->name, $workshop->name); + } +} diff --git a/tests/Features/ChildModelsActLikeParentModelsTest.php b/tests/Features/ChildModelsActLikeParentModelsTest.php index b4cf2a4..a5452bb 100644 --- a/tests/Features/ChildModelsActLikeParentModelsTest.php +++ b/tests/Features/ChildModelsActLikeParentModelsTest.php @@ -5,14 +5,13 @@ use Parental\Tests\Models\Car; use Parental\Tests\Models\Driver; use Parental\Tests\Models\Passenger; -use Parental\Tests\Models\Trip; use Parental\Tests\Models\Vehicle; use Parental\Tests\TestCase; class ChildModelsActLikeParentModelsTest extends TestCase { /** @test */ - function vehicle_can_access_belongs_to_relationship_on_car_model() + public function vehicle_can_access_belongs_to_relationship_on_car_model() { $car = Car::create([ 'driver_id' => Driver::create(['name' => 'Joe'])->id, @@ -24,7 +23,7 @@ function vehicle_can_access_belongs_to_relationship_on_car_model() } /** @test */ - function vehicle_can_access_has_many_relationship_on_car_model() + public function vehicle_can_access_has_many_relationship_on_car_model() { $car = Car::create(); @@ -37,7 +36,7 @@ function vehicle_can_access_has_many_relationship_on_car_model() } /** @test */ - function vehicle_can_access_many_to_many_relationship_on_car_model() + public function vehicle_can_access_many_to_many_relationship_on_car_model() { $car = Car::create(); diff --git a/tests/Features/ChildModelsAreAutomaticallyScopedTest.php b/tests/Features/ChildModelsAreAutomaticallyScopedTest.php index 3082dd7..629700b 100644 --- a/tests/Features/ChildModelsAreAutomaticallyScopedTest.php +++ b/tests/Features/ChildModelsAreAutomaticallyScopedTest.php @@ -17,7 +17,7 @@ class ChildModelsAreAutomaticallyScopedTest extends TestCase { /** @test */ - function child_is_scoped_based_on_type_column() + public function child_is_scoped_based_on_type_column() { Car::create(); Vehicle::create(); @@ -27,7 +27,7 @@ function child_is_scoped_based_on_type_column() } /** @test */ - function child_without_type_column_isnt_scoped() + public function child_without_type_column_isnt_scoped() { Admin::create(); User::create(); @@ -37,7 +37,7 @@ function child_without_type_column_isnt_scoped() } /** @test */ - function child_is_scoped_when_accessed_from_belongs_to() + public function child_is_scoped_when_accessed_from_belongs_to() { $car = Car::create(); $vehicle = Vehicle::create(); @@ -53,7 +53,7 @@ function child_is_scoped_when_accessed_from_belongs_to() } /** @test */ - function child_is_scoped_when_accessed_from_has_many() + public function child_is_scoped_when_accessed_from_has_many() { $driver = Driver::create(['name' => 'joe']); Car::create(['driver_id' => $driver->id]); @@ -64,7 +64,7 @@ function child_is_scoped_when_accessed_from_has_many() } /** @test */ - function child_is_scoped_when_accessed_from_belongs_to_many() + public function child_is_scoped_when_accessed_from_belongs_to_many() { $car = Car::create(); $vehicle = Vehicle::create(); @@ -76,7 +76,7 @@ function child_is_scoped_when_accessed_from_belongs_to_many() } /** @test */ - function child_is_scoped_when_accessed_from_has_one_through() + public function child_is_scoped_when_accessed_from_has_one_through() { // Create root with children $rootA = ParentNode::create(['name' => 'Root A']); @@ -97,7 +97,7 @@ function child_is_scoped_when_accessed_from_has_one_through() } /** @test */ - function child_is_scoped_when_accessed_from_has_many_through() + public function child_is_scoped_when_accessed_from_has_many_through() { // Create root with children $rootA = ParentNode::create(['name' => 'Root A']); diff --git a/tests/Features/ParentsAreAwareOfChildrenTest.php b/tests/Features/ParentsAreAwareOfChildrenTest.php index bb64774..982e9af 100644 --- a/tests/Features/ParentsAreAwareOfChildrenTest.php +++ b/tests/Features/ParentsAreAwareOfChildrenTest.php @@ -12,7 +12,7 @@ class ParentsAreAwareOfChildrenTest extends TestCase { /** @test */ - function vehicle_all_method_returns_child_models() + public function vehicle_all_method_returns_child_models() { Car::create(['type' => Car::class]); Plane::create(['type' => Plane::class]); @@ -24,7 +24,7 @@ function vehicle_all_method_returns_child_models() } /** @test */ - function type_column_values_can_accept_type_aliases() + public function type_column_values_can_accept_type_aliases() { // Looks for "childTypes" property on Vehicle class. Car::create(['type' => 'car']); @@ -37,7 +37,7 @@ function type_column_values_can_accept_type_aliases() } /** @test */ - function vehicle_query_builder_get_method_returns_child_models() + public function vehicle_query_builder_get_method_returns_child_models() { Car::create(['type' => Car::class]); Plane::create(['type' => Plane::class]); @@ -51,7 +51,7 @@ function vehicle_query_builder_get_method_returns_child_models() } /** @test */ - function has_many_returns_child_models() + public function has_many_returns_child_models() { $driver = Driver::create(['name' => 'Joe']); Car::create([ @@ -67,7 +67,7 @@ function has_many_returns_child_models() } /** @test */ - function belongs_to_returns_child_models() + public function belongs_to_returns_child_models() { $car = Car::create(['type' => Car::class]); $passenger = Passenger::create([ @@ -81,7 +81,7 @@ function belongs_to_returns_child_models() } /** @test */ - function many_to_many_returns_child_models() + public function many_to_many_returns_child_models() { $car = Car::create(['type' => Car::class]); $trip = $car->trips()->create([]); diff --git a/tests/Features/TypeColumnCanBeAliasedTest.php b/tests/Features/TypeColumnCanBeAliasedTest.php index ee67a3a..65153a4 100644 --- a/tests/Features/TypeColumnCanBeAliasedTest.php +++ b/tests/Features/TypeColumnCanBeAliasedTest.php @@ -16,7 +16,7 @@ class TypeColumnCanBeAliasedTest extends TestCase { /** @test */ - function type_column_values_can_accept_type_aliases() + public function type_column_values_can_accept_type_aliases() { Car::create(['type' => 'car']); Plane::create(['type' => Plane::class]); @@ -28,7 +28,7 @@ function type_column_values_can_accept_type_aliases() } /** @test */ - function type_aliases_are_set_on_creation() + public function type_aliases_are_set_on_creation() { $car = Car::create(); @@ -36,7 +36,7 @@ function type_aliases_are_set_on_creation() } /** @test */ - function type_column_values_can_accept_type_aliases_from_abstract_parent() + public function type_column_values_can_accept_type_aliases_from_abstract_parent() { ChildFromAbstractParent::create(['type' => 'ChildFromAbstractParent']); @@ -46,7 +46,7 @@ function type_column_values_can_accept_type_aliases_from_abstract_parent() } /** @test */ - function enums_can_be_used_as_type_alias() + public function enums_can_be_used_as_type_alias() { if (phpversion() < 8.1) { $this->markTestSkipped('Enums are not supported in this version of PHP'); diff --git a/tests/Features/TypeColumnGetsSetAutomaticallyTest.php b/tests/Features/TypeColumnGetsSetAutomaticallyTest.php index 4b35b4d..1e53df3 100644 --- a/tests/Features/TypeColumnGetsSetAutomaticallyTest.php +++ b/tests/Features/TypeColumnGetsSetAutomaticallyTest.php @@ -11,7 +11,7 @@ class TypeColumnGetsSetAutomaticallyTest extends TestCase { /** @test */ - function type_column_gets_set_on_creation() + public function type_column_gets_set_on_creation() { $car = Car::create(); @@ -19,7 +19,7 @@ function type_column_gets_set_on_creation() } /** @test */ - function type_column_gets_set_on_creation_from_many_to_many_relationship() + public function type_column_gets_set_on_creation_from_many_to_many_relationship() { $trip = Trip::create(); $car = $trip->cars()->create([]); @@ -28,7 +28,7 @@ function type_column_gets_set_on_creation_from_many_to_many_relationship() } /** @test */ - function type_column_gets_set_on_creation_from_has_many_relationship() + public function type_column_gets_set_on_creation_from_has_many_relationship() { $driver = Driver::create(['name' => 'Joe']); $car = $driver->cars()->create([]); @@ -37,7 +37,7 @@ function type_column_gets_set_on_creation_from_has_many_relationship() } /** @test */ - function type_column_gets_set_on_saving_from_has_many_relationship() + public function type_column_gets_set_on_saving_from_has_many_relationship() { $driver = Driver::create(['name' => 'Joe']); $car = $driver->cars()->save(new Car); @@ -46,7 +46,7 @@ function type_column_gets_set_on_saving_from_has_many_relationship() } /** @test */ - function type_column_gets_set_on_creation_from_a_model_factory() + public function type_column_gets_set_on_creation_from_a_model_factory() { $car = Car::factory()->create(); @@ -54,7 +54,7 @@ function type_column_gets_set_on_creation_from_a_model_factory() } /** @test */ - function custom_type_column_gets_used() + public function custom_type_column_gets_used() { $internationalTrip = InternationalTrip::create(); diff --git a/tests/Models/AbstractParent.php b/tests/Models/AbstractParent.php index 9ff5259..592a0c9 100644 --- a/tests/Models/AbstractParent.php +++ b/tests/Models/AbstractParent.php @@ -10,7 +10,7 @@ abstract class AbstractParent extends Model use HasChildren; protected $fillable = [ - 'type' + 'type', ]; protected $childTypes = [ diff --git a/tests/Models/Car.php b/tests/Models/Car.php index 79f296b..47eed80 100644 --- a/tests/Models/Car.php +++ b/tests/Models/Car.php @@ -7,6 +7,6 @@ class Car extends Vehicle { - use HasParent; use HasFactory; + use HasParent; } diff --git a/tests/Models/ChildFromAbstractParent.php b/tests/Models/ChildFromAbstractParent.php index 6265cc4..ac76356 100644 --- a/tests/Models/ChildFromAbstractParent.php +++ b/tests/Models/ChildFromAbstractParent.php @@ -7,6 +7,6 @@ class ChildFromAbstractParent extends AbstractParent { - use HasParent; use HasFactory; + use HasParent; } diff --git a/tests/Models/ClawHammer.php b/tests/Models/ClawHammer.php index 0e609f8..d8460af 100644 --- a/tests/Models/ClawHammer.php +++ b/tests/Models/ClawHammer.php @@ -7,6 +7,6 @@ class ClawHammer extends Tool { - use HasParent; use HasFactory; + use HasParent; } diff --git a/tests/Models/Conference.php b/tests/Models/Conference.php new file mode 100644 index 0000000..c7d3c42 --- /dev/null +++ b/tests/Models/Conference.php @@ -0,0 +1,12 @@ +value => ClawHammer::class, ToolNames::Mallet->value => Mallet::class, ToolNames::SledgeHammer->value => SledgeHammer::class, - ]; + ]; } } diff --git a/tests/Models/Vehicle.php b/tests/Models/Vehicle.php index ff005da..1dd9278 100644 --- a/tests/Models/Vehicle.php +++ b/tests/Models/Vehicle.php @@ -10,7 +10,7 @@ class Vehicle extends Model use HasChildren; protected $fillable = [ - 'type', 'driver_id' + 'type', 'driver_id', ]; protected $childTypes = [ diff --git a/tests/Models/Workshop.php b/tests/Models/Workshop.php new file mode 100644 index 0000000..c3d0c17 --- /dev/null +++ b/tests/Models/Workshop.php @@ -0,0 +1,12 @@ +runMigrations(); Factory::guessFactoryNamesUsing(static function (string $modelName) { - return sprintf("Database\\Factories\\%sFactory", class_basename($modelName)); + return sprintf('Database\\Factories\\%sFactory', class_basename($modelName)); }); } - protected function getEnvironmentSetUp($app) - { - // Setup default database to use sqlite :memory: - $app['config']->set('database.default', 'testbench'); - $app['config']->set('database.connections.testbench', [ - 'driver' => 'sqlite', - 'database' => ':memory:', - 'prefix' => '', - ]); - } - public function runMigrations() { Schema::create('drivers', function ($table) { @@ -110,5 +97,25 @@ public function runMigrations() $table->string('type')->nullable(); $table->timestamps(); }); + + Schema::create('events', function ($table) { + $table->increments('id'); + $table->string('type')->nullable(); + $table->string('name'); + $table->string('industry')->nullable(); + $table->string('skill_level')->nullable(); + $table->timestamps(); + }); + } + + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); } } diff --git a/tests/Unit/HasChildren/Bar.php b/tests/Unit/HasChildren/Bar.php new file mode 100644 index 0000000..755dc7e --- /dev/null +++ b/tests/Unit/HasChildren/Bar.php @@ -0,0 +1,8 @@ +mutatorWasCalled = true; + } +} diff --git a/tests/Unit/HasChildren/HasChildrenParentModel.php b/tests/Unit/HasChildren/HasChildrenParentModel.php new file mode 100644 index 0000000..4c6c398 --- /dev/null +++ b/tests/Unit/HasChildren/HasChildrenParentModel.php @@ -0,0 +1,13 @@ + Foo::class, + 'bar' => Bar::class, + ]; + } +} diff --git a/tests/Unit/HasChildrenTest.php b/tests/Unit/HasChildrenTest.php index 972ae25..0d03c8c 100644 --- a/tests/Unit/HasChildrenTest.php +++ b/tests/Unit/HasChildrenTest.php @@ -2,25 +2,28 @@ namespace Parental\Tests\Unit; -use Illuminate\Database\Eloquent\Model; -use Parental\HasChildren; use Parental\Tests\TestCase; +use Parental\Tests\Unit\HasChildren\Bar; +use Parental\Tests\Unit\HasChildren\Foo; +use Parental\Tests\Unit\HasChildren\HasChildrenChildModel; +use Parental\Tests\Unit\HasChildren\HasChildrenParentModel; +use Parental\Tests\Unit\HasChildren\HasChildrenParentModelWithMethodTypes; class HasChildrenTest extends TestCase { /** @test */ - function child_model_mutators_are_not_instigated() + public function child_model_mutators_are_not_instigated() { $model = (new HasChildrenParentModel)->newFromBuilder([ 'type' => HasChildrenChildModel::class, - 'test' => 'value' + 'test' => 'value', ]); $this->assertEquals($model->mutatorWasCalled, false); } /** @test */ - function child_model_types_can_be_set_via_method() + public function child_model_types_can_be_set_via_method() { $types = (new HasChildrenParentModelWithMethodTypes)->getChildTypes(); @@ -30,31 +33,3 @@ function child_model_types_can_be_set_via_method() ], $types); } } - -class HasChildrenParentModel extends Model { - use HasChildren; - - protected $fillable = ['type', 'test']; -} - -class HasChildrenChildModel extends HasChildrenParentModel { - public $mutatorWasCalled = false; - - public function setTestAttribute() - { - $this->mutatorWasCalled = true; - } -} - -class HasChildrenParentModelWithMethodTypes extends Model -{ - use HasChildren; - - public function getChildTypes() - { - return [ - 'foo' => Foo::class, - 'bar' => Bar::class, - ]; - } -} diff --git a/tests/Unit/HasParent/ChildModel.php b/tests/Unit/HasParent/ChildModel.php new file mode 100644 index 0000000..8e2c1fb --- /dev/null +++ b/tests/Unit/HasParent/ChildModel.php @@ -0,0 +1,10 @@ +assertEquals('parent_models', (new ParentModel)->getTable()); $this->assertEquals('parent_models', (new ChildModel)->getTable()); @@ -17,7 +19,7 @@ function child_model_has_table_name_of_parent_model() } /** @test */ - function child_model_has_same_foreign_key_as_parent() + public function child_model_has_same_foreign_key_as_parent() { $this->assertEquals('parent_model_id', (new ParentModel)->getForeignKey()); $this->assertEquals('parent_model_id', (new ChildModel)->getForeignKey()); @@ -25,7 +27,7 @@ function child_model_has_same_foreign_key_as_parent() } /** @test */ - function child_model_has_same_pivot_table_name_as_parent() + public function child_model_has_same_pivot_table_name_as_parent() { $related = new RelatedModel; @@ -34,19 +36,3 @@ function child_model_has_same_pivot_table_name_as_parent() $this->assertEquals('child_model_without_trait_related_model', (new ChildModelWithoutTrait)->joiningTable($related)); } } - -class ParentModel extends Model { - // -} - -class ChildModel extends ParentModel { - use HasParent; -} - -class ChildModelWithoutTrait extends ParentModel { - // -} - -class RelatedModel extends Model { - // -} diff --git a/tests/factories/CarFactory.php b/tests/factories/CarFactory.php index 4a4ce94..3ad8147 100644 --- a/tests/factories/CarFactory.php +++ b/tests/factories/CarFactory.php @@ -2,14 +2,13 @@ namespace Database\Factories; -use Parental\Tests\Models\Car; use Illuminate\Database\Eloquent\Factories\Factory; +use Parental\Tests\Models\Car; class CarFactory extends Factory { protected $model = Car::class; - public function definition() { return []; diff --git a/tests/factories/ChildFromAbstractParentFactory.php b/tests/factories/ChildFromAbstractParentFactory.php index 23523f5..baebd26 100644 --- a/tests/factories/ChildFromAbstractParentFactory.php +++ b/tests/factories/ChildFromAbstractParentFactory.php @@ -9,7 +9,6 @@ class ChildFromAbstractParentFactory extends Factory { protected $model = ChildFromAbstractParent::class; - public function definition() { return [];