diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 6a6e4122..a9b13a72 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -10,6 +10,7 @@ return [ Command\ListShortcodesCommand::class, Command\GetVisitsCommand::class, Command\ProcessVisitsCommand::class, + Command\Config\GenerateCharsetCommand::class, ] ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 86398cf4..d99f68d9 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -1,19 +1,22 @@ [ 'factories' => [ - Console\Application::class => CLI\Factory\ApplicationFactory::class, + Application::class => ApplicationFactory::class, - CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, - CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, - CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class, - CLI\Command\GetVisitsCommand::class => AnnotatedFactory::class, - CLI\Command\ProcessVisitsCommand::class => AnnotatedFactory::class, + Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, + Command\ResolveUrlCommand::class => AnnotatedFactory::class, + Command\ListShortcodesCommand::class => AnnotatedFactory::class, + Command\GetVisitsCommand::class => AnnotatedFactory::class, + Command\ProcessVisitsCommand::class => AnnotatedFactory::class, + Command\ProcessVisitsCommand::class => AnnotatedFactory::class, + Command\Config\GenerateCharsetCommand::class => AnnotatedFactory::class, ], ], diff --git a/module/CLI/src/Command/Config/GenerateCharsetCommand.php b/module/CLI/src/Command/Config/GenerateCharsetCommand.php new file mode 100644 index 00000000..22369934 --- /dev/null +++ b/module/CLI/src/Command/Config/GenerateCharsetCommand.php @@ -0,0 +1,44 @@ +translator = $translator; + parent::__construct(null); + } + + public function configure() + { + $this->setName('config:generate-charset') + ->setDescription(sprintf($this->translator->translate( + 'Generates a character set sample just by shuffling the default one, "%s". ' + . 'Then it can be set in the SHORTCODE_CHARS environment variable' + ), UrlShortener::DEFAULT_CHARS)); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $charSet = str_shuffle(UrlShortener::DEFAULT_CHARS); + $output->writeln($this->translator->translate('Character set:') . sprintf(' %s', $charSet)); + } +} diff --git a/module/CLI/test/Command/Config/GenerateCharsetCommandTest.php b/module/CLI/test/Command/Config/GenerateCharsetCommandTest.php new file mode 100644 index 00000000..03a81b53 --- /dev/null +++ b/module/CLI/test/Command/Config/GenerateCharsetCommandTest.php @@ -0,0 +1,56 @@ +add($command); + + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function charactersAreGeneratedFromDefault() + { + $prefix = 'Character set: '; + $prefixLength = strlen($prefix); + + $this->commandTester->execute([ + 'command' => 'config:generate-charset', + ]); + $output = $this->commandTester->getDisplay(); + + // Both default character set and the new one should have the same length + $this->assertEquals($prefixLength + strlen(UrlShortener::DEFAULT_CHARS) + 1, strlen($output)); + + // Both default character set and the new one should have the same characters + $charset = substr($output, $prefixLength, strlen(UrlShortener::DEFAULT_CHARS)); + $orderedDefault = $this->orderStringLetters(UrlShortener::DEFAULT_CHARS); + $orderedCharset = $this->orderStringLetters($charset); + $this->assertEquals($orderedDefault, $orderedCharset); + } + + protected function orderStringLetters($string) + { + $letters = str_split($string); + sort($letters); + return implode('', $letters); + } +}