Create admin command cleanup
This commit is contained in:
parent
5bb68507d8
commit
bf6ac530a6
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -33,91 +48,35 @@ class CreateAdminCommand extends Command
|
|||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
if (empty($adminEmail)) {
|
||||
return;
|
||||
// Function to validate mail address.
|
||||
$mailValidator = function ($answer) {
|
||||
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new \InvalidArgumentException(Lang::get('must_be_valid_email'));
|
||||
}
|
||||
|
||||
$adminPass = $this->ask(Lang::get('enter_pass'));
|
||||
$adminName = $this->ask(Lang::get('enter_name'));
|
||||
return $answer;
|
||||
};
|
||||
|
||||
$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('<error>%s</error>', Lang::get('failed_to_create')));
|
||||
$output->writeln(sprintf('<error>%s</error>', $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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
78
Tests/PHPCI/Command/CreateAdminCommandTest.php
Normal file
78
Tests/PHPCI/Command/CreateAdminCommandTest.php
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link http://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Plugin\Tests\Command;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class CreateAdminCommandTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Command\CreateAdminCommand|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Application|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $application;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Helper\DialogHelper|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $dialog;
|
||||
|
||||
public function setup()
|
||||
{
|
||||
parent::setup();
|
||||
|
||||
$this->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());
|
||||
}
|
||||
}
|
3
console
3
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();
|
Loading…
Reference in a new issue