From c5382b2a7f352d566762688d78860e7b08a293dc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 6 Aug 2016 18:26:07 +0200 Subject: [PATCH] Created DisableKeyCommand --- module/CLI/config/cli.config.php | 1 + module/CLI/config/dependencies.config.php | 1 + .../CLI/src/Command/Api/DisableKeyCommand.php | 58 ++++++++++++++++- .../Command/Api/DisableKeyCommandTest.php | 62 +++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 module/CLI/test/Command/Api/DisableKeyCommandTest.php diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 3d9ff486..5739228c 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -12,6 +12,7 @@ return [ Command\ProcessVisitsCommand::class, Command\Config\GenerateCharsetCommand::class, Command\Api\GenerateKeyCommand::class, + Command\Api\DisableKeyCommand::class, ] ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index ca671731..03e3bd2c 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -18,6 +18,7 @@ return [ Command\ProcessVisitsCommand::class => AnnotatedFactory::class, Command\Config\GenerateCharsetCommand::class => AnnotatedFactory::class, Command\Api\GenerateKeyCommand::class => AnnotatedFactory::class, + Command\Api\DisableKeyCommand::class => AnnotatedFactory::class, ], ], diff --git a/module/CLI/src/Command/Api/DisableKeyCommand.php b/module/CLI/src/Command/Api/DisableKeyCommand.php index 3e52908e..738b8b43 100644 --- a/module/CLI/src/Command/Api/DisableKeyCommand.php +++ b/module/CLI/src/Command/Api/DisableKeyCommand.php @@ -1,6 +1,62 @@ apiKeyService = $apiKeyService; + $this->translator = $translator; + parent::__construct(null); + } + + public function configure() + { + $this->setName('api-key:disable') + ->setDescription($this->translator->translate('Disables an API key.')) + ->addArgument('apiKey', InputArgument::REQUIRED, $this->translator->translate('The API key to disable')); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $apiKey = $input->getArgument('apiKey'); + + try { + $this->apiKeyService->disable($apiKey); + $output->writeln(sprintf( + $this->translator->translate('API key %s properly disabled'), + '' . $apiKey . '' + )); + } catch (\InvalidArgumentException $e) { + $output->writeln(sprintf( + '' . $this->translator->translate('API key "%s" does not exist.') . '', + $apiKey + )); + } + } } diff --git a/module/CLI/test/Command/Api/DisableKeyCommandTest.php b/module/CLI/test/Command/Api/DisableKeyCommandTest.php new file mode 100644 index 00000000..68d5f8c2 --- /dev/null +++ b/module/CLI/test/Command/Api/DisableKeyCommandTest.php @@ -0,0 +1,62 @@ +apiKeyService = $this->prophesize(ApiKeyService::class); + $command = new DisableKeyCommand($this->apiKeyService->reveal(), Translator::factory([])); + $app = new Application(); + $app->add($command); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function providedApiKeyIsDisabled() + { + $apiKey = 'abcd1234'; + $this->apiKeyService->disable($apiKey)->shouldBeCalledTimes(1); + $this->commandTester->execute([ + 'command' => 'api-key:disable', + 'apiKey' => $apiKey, + ]); + } + + /** + * @test + */ + public function errorIsReturnedIfServiceThrowsException() + { + $apiKey = 'abcd1234'; + $this->apiKeyService->disable($apiKey)->willThrow(InvalidArgumentException::class) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'api-key:disable', + 'apiKey' => $apiKey, + ]); + $output = $this->commandTester->getDisplay(); + $this->assertEquals('API key "abcd1234" does not exist.' . PHP_EOL, $output); + } +}