From ea309cd6cfa37047773f2a77a00e730c7f63a692 Mon Sep 17 00:00:00 2001 From: Markus Poerschke Date: Mon, 7 Aug 2023 22:18:45 +0200 Subject: [PATCH] fix: additional content presets for PHP enums are not applied (#1459) --- src/generators/php/renderers/EnumRenderer.ts | 5 +++- test/generators/php/PhpGenerator.spec.ts | 26 +++++++++++++++++++ .../__snapshots__/PhpGenerator.spec.ts.snap | 12 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/generators/php/renderers/EnumRenderer.ts b/src/generators/php/renderers/EnumRenderer.ts index 46ddf614c2..09a477095e 100644 --- a/src/generators/php/renderers/EnumRenderer.ts +++ b/src/generators/php/renderers/EnumRenderer.ts @@ -13,7 +13,10 @@ import { PhpOptions } from '../PhpGenerator'; */ export class EnumRenderer extends PhpRenderer { async defaultSelf(): Promise { - const content = [await this.renderItems()]; + const content = [ + await this.renderItems(), + await this.runAdditionalContentPreset() + ]; return `enum ${this.model.name} { ${this.indent(this.renderBlock(content, 2))} diff --git a/test/generators/php/PhpGenerator.spec.ts b/test/generators/php/PhpGenerator.spec.ts index abb9cbdecf..f56d257739 100644 --- a/test/generators/php/PhpGenerator.spec.ts +++ b/test/generators/php/PhpGenerator.spec.ts @@ -50,6 +50,32 @@ describe('PhpGenerator', () => { expect(models).toHaveLength(1); expect(models[0].result).toMatchSnapshot(); }); + + test('should render `enum` with presets applied', async () => { + const doc = { + $id: 'Things', + enum: ['A', 'B', 'C'] + }; + + generator = new PhpGenerator({ + presets: [ + { + enum: { + self({ content }) { + return `${content}// self content`; + }, + additionalContent({ content }) { + return `${content}// additional content`; + } + } + } + ] + }); + + const models = await generator.generate(doc); + expect(models).toHaveLength(1); + expect(models[0].result).toMatchSnapshot(); + }); }); describe('Class', () => { test('should not render reserved keyword', async () => { diff --git a/test/generators/php/__snapshots__/PhpGenerator.spec.ts.snap b/test/generators/php/__snapshots__/PhpGenerator.spec.ts.snap index e455dee399..f4b876fa85 100644 --- a/test/generators/php/__snapshots__/PhpGenerator.spec.ts.snap +++ b/test/generators/php/__snapshots__/PhpGenerator.spec.ts.snap @@ -132,6 +132,18 @@ exports[`PhpGenerator Enum should render \`enum\` with mixed types (union type) " `; +exports[`PhpGenerator Enum should render \`enum\` with presets applied 1`] = ` +"enum Things +{ + case A; + case B; + case C; + + // additional content +} +// self content" +`; + exports[`PhpGenerator Enum should render enums with translated special characters 1`] = ` "enum States {