Skip to content

Commit

Permalink
#38 - Extend eloquent tools (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrFedak authored Jun 28, 2024
1 parent 4005313 commit 18d2381
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 14 deletions.
148 changes: 134 additions & 14 deletions src/Features/Traits/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,156 @@

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Blumilk\BLT\Helpers\RecognizeClassHelper;
use Blumilk\BLT\LaravelRelations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use PHPUnit\Framework\Assert;

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 seedModelInTheDatabase(string $model, ?TableNode $table = null): void
{
$modelClass = $this->recognizeModelClass($model);
Assert::assertEquals($count, $modelClass::query()->count());
$modelClass = RecognizeClassHelper::recognizeObjectClass($model);
$attributes = $table ? $table->getRowsHash() : [];

if (method_exists($modelClass, "factory")) {
$modelClass::factory()->create($attributes);
} else {
$modelClass::create($attributes);
}
}

protected function getModelNamespace(): string
/**
* @Then the model :model exists in the database
*/
public function assertModelExistsInTheDatabase(string $model): void
{
return "App\\Models\\";
$modelClass = RecognizeClassHelper::recognizeObjectClass($model);

Assert::assertTrue($modelClass::query()->exists(), "The model $model does not exist in the database.");
}

protected function recognizeModelClass(string $model): string
/**
* @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
{
if (str_contains($model, "\\")) {
return $model;
$modelClass = RecognizeClassHelper::recognizeObjectClass($model);
$existingCount = $modelClass::query()->count();

if ($existingCount < $count) {
$modelClass::factory()->count($count - $existingCount)->create();
}

$model = Str::ucfirst(Str::singular($model));
Assert::assertEquals($count, $modelClass::query()->count());
}

/**
* @Then the model :model1 has many :model2
*/
public function theModelHasMany(string $model1, string $model2): void
{
$model1Class = RecognizeClassHelper::recognizeObjectClass($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.",
);

$this->assertRelation($instance, $relation, LaravelRelations::HAS_MANY);
}

/**
* @Then the model :model1 belongs to :model2
*/
public function theModelBelongsTo(string $model1, string $model2): void
{
$model1Class = RecognizeClassHelper::recognizeObjectClass($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.",
);

$this->assertRelation($instance, $relation, LaravelRelations::BELONGS_TO);
}

/**
* @Then the model :model1 has one :model2
*/
public function theModelHasOne(string $model1, string $model2): void
{
$model1Class = RecognizeClassHelper::recognizeObjectClass($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.",
);

$this->assertRelation($instance, $relation, LaravelRelations::HAS_ONE);
}

/**
* @Then the model :model1 belongs to many :model2
*/
public function theModelBelongsToMany(string $model1, string $model2): void
{
$model1Class = RecognizeClassHelper::recognizeObjectClass($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.",
);

$this->assertRelation($instance, $relation, LaravelRelations::BELONGS_TO_MANY);
}

/**
* @Then the model :model1 has :count related :model2
*/
public function theModelHasExpectedNumberOfRelated(string $model1, string $model2, int $count): void
{
$model1Class = RecognizeClassHelper::recognizeObjectClass($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.",
);

$relatedCount = $instance->{$relation}()->count();

Assert::assertEquals(
$count,
$relatedCount,
"The model $model1 does not have $count related $relation.",
);
}

protected function assertRelation(Model $instance, string $relation, string $relationType): void
{
$related = $instance->{$relation}();

return $this->getModelNamespace() . $model;
Assert::assertInstanceOf(
$relationType,
$related,
"The relation $relation is not of type $relationType.",
);
}
}
13 changes: 13 additions & 0 deletions src/LaravelRelations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT;

class LaravelRelations
{
public const HAS_MANY = 'Illuminate\Database\Eloquent\Relations\HasMany';
public const BELONGS_TO = 'Illuminate\Database\Eloquent\Relations\BelongsTo';
public const HAS_ONE = 'Illuminate\Database\Eloquent\Relations\HasOne';
public const BELONGS_TO_MANY = 'Illuminate\Database\Eloquent\Relations\BelongsToMany';
}

0 comments on commit 18d2381

Please sign in to comment.