Skip to content

Commit 22d2cec

Browse files
ajoh504mikaelcom
andauthored
issue #324 - fix how composer settings are created in the GeneratorOptions class (#336)
* add private boolean field 'calledSetComposerSettings' - Used internally by the class to determine if the setComposerSettings method has been called already - When set to true, this property ensures 'setComposerSettings' is not called infinitely * add a new condition to the 'setOptionValue' method - the condition checks if the input contains composer settings, then calls 'setComposerSettings' if so - updating 'calledSetComposerSettings' to true before calling 'setComposerSettings' is required to prevent an infinite call chain - this fixes issue #324 where the composer settings are incorrectly written to the composer.json file * add two tests and four assertions for GeneratorOptions::setOptionsValue - the goal is to test writing composer settings both from the command line and from a PHP array - this is to ensure that the caller can successfully create composer settings from both the command line and from a WsdlToPHP project script * add a test assertion to check for valid JSON - this test also adds additional composer settings via `setOptionComposerSettings` * update composer file tests to use new resource file * update ComposerTest and Bing resource file - Order the key/value pairs to appropriately match the expected values and actual values * implement changes from code review in pr #336 * fix whitespace issue in testing resource file Co-authored-by: Mikaël DELSOL <contact@mikael-delsol.fr> --------- Co-authored-by: Mikaël DELSOL <contact@mikael-delsol.fr>
1 parent 4f65873 commit 22d2cec

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

src/ConfigurationReader/GeneratorOptions.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function getOptionValue(string $optionName)
162162
return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default'];
163163
}
164164

165-
public function setOptionValue(string $optionName, $optionValue, array $values = []): self
165+
public function setOptionValue(string $optionName, $optionValue, array $values = [], bool $callCustomMethod = true): self
166166
{
167167
if (!array_key_exists($optionName, $this->options)) {
168168
$this->options[$optionName] = [
@@ -171,6 +171,8 @@ public function setOptionValue(string $optionName, $optionValue, array $values =
171171
];
172172
} elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) {
173173
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
174+
} elseif (self::COMPOSER_SETTINGS === $optionName && is_array($optionValue) && $callCustomMethod) {
175+
$this->setComposerSettings($optionValue);
174176
} else {
175177
$this->options[$optionName]['value'] = $optionValue;
176178
}
@@ -232,7 +234,7 @@ public function setComposerSettings(array $composerSettings = []): self
232234
}
233235
}
234236

235-
return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings);
237+
return $this->setOptionValue(self::COMPOSER_SETTINGS, $settings, [], false);
236238
}
237239

238240
public function toArray(): array

tests/ConfigurationReader/GeneratorOptionsTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,58 @@ public function testSetUnexistingOptionValueWithInvalidValue(): void
545545
self::optionsInstance()->setGenerateTutorialFile('null');
546546
}
547547

548+
public function testSetOptionValueComposerSettingsDotNotation(): void
549+
{
550+
$newOptionKey = 'composer_settings';
551+
$newOptionValue = [
552+
'config.disable-tls:true',
553+
'autoload.psr-4.WsdlToPhp/PackageGenerator:src',
554+
];
555+
$expectedResult = [
556+
'config' => ['disable-tls' => true],
557+
'autoload' => ['psr-4' => ['WsdlToPhp/PackageGenerator' => 'src']],
558+
];
559+
560+
$instance = self::optionsInstance();
561+
$instance->setOptionValue($newOptionKey, $newOptionValue);
562+
563+
$this->assertSame(
564+
$expectedResult['config'],
565+
$instance->getComposerSettings()['config']
566+
);
567+
568+
$this->assertSame(
569+
$expectedResult['autoload'],
570+
$instance->getComposerSettings()['autoload']
571+
);
572+
}
573+
574+
public function testSetOptionValueComposerSettingsPhpArray(): void
575+
{
576+
$newOptionKey = 'composer_settings';
577+
$newOptionValue = [
578+
'config' => ['disable-tls' => true],
579+
'autoload' => ['psr-4' => ['WsdlToPhp/PackageGenerator' => 'src']],
580+
];
581+
$expectedResult = [
582+
'config' => ['disable-tls' => true],
583+
'autoload' => ['psr-4' => ['WsdlToPhp/PackageGenerator' => 'src']],
584+
];
585+
586+
$instance = self::optionsInstance();
587+
$instance->setOptionValue($newOptionKey, $newOptionValue);
588+
589+
$this->assertSame(
590+
$expectedResult['config'],
591+
$instance->getComposerSettings()['config']
592+
);
593+
594+
$this->assertSame(
595+
$expectedResult['autoload'],
596+
$instance->getComposerSettings()['autoload']
597+
);
598+
}
599+
548600
public function testGetUnexistingOptionValue(): void
549601
{
550602
$this->expectException(\InvalidArgumentException::class);

tests/File/ComposerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ public function testBingWithSettings(): void
6666
$this->assertSameFileContent('ValidBingComposerSettings', $composerFile, 'json');
6767
}
6868

69+
public function testBingWithSettingsAdditionalOptions(): void
70+
{
71+
$instance = self::getBingGeneratorInstance(true);
72+
$instance
73+
->setOptionPrefix('Api')
74+
->setOptionComposerName('wsdltophp/bing')
75+
->setOptionComposerSettings([
76+
'require.wsdltophp/wssecurity:dev-master',
77+
'require-dev.friendsofphp/php-cs-fixer:^3.0',
78+
'require-dev.phpstan/phpstan:^2',
79+
'require-dev.phpunit/phpunit:^9',
80+
'require-dev.rector/rector:^2',
81+
'config.disable-tls:true',
82+
'config.sort-packages:true'
83+
])
84+
;
85+
$composerFile = new Composer($instance, 'composer');
86+
$composerFile
87+
->setRunComposerUpdate(false)
88+
->write()
89+
;
90+
91+
$this->assertSameFileContent('ValidBingComposerSettingsAdditionalOptions', $composerFile, 'json');
92+
}
93+
6994
public function testBingWithEmptySrcDirname(): void
7095
{
7196
$instance = self::getBingGeneratorInstance(true);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "wsdltophp/bing",
3+
"description": "Package generated from __WSDL_URL__ using wsdltophp/packagegenerator",
4+
"require": {
5+
"php": ">=7.4",
6+
"ext-dom": "*",
7+
"ext-mbstring": "*",
8+
"ext-soap": "*",
9+
"wsdltophp/packagebase": "~5.0",
10+
"wsdltophp/wssecurity": "dev-master"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"": "./src/"
15+
}
16+
},
17+
"require-dev": {
18+
"friendsofphp/php-cs-fixer": "^3.0",
19+
"phpstan/phpstan": "^2",
20+
"phpunit/phpunit": "^9",
21+
"rector/rector": "^2"
22+
},
23+
"config": {
24+
"disable-tls": true,
25+
"sort-packages": true
26+
}
27+
}

0 commit comments

Comments
 (0)