Skip to content

Commit

Permalink
ECS-427 Added WithSetPkTrait [2]
Browse files Browse the repository at this point in the history
  • Loading branch information
valerialukinykh committed Oct 14, 2024
1 parent 6917d1d commit 7156177
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 10 additions & 1 deletion 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`, `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 `generatePk` call in `definition`.
Following is the example of a factory ('client_id' and 'location_id' are fields forming unique index):

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

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']);
Expand Down Expand Up @@ -174,6 +181,8 @@ 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.

## Parent classes

### BaseApiFactory
Expand Down
5 changes: 1 addition & 4 deletions src/WithSetPkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ protected function stateSetPk(mixed $state, array $keys, bool $isSequence = fals
$variables = $this->getVariables($state, $keys);

if ($this->hasValue) {
return $this->state(function () use ($variables) {
return $this->generatePk(...$variables);
})
->state($state);
return $this->setPk(...$variables)->state($state);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Stubs/TestObjectWithSetPkTraitFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public function definition(): array
]);
}

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']);
Expand Down
9 changes: 8 additions & 1 deletion tests/WithSetPkTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

test('TestObjectWithSetPkTraitFactory create', function () {
/** @var TestObjectModel $result */
$result = TestObjectWithSetPkTraitFactory::new()->make(['client_id' => 1]);
$result = TestObjectWithSetPkTraitFactory::new()->make(['amount' => 100]);

expect($result->amount)->toEqual(100);
});

test('TestObjectWithSetPkTraitFactory create with setPk', function () {
/** @var TestObjectModel $result */
$result = TestObjectWithSetPkTraitFactory::new()->setPk(1)->make();

expect($result->client_id)->toEqual(1);
});
Expand Down

0 comments on commit 7156177

Please sign in to comment.