From c92894b17a7ca18617c4efc7f33eb912fbe94954 Mon Sep 17 00:00:00 2001 From: PiotrFedak Date: Fri, 7 Jun 2024 20:18:53 +0200 Subject: [PATCH] model relations tools added --- src/Features/Traits/Eloquent.php | 119 +++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 5 deletions(-) diff --git a/src/Features/Traits/Eloquent.php b/src/Features/Traits/Eloquent.php index 73c13d4..486b1d4 100644 --- a/src/Features/Traits/Eloquent.php +++ b/src/Features/Traits/Eloquent.php @@ -10,17 +10,126 @@ trait Eloquent { /** - * @Given there is :count :model object in database - * @Given there should be :count :model object in database - * @Given there are :count :model objects in database - * @Given there should be :count :model objects in database + * @Given there is a model :model in the database + * @Given there is a :model in the database */ - public function thereAreModelsInDatabase(string $model, int $count): void + public function thereIsAModelInTheDatabase(string $model): void { $modelClass = $this->recognizeModelClass($model); + if (!$modelClass::query()->exists()) { + $modelClass::factory()->create(); + } + Assert::assertTrue($modelClass::query()->exists(), "The model $model does not exist in the database."); + } + + /** + * @Given there are :count :model objects in the database + * @Given there are :count :model in the database + */ + public function thereAreModelsInTheDatabase(string $model, int $count): void + { + $modelClass = $this->recognizeModelClass($model); + $existingCount = $modelClass::query()->count(); + + if ($existingCount < $count) { + $modelClass::factory()->count($count - $existingCount)->create(); + } + Assert::assertEquals($count, $modelClass::query()->count()); } + /** + * @Then the model :model1 has many :model2 + */ + public function theModelHasMany(string $model1, string $model2): void + { + $model1Class = $this->recognizeModelClass($model1); + $relation = Str::plural($model2); + $instance = $model1Class::first() ?: $model1Class::factory()->create(); + + Assert::assertTrue( + method_exists($instance, $relation), + "The model $model1 does not have a $relation relation method.", + ); + + $related = $instance->{$relation}(); + + Assert::assertInstanceOf( + 'Illuminate\Database\Eloquent\Relations\HasMany', + $related, + "The relation $relation is not of type HasMany.", + ); + } + + /** + * @Then the model :model1 belongs to :model2 + */ + public function theModelBelongsTo(string $model1, string $model2): void + { + $model1Class = $this->recognizeModelClass($model1); + $relation = Str::singular($model2); + $instance = $model1Class::first() ?: $model1Class::factory()->create(); + + Assert::assertTrue( + method_exists($instance, $relation), + "The model $model1 does not have a $relation relation method.", + ); + + $related = $instance->{$relation}(); + + Assert::assertInstanceOf( + 'Illuminate\Database\Eloquent\Relations\BelongsTo', + $related, + "The relation $relation is not of type BelongsTo.", + ); + } + + /** + * @Then the model :model1 has one :model2 + */ + public function theModelHasOne(string $model1, string $model2): void + { + $model1Class = $this->recognizeModelClass($model1); + $relation = Str::singular($model2); + $instance = $model1Class::first() ?: $model1Class::factory()->create(); + + Assert::assertTrue( + method_exists($instance, $relation), + "The model $model1 does not have a $relation relation method.", + ); + + $related = $instance->{$relation}(); + + Assert::assertInstanceOf( + 'Illuminate\Database\Eloquent\Relations\HasOne', + $related, + "The relation $relation is not of type HasOne.", + ); + } + + /** + * @Then the model :model1 belongs to many :model2 + */ + public function theModelBelongsToMany(string $model1, string $model2): void + { + $model1Class = $this->recognizeModelClass($model1); + $relation = Str::plural($model2); + $instance = $model1Class::first() ?: $model1Class::factory()->create(); + + Assert::assertTrue( + method_exists($instance, $relation), + "The model $model1 does not have a $relation relation method.", + ); + + $related = $instance->{$relation}(); + + Assert::assertInstanceOf( + 'Illuminate\Database\Eloquent\Relations\BelongsToMany', + $related, + "The relation $relation is not of type BelongsToMany.", + ); + } + protected function getModelNamespace(): string { return "App\\Models\\";