Skip to content

Commit dd326bd

Browse files
committed
Add tagging mode enum
1 parent 75983e4 commit dd326bd

6 files changed

Lines changed: 81 additions & 30 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ Symfony bundle which provides a way to version your application using various ve
6464
6565
# The mode for applying tags to version commits:
6666
# - 'always': automatically add a tag without prompting
67-
# - 'never': do not add a tag
6867
# - 'ask': prompt before tagging when incrementing versions
69-
tagging_mode: ask # One of "always"; "never"; "ask"
68+
# - 'never': do not add a tag
69+
tagging_mode: ask # One of "always"; "ask"; "never"
7070
7171
# The message to use for the VCS tag.
7272
tag_message: 'Update application version to %s'

src/Command/IncrementCommand.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bizkit\VersioningBundle\Exception\VCSException;
1010
use Bizkit\VersioningBundle\Reader\ReaderInterface;
1111
use Bizkit\VersioningBundle\Strategy\StrategyInterface;
12+
use Bizkit\VersioningBundle\VCS\TaggingMode;
1213
use Bizkit\VersioningBundle\VCS\VCSHandlerInterface;
1314
use Bizkit\VersioningBundle\Version;
1415
use Bizkit\VersioningBundle\Writer\WriterInterface;
@@ -25,22 +26,14 @@
2526
)]
2627
final class IncrementCommand extends Command
2728
{
28-
public const TAGGING_MODES = ['always', 'never', 'ask'];
29-
3029
public function __construct(
3130
private readonly string $file,
3231
private readonly ReaderInterface $reader,
3332
private readonly WriterInterface $writer,
3433
private readonly StrategyInterface $strategy,
3534
private readonly ?VCSHandlerInterface $vcsHandler = null,
36-
private readonly string $vcsTaggingMode = 'ask',
35+
private readonly TaggingMode $vcsTaggingMode = TaggingMode::Ask,
3736
) {
38-
if (!\in_array($vcsTaggingMode, self::TAGGING_MODES, true)) {
39-
throw new \InvalidArgumentException(
40-
\sprintf('Invalid VCS tagging mode "%s". Expected one of: "%s".', $vcsTaggingMode, implode('", "', self::TAGGING_MODES)),
41-
);
42-
}
43-
4437
parent::__construct();
4538
}
4639

@@ -100,12 +93,12 @@ private function commitAndTag(StyleInterface $io, Version $version): void
10093
$this->vcsHandler->commit($io, $version);
10194
$io->success('Your application version file has successfully been committed to your VCS.');
10295

103-
if ('never' === $this->vcsTaggingMode) {
96+
if (TaggingMode::Never === $this->vcsTaggingMode) {
10497
return;
10598
}
10699

107100
if (
108-
'always' === $this->vcsTaggingMode
101+
TaggingMode::Always === $this->vcsTaggingMode
109102
|| $io->confirm(\sprintf('Do you wish to create a tag with the version "%s"?', $version))
110103
) {
111104
$this->vcsHandler->tag($io, $version);

src/DependencyInjection/Configuration.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Bizkit\VersioningBundle\DependencyInjection;
66

7-
use Bizkit\VersioningBundle\Command\IncrementCommand;
7+
use Bizkit\VersioningBundle\VCS\TaggingMode;
88
use Bizkit\VersioningBundle\VCS\VCSHandlerInterface;
99
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1010
use Symfony\Component\Config\Definition\ConfigurationInterface;
11+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1112

1213
final class Configuration implements ConfigurationInterface
1314
{
@@ -67,10 +68,18 @@ public function getConfigTreeBuilder(): TreeBuilder
6768
->defaultValue(VCSHandlerInterface::DEFAULT_MESSAGE)
6869
->end()
6970
->enumNode('tagging_mode')
70-
->values(IncrementCommand::TAGGING_MODES)
71+
->beforeNormalization()
72+
->ifString()
73+
->then(static fn ($v) => TaggingMode::tryFrom($v) ?? throw new InvalidConfigurationException(\sprintf(
74+
'Invalid tagging mode provided: expected one of "%s", got "%s".',
75+
implode('", "', array_map(static fn (TaggingMode $case) => $case->value, TaggingMode::cases())),
76+
$v,
77+
)))
78+
->end()
79+
->values(TaggingMode::cases())
7180
->info("The mode for applying tags to version commits:\n- 'always': automatically add a tag without prompting\n- 'never': do not add a tag\n- 'ask': prompt before tagging when incrementing versions")
7281
->cannotBeEmpty()
73-
->defaultValue('ask')
82+
->defaultValue(TaggingMode::Ask)
7483
->end()
7584
->scalarNode('tag_message')
7685
->info('The message to use for the VCS tag.')

src/VCS/TaggingMode.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bizkit\VersioningBundle\VCS;
6+
7+
enum TaggingMode: string
8+
{
9+
case Always = 'always';
10+
case Ask = 'ask';
11+
case Never = 'never';
12+
}

tests/Command/IncrementCommandTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bizkit\VersioningBundle\Strategy\IncrementingStrategy;
1010
use Bizkit\VersioningBundle\Tests\TestCase;
1111
use Bizkit\VersioningBundle\VCS\GitHandler;
12+
use Bizkit\VersioningBundle\VCS\TaggingMode;
1213
use Bizkit\VersioningBundle\Writer\YamlFileWriter;
1314
use Symfony\Component\Console\Tester\CommandTester;
1415
use Symfony\Component\Yaml\Yaml;
@@ -41,14 +42,6 @@ protected function tearDown(): void
4142
);
4243
}
4344

44-
public function testExceptionIsThrownForInvalidTaggingMode(): void
45-
{
46-
$this->expectException(\InvalidArgumentException::class);
47-
$this->expectExceptionMessage('Invalid VCS tagging mode "invalid". Expected one of: "always", "never", "ask".');
48-
49-
$this->createCommandTester($this->validFile, null, 'invalid');
50-
}
51-
5245
public function testVersionIsIncremented(): void
5346
{
5447
$commandTester = $this->createCommandTester($this->validFile);
@@ -186,7 +179,7 @@ public function testFileIsCommittedAndTagIsNotCreatedIfVCSHandlerIsNotNullAndCon
186179

187180
public function testFileIsCommittedAndTagIsAutocreatedIfVCSHandlerIsNotNullAndTaggingModeIsAlways(): void
188181
{
189-
$commandTester = $this->createCommandTester($this->validFile, __DIR__.'/Fixtures/fake-git/success.php', 'always');
182+
$commandTester = $this->createCommandTester($this->validFile, __DIR__.'/Fixtures/fake-git/success.php', TaggingMode::Always);
190183
$commandTester->setInputs(['yes', 'yes']);
191184

192185
$statusCode = $commandTester->execute([]);
@@ -202,7 +195,7 @@ public function testFileIsCommittedAndTagIsAutocreatedIfVCSHandlerIsNotNullAndTa
202195

203196
public function testFileIsCommittedAndTagIsNotPromptedIfVCSHandlerIsNotNullAndTaggingModeIsNever(): void
204197
{
205-
$commandTester = $this->createCommandTester($this->validFile, __DIR__.'/Fixtures/fake-git/success.php', 'never');
198+
$commandTester = $this->createCommandTester($this->validFile, __DIR__.'/Fixtures/fake-git/success.php', TaggingMode::Never);
206199
$commandTester->setInputs(['yes', 'yes']);
207200

208201
$statusCode = $commandTester->execute([]);
@@ -232,7 +225,7 @@ public function testVCSHandlerErrorIsSentToOutput(): void
232225
self::assertStringContainsString('Cannot create the tag "v2" as it already exists.', $display);
233226
}
234227

235-
private function createCommandTester(string $file, ?string $pathToVCSExecutable = null, string $taggingMode = 'ask'): CommandTester
228+
private function createCommandTester(string $file, ?string $pathToVCSExecutable = null, TaggingMode $taggingMode = TaggingMode::Ask): CommandTester
236229
{
237230
$vcs = null !== $pathToVCSExecutable ? new GitHandler($file, pathToExecutable: $pathToVCSExecutable) : null;
238231

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Bizkit\VersioningBundle\DependencyInjection\Configuration;
88
use Bizkit\VersioningBundle\Tests\TestCase;
9+
use Bizkit\VersioningBundle\VCS\TaggingMode;
10+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
911
use Symfony\Component\Config\Definition\Processor;
1012

1113
/**
@@ -26,7 +28,7 @@ public function testDefaultConfig(): void
2628
'vcs' => [
2729
'handler' => 'git',
2830
'commit_message' => 'Update application version to %s',
29-
'tagging_mode' => 'ask',
31+
'tagging_mode' => TaggingMode::Ask,
3032
'tag_message' => 'Update application version to %s',
3133
'name' => null,
3234
'email' => null,
@@ -45,7 +47,7 @@ public function testConfigWhenVCSIsTrue(): void
4547
self::assertSame([
4648
'handler' => 'git',
4749
'commit_message' => 'Update application version to %s',
48-
'tagging_mode' => 'ask',
50+
'tagging_mode' => TaggingMode::Ask,
4951
'tag_message' => 'Update application version to %s',
5052
'name' => null,
5153
'email' => null,
@@ -63,11 +65,53 @@ public function testConfigWhenVCSIsFalse(): void
6365
self::assertSame([
6466
'handler' => null,
6567
'commit_message' => 'Update application version to %s',
66-
'tagging_mode' => 'ask',
68+
'tagging_mode' => TaggingMode::Ask,
6769
'tag_message' => 'Update application version to %s',
6870
'name' => null,
6971
'email' => null,
7072
'path_to_executable' => null,
7173
], $config['vcs']);
7274
}
75+
76+
/**
77+
* @dataProvider provideVCSTaggingModeAsStringCases
78+
*/
79+
public function testConfigWhenVCSTaggingModeIsString(string $taggingMode, TaggingMode $expectedTaggingMode): void
80+
{
81+
$config = (new Processor())->processConfiguration(new Configuration(), ['bizkit_versioning' => [
82+
'vcs' => [
83+
'tagging_mode' => $taggingMode,
84+
],
85+
]]);
86+
87+
self::assertArrayHasKey('vcs', $config);
88+
self::assertSame([
89+
'tagging_mode' => $expectedTaggingMode,
90+
'handler' => 'git',
91+
'commit_message' => 'Update application version to %s',
92+
'tag_message' => 'Update application version to %s',
93+
'name' => null,
94+
'email' => null,
95+
'path_to_executable' => null,
96+
], $config['vcs']);
97+
}
98+
99+
public static function provideVCSTaggingModeAsStringCases(): iterable
100+
{
101+
yield ['always', TaggingMode::Always];
102+
yield ['ask', TaggingMode::Ask];
103+
yield ['never', TaggingMode::Never];
104+
}
105+
106+
public function testConfigWhenVCSTaggingModeIsInvalidString(): void
107+
{
108+
$this->expectException(InvalidConfigurationException::class);
109+
$this->expectExceptionMessage('Invalid tagging mode provided: expected one of "always", "ask", "never", got "invalid_mode".');
110+
111+
(new Processor())->processConfiguration(new Configuration(), ['bizkit_versioning' => [
112+
'vcs' => [
113+
'tagging_mode' => 'invalid_mode',
114+
],
115+
]]);
116+
}
73117
}

0 commit comments

Comments
 (0)