Skip to content

Commit

Permalink
ECS-427 Moved overriden factory methods into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
valerialukinykh committed Oct 17, 2024
1 parent 6c07bf6 commit 41ab6d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ $this->faker->modelId() // return unsigned bit integer value

If your model has unique index consisting of multiple fields, WithSetPkTrait trait should be used to ensure generated values for these fields are unique.

In order for trait to work, you have to define methods `state`, `setPk`, `sequence` and `generatePk` and include `getPk` call in `definition`.
In order for trait to work, you have to define methods `getPkFields`, `setPk` and `generatePk` and include `getPk` call in `definition`.
Following is the example of a factory ('client_id' and 'location_id' are fields forming unique index):

```php
Expand All @@ -147,23 +147,18 @@ Following is the example of a factory ('client_id' and 'location_id' are fields
]);
}

public function getPkFields(): array
{
return ['client_id', 'location_id'];
}

public function setPk(?int $clientId = null, ?string $locationId = null): self // Use in tests to define values
{
return $this->state(function () use ($clientId, $locationId) {
return $this->generatePk($clientId, $locationId);
});
}

public function state(mixed $state): static // Override of Laravel Eloquent Factory method
{
return $this->stateSetPk($state, ['client_id', 'location_id']);
}

public function sequence(...$sequence): static // Override of Laravel Eloquent Factory method
{
return $this->stateSetPk($sequence, ['client_id', 'location_id'], true);
}

protected function generatePk(?int $clientId = null, ?string $locationId = null): array
{
$clientIdFormat = $clientId ?: '\d{10}';
Expand All @@ -181,7 +176,7 @@ Following is the example of a factory ('client_id' and 'location_id' are fields
}
```

*Important note* - fields must be declared in the same order in all methods.
*Important note* - fields must be declared in the same order in `getPkFields` and `setPk` methods.

## Parent classes

Expand Down
22 changes: 22 additions & 0 deletions src/WithSetPkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ trait WithSetPkTrait
protected bool $hasValue = false;
protected array $pkValues = [];

/**
* Returns array of fields that form unique index
* @return string[]
*/
abstract public function getPkFields(): array;

/** Adds a state transformation to a model using unique generated values */
abstract public function setPk(): self;

/** Generates unique values and returns them in format compatible with definition() */
abstract protected function generatePk(): array;

public function state(mixed $state): static
{
return $this->stateSetPk($state, $this->getPkFields());
}

public function sequence(...$sequence): static
{
return $this->stateSetPk($sequence, $this->getPkFields(), true);
}

protected function stateSetPk(mixed $state, array $keys, bool $isSequence = false): static
{
if ($isSequence) {
Expand Down
15 changes: 5 additions & 10 deletions tests/Stubs/TestObjectWithSetPkTraitFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@ public function definition(): array
]);
}

public function getPkFields(): array
{
return ['client_id', 'location_id'];
}

public function setPk(?int $clientId = null, ?string $locationId = null): self
{
return $this->state(function () use ($clientId, $locationId) {
return $this->generatePk($clientId, $locationId);
});
}

public function state(mixed $state): static
{
return $this->stateSetPk($state, ['client_id', 'location_id']);
}

public function sequence(...$sequence): static
{
return $this->stateSetPk($sequence, ['client_id', 'location_id'], true);
}

protected function generatePk(?int $clientId = null, ?string $locationId = null): array
{
$clientIdFormat = $clientId ?: '\d{10}';
Expand Down

0 comments on commit 41ab6d8

Please sign in to comment.