Skip to content
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
2 changes: 1 addition & 1 deletion src/Factories/ImmutableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract public function definition(): array;
* @param array<string, mixed> $attributes
* @return TClass
*/
abstract protected function instance(array $attributes): mixed;
abstract protected function instance(array $attributes): object;

private function resolveValue(mixed $value): mixed
{
Expand Down
35 changes: 35 additions & 0 deletions src/Factories/InstanceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Craftzing\TestBench\Factories;

use ReflectionClass;
use ReflectionProperty;

/**
* @template TClass
*/
final readonly class InstanceFactory
{
public function __construct(
/** @var class-string<TClass> */
private string $classFQN,
) {}

/**
* @param array<string, mixed> $attributes
* @return TClass
*/
public function make(array $attributes): object
{
$instance = new ReflectionClass($this->classFQN)->newInstanceWithoutConstructor();

foreach ($attributes as $attribute => $value) {
new ReflectionProperty($this->classFQN, $attribute)->setValue($instance, $value);
}

return $instance;
}
}

78 changes: 78 additions & 0 deletions src/Factories/InstanceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Craftzing\TestBench\Factories;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionProperty;

final class InstanceFactoryTest extends TestCase
{
#[Test]
public function itFailsWhenConstructingInstancesWithPropertiesThatDoNotExist(): void
{
$subject = new readonly class('some-id')
{
public function __construct(
public string $id,
) {}
};

$this->expectException(ReflectionException::class);

new InstanceFactory($subject::class)->make(['nonsense' => true]);
}

#[Test]
public function itCanConstructInstances(): void
{
$attributes = [
'public' => 'Public',
'protected' => 'Protected',
'private' => 'Private',
];
$subject = new readonly class(...$attributes)
{
public function __construct(
public string $public,
protected string $protected,
private string $private,
) {}

public function protected(): string
{
return $this->protected;
}

public function private(): string
{
return $this->private;
}
};

$instance = new InstanceFactory($subject::class)->make($attributes);

$this->assertSame($attributes['public'], $instance->public);
$this->assertSame($attributes['protected'], $instance->protected());
$this->assertSame($attributes['private'], $instance->private());
}

#[Test]
public function itConstructsInstancesWithUninitializedPropertiesWhenNotProvidingAttributes(): void
{
$subject = new readonly class('some-id')
{
public function __construct(
public string $id,
) {}
};

$result = new InstanceFactory($subject::class)->make([]);

$this->assertInstanceOf($subject::class, $result);
$this->assertFalse(new ReflectionProperty($subject::class, 'id')->isInitialized($result));
}
}