Skip to content

Commit

Permalink
ECS-427 Fixed definition
Browse files Browse the repository at this point in the history
  • Loading branch information
valerialukinykh committed Oct 15, 2024
1 parent af7e9fa commit 6c07bf6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
6 changes: 2 additions & 4 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 `generatePk` call in `definition`.
In order for trait to work, you have to define methods `state`, `setPk`, `sequence` 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 @@ -142,9 +142,7 @@ Following is the example of a factory ('client_id' and 'location_id' are fields

public function definition(): array
{
$pk = $this->skipGeneratePk() ? [] : $this->generatePk();

return array_merge($pk, [
return array_merge($this->getPk(), [
'amount' => $this->faker->numberBetween(1, 1_000_000),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/WithSetPkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected function getPkSequenceStates(): Collection
return $this->states->filter(fn ($state) => $state::class === SequenceWithSetPk::class && count($state->pkVariables) > 0);
}

public function skipGeneratePk(): bool
public function getPk(): array
{
return $this->getPkSequenceStates()->isNotEmpty();
return $this->getPkSequenceStates()->isNotEmpty() ? [] : $this->generatePk();
}
}
4 changes: 1 addition & 3 deletions tests/Stubs/TestObjectWithSetPkTraitFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class TestObjectWithSetPkTraitFactory extends BaseModelFactory

public function definition(): array
{
$pk = $this->skipGeneratePk() ? [] : $this->generatePk();

return array_merge($pk, [
return array_merge($this->getPk(), [
'amount' => $this->faker->numberBetween(1, 1_000_000),
]);
}
Expand Down

0 comments on commit 6c07bf6

Please sign in to comment.