Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass to afterPersist hook the attributes from beforeInstantiate #745

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ abstract class Factory
/** @phpstan-var Attributes[] */
private array $attributes;

/**
* Memoization of normalized parameters
*
* @internal
* @var Parameters|null
*/
protected array|null $normalizedParameters = null;

// keep an empty constructor for BC
public function __construct()
{
Expand Down Expand Up @@ -148,6 +156,16 @@ final protected static function faker(): Faker\Generator
return Configuration::instance()->faker;
}

/**
* Override to adjust default attributes & config.
*
* @return static
*/
protected function initialize(): static
{
return $this;
}

/**
* @internal
*
Expand All @@ -174,14 +192,6 @@ final protected function normalizeAttributes(array|callable $attributes = []): a
);
}

/**
* Override to adjust default attributes & config.
*/
protected function initialize(): static
{
return $this;
}

/**
* @internal
*
Expand All @@ -191,7 +201,7 @@ protected function initialize(): static
*/
protected function normalizeParameters(array $parameters): array
{
return array_combine(
return $this->normalizedParameters = array_combine(
array_keys($parameters),
\array_map($this->normalizeParameter(...), array_keys($parameters), $parameters)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function create(callable|array $attributes = []): object
$this->tempAfterPersist = [];

if ($this->afterPersist) {
$attributes = $this->normalizeAttributes($attributes);
$attributes = $this->normalizedParameters ?? throw new \LogicException('Factory::$normalizedParameters has not been initialized.');

foreach ($this->afterPersist as $callback) {
$callback($object, $attributes);
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/ObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ public function can_create_non_service_factories(): void

$this->assertSame('router-constructor', $object->object->getProp1());
}

/**
* @test
*/
public function can_create_different_objects_based_on_same_factory(): void
{
$factory = Object1Factory::new(['prop1' => 'first object']);
$object1 = $factory->create();
self::assertSame('first object-constructor', $object1->getProp1());

$object2 = $factory->create(['prop1' => 'second object']);
self::assertSame('second object-constructor', $object2->getProp1());

$object3 = $factory->with(['prop1' => 'third object'])->create();
self::assertSame('third object-constructor', $object3->getProp1());
}
}
21 changes: 21 additions & 0 deletions tests/Integration/Persistence/GenericProxyFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ public function can_use_after_persist_with_attributes(): void
$this->assertSame($value, $object->getProp1());
}

/**
* @test
*/
public function can_use_after_persist_with_attributes_added_in_before_instantiate(): void
{
$value = 'value set with before instantiate';
$object = $this->factory()
->instantiateWith(Instantiator::withConstructor()->allowExtra('extra'))
->beforeInstantiate(function (array $attributes) use ($value) {
$attributes['extra'] = $value;

return $attributes;
})
->afterPersist(function (GenericModel $object, array $attributes) {
$object->setProp1($attributes['extra']);
})
->create();

$this->assertSame($value, $object->getProp1());
}

/**
* @return PersistentProxyObjectFactory<GenericModel>
*/
Expand Down
Loading