diff --git a/CHANGELOG.md b/CHANGELOG.md index 76bc5a78..60351e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ * A fluent interface isn't generated anymore by default unless you set the `fluentMutatorMethods` config flag to `true` * Useless PHPDoc (`@param` and `@return` annotations when a typehint exist and mutator description) isn't generated anymore * `DateTimeInterface` is used instead of `DateTime` when possible -* Add the ability to not generate mutator methods (useful when generating public properties) +* Add the ability to not generate accessor methods (useful when generating public properties) * The `extract-cardinalities` gains two new options: one to configure the Schema.org's file to use, the other for the GoodRelations one * The annotation generator for API Platform v1 has been dropped. Only API Platform v2 is now supported. * Parent class is not generated by default anymore (using inheritance for entity is discouraged) diff --git a/src/TypesGeneratorConfiguration.php b/src/TypesGeneratorConfiguration.php index b38b8591..0b8a056c 100644 --- a/src/TypesGeneratorConfiguration.php +++ b/src/TypesGeneratorConfiguration.php @@ -100,7 +100,7 @@ function ($rdfa) { ->end() ->scalarNode('author')->defaultFalse()->info('The value of the phpDoc\'s @author annotation')->example('Kévin Dunglas ')->end() ->enumNode('fieldVisibility')->values(['private', 'protected', 'public'])->defaultValue('private')->cannotBeEmpty()->info('Visibility of entities fields')->end() - ->booleanNode('mutatorMethods')->defaultTrue()->info('Set this flag to false to not generate getter, setter, adder and remover methods')->end() + ->booleanNode('accessorMethods')->defaultTrue()->info('Set this flag to false to not generate getter, setter, adder and remover methods')->end() ->booleanNode('fluentMutatorMethods')->defaultFalse()->info('Set this flag to true to generate fluent setter, adder and remover methods')->end() ->arrayNode('types') ->beforeNormalization() diff --git a/templates/class.php.twig b/templates/class.php.twig index a1af9e01..ff4705a5 100644 --- a/templates/class.php.twig +++ b/templates/class.php.twig @@ -59,7 +59,7 @@ use {{ use }}; } {% endif %} -{% if config.mutatorMethods %} +{% if config.accessorMethods %} {% for field in class.fields %} {% if field.isArray %} /** diff --git a/tests/Command/DumpConfigurationTest.php b/tests/Command/DumpConfigurationTest.php index e52b4ae7..41b208ce 100644 --- a/tests/Command/DumpConfigurationTest.php +++ b/tests/Command/DumpConfigurationTest.php @@ -96,7 +96,7 @@ interface: AppBundle\Model # Example: Acme\Model fieldVisibility: private # One of "private"; "protected"; "public" # Set this flag to false to not generate getter, setter, adder and remover methods - mutatorMethods: true + accessorMethods: true # Set this flag to true to generate fluent setter, adder and remover methods fluentMutatorMethods: false diff --git a/tests/Command/GenerateTypesCommandTest.php b/tests/Command/GenerateTypesCommandTest.php index 1ef23453..f802b41f 100644 --- a/tests/Command/GenerateTypesCommandTest.php +++ b/tests/Command/GenerateTypesCommandTest.php @@ -51,7 +51,6 @@ public function getArguments() [__DIR__.'/../../build/blog/', __DIR__.'/../config/blog.yaml'], [__DIR__.'/../../build/ecommerce/', __DIR__.'/../config/ecommerce.yaml'], [__DIR__.'/../../build/vgo/', __DIR__.'/../config/vgo.yaml'], - [__DIR__.'/../../build/public-properties/', __DIR__.'/../config/public-properties.yaml'], [__DIR__.'/../../build/mongodb/address-book/', __DIR__.'/../config/mongodb/address-book.yaml'], [__DIR__.'/../../build/mongodb/ecommerce/', __DIR__.'/../config/mongodb/ecommerce.yaml'], ]; @@ -61,14 +60,10 @@ public function testFluentMutators() { $outputDir = __DIR__.'/../../build/fluent-mutators'; $config = __DIR__.'/../config/fluent-mutators.yaml'; - $this->fs->mkdir($outputDir); - $commandTester = new CommandTester(new GenerateTypesCommand()); $this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config])); - $organization = file_get_contents($outputDir.'/AppBundle/Entity/Person.php'); - $this->assertContains(<<<'PHP' public function setUrl(?string $url): self { @@ -77,8 +72,7 @@ public function setUrl(?string $url): self return $this; } PHP - , $organization); - + , $organization); $this->assertContains(<<<'PHP' public function addFriends(Person $friends): self { @@ -96,4 +90,21 @@ public function removeFriends(Person $friends): self PHP , $organization); } + + public function testDoNotGenerateAccessorMethods() + { + $outputDir = __DIR__.'/../../build/public-properties'; + $config = __DIR__.'/../config/public-properties.yaml'; + + $this->fs->mkdir($outputDir); + + $commandTester = new CommandTester(new GenerateTypesCommand()); + $this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config])); + + $organization = file_get_contents($outputDir.'/AppBundle/Entity/Person.php'); + $this->assertNotContains('function get', $organization); + $this->assertNotContains('function set', $organization); + $this->assertNotContains('function add', $organization); + $this->assertNotContains('function remove', $organization); + } } diff --git a/tests/config/public-properties.yaml b/tests/config/public-properties.yaml index 91b822ff..15bac744 100644 --- a/tests/config/public-properties.yaml +++ b/tests/config/public-properties.yaml @@ -2,7 +2,7 @@ rdfa: [{ uri: tests/data/schema.rdfa, format: rdfa }] relations: [tests/data/v1.owl] fieldVisibility: public -mutatorMethods: false +accessorMethods: false types: Person: properties: