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

Attributes added in beforeInstantiate are not passed to afterPersist #634

Draft
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Draft
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
31 changes: 19 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ abstract class Factory
/** @var Attributes[] */
private array $attributes;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikophil, don't we need to set this back to null on clone?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to test this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so... an easy test like this one (that I added to our test suite) was failing because of the memoization:

    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());
    }

I'm really not proud of the solution I found (replace $this->normalizedParameters ??= by $this->normalizedParameters = in Factory::normalizeParameters(), but it's the only simple one I found 🤷


// keep an empty constructor for BC
public function __construct()
{
}


/**
* @param Attributes $attributes
*/
Expand Down Expand Up @@ -144,6 +151,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 @@ -170,16 +187,6 @@ final protected function normalizeAttributes(array|callable $attributes = []): a
);
}

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

/**
* @internal
*
Expand All @@ -189,7 +196,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 @@ -226,7 +226,7 @@ final 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 @@ -280,6 +280,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