Skip to content

Commit

Permalink
model relations tools added
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrFedak committed Jun 7, 2024
1 parent 8fcb3b9 commit c92894b
Showing 1 changed file with 114 additions and 5 deletions.
119 changes: 114 additions & 5 deletions src/Features/Traits/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\";
Expand Down

0 comments on commit c92894b

Please sign in to comment.