Skip to content

Commit

Permalink
feat: skip readonly properties on entities when generating factories
Browse files Browse the repository at this point in the history
  • Loading branch information
KDederichs committed Jan 29, 2025
1 parent 7961d20 commit 268a9a9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
12 changes: 1 addition & 11 deletions src/Maker/Factory/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ final class FactoryGenerator
public const PHPSTAN_PATH = '/vendor/phpstan/phpstan/phpstan';
public const PSALM_PATH = '/vendor/vimeo/psalm/psalm';

/** @var string[]|true */
private array|bool $forceProperties = [];

/** @param \Traversable<int, DefaultPropertiesGuesser> $defaultPropertiesGuessers */
public function __construct(
private ?PersistenceManager $persistenceManager,
private KernelInterface $kernel,
private \Traversable $defaultPropertiesGuessers,
private FactoryClassMap $factoryClassMap,
private NamespaceGuesser $namespaceGuesser,
private bool $forceProperties = false
) {
}

Expand Down Expand Up @@ -165,12 +163,4 @@ private function staticAnalysisTool(): string
default => MakeFactoryData::STATIC_ANALYSIS_TOOL_NONE,
};
}

public function alwaysForce(string ...$properties): self
{
$clone = clone $this;
$clone->forceProperties = $properties ?: true;

return $clone;
}
}
10 changes: 1 addition & 9 deletions src/Maker/Factory/MakeFactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ final class MakeFactoryData
private array $defaultProperties = [];
/** @var list<MakeFactoryPHPDocMethod> */
private array $methodsInPHPDoc;
/** @var string[]|true */
private array|bool $forceProperties;

public function __construct(
private \ReflectionClass $object,
Expand All @@ -48,8 +46,7 @@ public function __construct(
private string $staticAnalysisTool,
private bool $persisted,
bool $withPhpDoc,
/** @var string[]|true $forceProperties */
array|bool $forceProperties
private bool $forceProperties
) {
$this->uses = [
$this->getFactoryClass(),
Expand All @@ -69,7 +66,6 @@ public function __construct(
}

$this->methodsInPHPDoc = $withPhpDoc ? MakeFactoryPHPDocMethod::createAll($this) : [];
$this->forceProperties = $forceProperties;
}

// @phpstan-ignore-next-line
Expand Down Expand Up @@ -169,10 +165,6 @@ public function getDefaultProperties(): array
return true;
}

if (is_array($this->forceProperties) && \in_array($propertyName, $this->forceProperties, true)) {
return true;
}

return self::propertyInfo()->isWritable($clazz, $propertyName) || self::propertyInfo()->isInitializable($clazz, $propertyName);
}, ARRAY_FILTER_USE_KEY);

Expand Down
6 changes: 3 additions & 3 deletions src/ZenstruckFoundryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ public function loadExtension(array $config, ContainerConfigurator $configurator
if (!isset($bundles['DoctrineBundle']) && !isset($bundles['DoctrineMongoDBBundle'])) {
$container->removeDefinition('.zenstruck_foundry.maker.factory.doctrine_scalar_fields_default_properties_guesser');
}

$container->getDefinition('.zenstruck_foundry.maker.factory.generator')
->setArgument('$forceProperties', $config['instantiator']['always_force_properties'] ?? false);
} else {
$configurator->import('../config/command_stubs.php');
}
Expand Down Expand Up @@ -341,9 +344,6 @@ private function configureInstantiator(array $config, ContainerBuilder $containe
$container->getDefinition('.zenstruck_foundry.instantiator')
->addMethodCall('alwaysForce', returnsClone: true)
;
$container->getDefinition('.zenstruck_foundry.maker.factory.generator')
->addMethodCall('alwaysForce', returnsClone: true)
;
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Fixture/FoundryTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ abstract class FoundryTestKernel extends Kernel
{
use MicroKernelTrait;

public function __construct()
{
parent::__construct('test', true);

}

public function registerBundles(): iterable
{
yield new FrameworkBundle();
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixture/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
{
parent::configureContainer($c, $loader);

if ($this->getEnvironment() !== 'test') {
$loader->load(\sprintf('%s/config/%s.yaml', __DIR__, $this->getEnvironment()));
}

$c->loadFromExtension('zenstruck_foundry', [
'orm' => [
'reset' => [
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixture/config/always_force.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zenstruck_foundry:
instantiator:
always_force_properties: true # always "force set" properties
2 changes: 2 additions & 0 deletions tests/Integration/Maker/MakeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ public function does_not_initialize_non_settable(): void
*/
public function does_force_initialization_of_non_settable_with_always_force(): void
{
self::bootKernel(['environment' => 'always_force']);

$tester = $this->makeFactoryCommandTester();

$tester->execute(['class' => ObjectWithNonWriteable::class, '--no-persistence' => true]);
Expand Down

0 comments on commit 268a9a9

Please sign in to comment.