-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skip readonly properties on entities when generating factories (#…
…798) Co-authored-by: Nicolas PHILIPPE <[email protected]>
- Loading branch information
1 parent
636cedd
commit 9032c38
Showing
11 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Fixture/Maker/expected/does_force_initialization_of_non_settable_with_always_force.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Factory; | ||
|
||
use Zenstruck\Foundry\ObjectFactory; | ||
use Zenstruck\Foundry\Tests\Fixture\ObjectWithNonWriteable; | ||
|
||
/** | ||
* @extends ObjectFactory<ObjectWithNonWriteable> | ||
*/ | ||
final class ObjectWithNonWriteableFactory extends ObjectFactory | ||
{ | ||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | ||
* | ||
* @todo inject services if required | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function class(): string | ||
{ | ||
return ObjectWithNonWriteable::class; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | ||
* | ||
* @todo add your default values here | ||
*/ | ||
protected function defaults(): array|callable | ||
{ | ||
return [ | ||
'bar' => self::faker()->sentence(), | ||
'baz' => self::faker()->sentence(), | ||
'foo' => self::faker()->sentence(), | ||
]; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | ||
*/ | ||
protected function initialize(): static | ||
{ | ||
return $this | ||
// ->afterInstantiate(function(ObjectWithNonWriteable $objectWithNonWriteable): void {}) | ||
; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/Fixture/Maker/expected/does_not_initialize_non_settable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Factory; | ||
|
||
use Zenstruck\Foundry\ObjectFactory; | ||
use Zenstruck\Foundry\Tests\Fixture\ObjectWithNonWriteable; | ||
|
||
/** | ||
* @extends ObjectFactory<ObjectWithNonWriteable> | ||
*/ | ||
final class ObjectWithNonWriteableFactory extends ObjectFactory | ||
{ | ||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | ||
* | ||
* @todo inject services if required | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function class(): string | ||
{ | ||
return ObjectWithNonWriteable::class; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | ||
* | ||
* @todo add your default values here | ||
*/ | ||
protected function defaults(): array|callable | ||
{ | ||
return [ | ||
'baz' => self::faker()->sentence(), | ||
'foo' => self::faker()->sentence(), | ||
]; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | ||
*/ | ||
protected function initialize(): static | ||
{ | ||
return $this | ||
// ->afterInstantiate(function(ObjectWithNonWriteable $objectWithNonWriteable): void {}) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixture; | ||
|
||
final class ObjectWithNonWriteable | ||
{ | ||
private string $bar; | ||
// @phpstan-ignore-next-line We do not want assign a default value to this so the factory sets one | ||
private string $baz; | ||
|
||
public function __construct( | ||
public readonly string $foo | ||
) { | ||
$this->bar = 'bar'; | ||
} | ||
|
||
public function getBaz(): string | ||
{ | ||
return $this->baz; | ||
} | ||
|
||
public function setBaz(string $baz): ObjectWithNonWriteable | ||
{ | ||
$this->baz = $baz; | ||
return $this; | ||
} | ||
|
||
public function getBar(): string | ||
{ | ||
return $this->bar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
use Zenstruck\Foundry\Tests\Fixture\Entity\WithEmbeddableEntity; | ||
use Zenstruck\Foundry\Tests\Fixture\Object1; | ||
use Zenstruck\Foundry\Tests\Fixture\ObjectWithEnum; | ||
use Zenstruck\Foundry\Tests\Fixture\ObjectWithNonWriteable; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -421,14 +422,40 @@ public function can_create_factory_with_default_enum(): void | |
$this->assertFileFromMakerSameAsExpectedFile(self::tempFile('src/Factory/ObjectWithEnumFactory.php')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function does_not_initialize_non_settable(): void | ||
{ | ||
$tester = $this->makeFactoryCommandTester(); | ||
|
||
$tester->execute(['class' => ObjectWithNonWriteable::class, '--no-persistence' => true]); | ||
|
||
$this->assertFileFromMakerSameAsExpectedFile(self::tempFile('src/Factory/ObjectWithNonWriteableFactory.php')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function does_force_initialization_of_non_settable_with_always_force(): void | ||
{ | ||
$tester = $this->makeFactoryCommandTester('always_force'); | ||
|
||
$tester->execute(['class' => ObjectWithNonWriteable::class, '--no-persistence' => true]); | ||
|
||
$this->assertFileFromMakerSameAsExpectedFile(self::tempFile('src/Factory/ObjectWithNonWriteableFactory.php')); | ||
} | ||
|
||
private function emulateSCAToolEnabled(string $scaToolFilePath): void | ||
{ | ||
\mkdir(\dirname($scaToolFilePath), 0777, true); | ||
\touch($scaToolFilePath); | ||
} | ||
|
||
private function makeFactoryCommandTester(): CommandTester | ||
private function makeFactoryCommandTester(string $appEnv = 'test'): CommandTester | ||
{ | ||
return new CommandTester((new Application(self::bootKernel()))->find('make:factory')); | ||
return new CommandTester((new Application(self::bootKernel([ | ||
'environment' => $appEnv, | ||
])))->find('make:factory')); | ||
} | ||
} |