Skip to content

Commit

Permalink
fix: attributes added in are not passed to
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jun 26, 2024
1 parent c2391f8 commit 093330a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
34 changes: 23 additions & 11 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ abstract class Factory
/** @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()
{
}

public function __clone(): void
{
$this->normalizedParameters = null;
}

/**
* @param Attributes $attributes
Expand Down Expand Up @@ -144,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 @@ -170,16 +192,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 +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 @@ -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());
}
}

0 comments on commit 093330a

Please sign in to comment.