From bf6ac530a67e0385032ed653338c34625fec82b8 Mon Sep 17 00:00:00 2001 From: Jon Gotlin Date: Thu, 4 Dec 2014 12:31:21 +0100 Subject: [PATCH] Create admin command cleanup --- .gitignore | 1 + PHPCI/Command/CreateAdminCommand.php | 129 ++++++------------ PHPCI/Command/InstallCommand.php | 6 +- .../PHPCI/Command/CreateAdminCommandTest.php | 78 +++++++++++ console | 5 +- 5 files changed, 129 insertions(+), 90 deletions(-) create mode 100644 Tests/PHPCI/Command/CreateAdminCommandTest.php diff --git a/.gitignore b/.gitignore index 79784a60..63e81c5b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ PHPCI/Model/Base/MigrationBase.php PHPCI/Store/MigrationStore.php PHPCI/Store/Base/MigrationStoreBase.php local_vars.php +Tests/PHPCI/config.yml diff --git a/PHPCI/Command/CreateAdminCommand.php b/PHPCI/Command/CreateAdminCommand.php index 3b69afd3..265f03b4 100644 --- a/PHPCI/Command/CreateAdminCommand.php +++ b/PHPCI/Command/CreateAdminCommand.php @@ -9,21 +9,36 @@ namespace PHPCI\Command; -use PHPCI\Helper\Lang; use PHPCI\Service\UserService; +use PHPCI\Helper\Lang; +use PHPCI\Store\UserStore; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use b8\Store\Factory; /** -* Create admin command - creates an admin user -* @author Wogan May (@woganmay) -* @package PHPCI -* @subpackage Console -*/ + * Create admin command - creates an admin user + * @author Wogan May (@woganmay) + * @package PHPCI + * @subpackage Console + */ class CreateAdminCommand extends Command { + /** + * @var UserStore + */ + protected $userStore; + + /** + * @param UserStore $userStore + */ + public function __construct(UserStore $userStore) + { + parent::__construct(); + + $this->userStore = $userStore; + } + protected function configure() { $this @@ -32,92 +47,36 @@ class CreateAdminCommand extends Command } /** - * Creates an admin user in the existing PHPCI database - */ + * Creates an admin user in the existing PHPCI database + * + * {@inheritDoc} + */ protected function execute(InputInterface $input, OutputInterface $output) { - $userStore = Factory::getStore('User'); - $userService = new UserService($userStore); + $userService = new UserService($this->userStore); - require(PHPCI_DIR . 'bootstrap.php'); + /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ + $dialog = $this->getHelperSet()->get('dialog'); - // Try to create a user account: - $adminEmail = $this->ask(Lang::get('enter_email'), true, FILTER_VALIDATE_EMAIL); + // Function to validate mail address. + $mailValidator = function ($answer) { + if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { + throw new \InvalidArgumentException(Lang::get('must_be_valid_email')); + } - if (empty($adminEmail)) { - return; - } + return $answer; + }; - $adminPass = $this->ask(Lang::get('enter_pass')); - $adminName = $this->ask(Lang::get('enter_name')); + $adminEmail = $dialog->askAndValidate($output, Lang::get('enter_email'), $mailValidator, false); + $adminName = $dialog->ask($output, Lang::get('enter_name')); + $adminPass = $dialog->askHiddenResponse($output, Lang::get('enter_password')); try { - $userService->createUser($adminName, $adminEmail, $adminPass, 1); - print Lang::get('user_created') . PHP_EOL; - } catch (\Exception $ex) { - print Lang::get('failed_to_create') . PHP_EOL; - print $ex->getMessage(); - print PHP_EOL; + $userService->createUser($adminName, $adminEmail, $adminPass, true); + $output->writeln(Lang::get('user_created')); + } catch (\Exception $e) { + $output->writeln(sprintf('%s', Lang::get('failed_to_create'))); + $output->writeln(sprintf('%s', $e->getMessage())); } } - - protected function ask($question, $emptyOk = false, $validationFilter = null) - { - print $question . ' '; - - $rtn = ''; - $stdin = fopen('php://stdin', 'r'); - $rtn = fgets($stdin); - fclose($stdin); - - $rtn = trim($rtn); - - if (!$emptyOk && empty($rtn)) { - $rtn = $this->ask($question, $emptyOk, $validationFilter); - } elseif (!is_null($validationFilter) && ! empty($rtn)) { - if (! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) { - print $statusMessage; - $rtn = $this->ask($question, $emptyOk, $validationFilter); - } - } - - return $rtn; - } - protected function controlFormat($valueToInspect, $filter, &$statusMessage) - { - $filters = !(is_array($filter))? array($filter) : $filter; - $statusMessage = ''; - $status = true; - $options = array(); - - foreach ($filters as $filter) { - if (! is_int($filter)) { - $regexp = $filter; - $filter = FILTER_VALIDATE_REGEXP; - $options = array( - 'options' => array( - 'regexp' => $regexp, - ) - ); - } - if (! filter_var($valueToInspect, $filter, $options)) { - $status = false; - - switch ($filter) - { - case FILTER_VALIDATE_URL: - $statusMessage = Lang::get('must_be_valid_url') . PHP_EOL; - break; - case FILTER_VALIDATE_EMAIL: - $statusMessage = Lang::get('must_be_valid_email') . PHP_EOL; - break; - case FILTER_VALIDATE_REGEXP: - $statusMessage = Lang::get('incorrect_format') . PHP_EOL; - break; - } - } - } - - return $status; - } } diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index d262b9f0..04e0f2dd 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -95,7 +95,7 @@ class InstallCommand extends Command $this->writeConfigFile($conf); $this->setupDatabase($output); - $admin = $this->getAdminInforamtion($input, $output); + $admin = $this->getAdminInformation($input, $output); $this->createAdminUser($admin, $output); } @@ -160,7 +160,7 @@ class InstallCommand extends Command * @param OutputInterface $output * @return array */ - protected function getAdminInforamtion(InputInterface $input, OutputInterface $output) + protected function getAdminInformation(InputInterface $input, OutputInterface $output) { $admin = array(); @@ -172,7 +172,7 @@ class InstallCommand extends Command // Function to validate mail address. $mailValidator = function ($answer) { if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { - throw new Exception(Lang::get('must_be_valid_email')); + throw new \InvalidArgumentException(Lang::get('must_be_valid_email')); } return $answer; diff --git a/Tests/PHPCI/Command/CreateAdminCommandTest.php b/Tests/PHPCI/Command/CreateAdminCommandTest.php new file mode 100644 index 00000000..738d8f8a --- /dev/null +++ b/Tests/PHPCI/Command/CreateAdminCommandTest.php @@ -0,0 +1,78 @@ +command = $this->getMockBuilder('PHPCI\\Command\\CreateAdminCommand') + ->setConstructorArgs([$this->getMock('PHPCI\\Store\\UserStore')]) + ->setMethods(['reloadConfig']) + ->getMock() + ; + + $this->dialog = $this->getMockBuilder('Symfony\\Component\\Console\\Helper\\DialogHelper') + ->setMethods([ + 'ask', + 'askAndValidate', + 'askHiddenResponse', + ]) + ->getMock() + ; + + $this->application = new Application(); + } + + /** + * @return CommandTester + */ + protected function getCommandTester() + { + $this->application->getHelperSet()->set($this->dialog, 'dialog'); + $this->application->add($this->command); + $command = $this->application->find('phpci:create-admin'); + $commandTester = new CommandTester($command); + + return $commandTester; + } + + public function testExecute() + { + $this->dialog->expects($this->at(0))->method('askAndValidate')->will($this->returnValue('test@example.com')); + $this->dialog->expects($this->at(1))->method('ask')->will($this->returnValue('A name')); + $this->dialog->expects($this->at(2))->method('askHiddenResponse')->will($this->returnValue('foobar123')); + + $commandTester = $this->getCommandTester(); + $commandTester->execute([]); + + $this->assertEquals('User account created!' . PHP_EOL, $commandTester->getDisplay()); + } +} diff --git a/console b/console index 43fc2b8f..782cc2d0 100755 --- a/console +++ b/console @@ -20,6 +20,7 @@ use PHPCI\Command\DaemonCommand; use PHPCI\Command\PollCommand; use PHPCI\Command\CreateAdminCommand; use Symfony\Component\Console\Application; +use b8\Store\Factory; $application = new Application(); @@ -29,6 +30,6 @@ $application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand'))); $application->add(new GenerateCommand); $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand'))); $application->add(new PollCommand($loggerConfig->getFor('PollCommand'))); -$application->add(new CreateAdminCommand); +$application->add(new CreateAdminCommand(Factory::getStore('User'))); -$application->run(); \ No newline at end of file +$application->run();