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/MigrationStore.php
|
||||||
PHPCI/Store/Base/MigrationStoreBase.php
|
PHPCI/Store/Base/MigrationStoreBase.php
|
||||||
local_vars.php
|
local_vars.php
|
||||||
|
Tests/PHPCI/config.yml
|
||||||
|
|
|
@ -9,21 +9,36 @@
|
||||||
|
|
||||||
namespace PHPCI\Command;
|
namespace PHPCI\Command;
|
||||||
|
|
||||||
use PHPCI\Helper\Lang;
|
|
||||||
use PHPCI\Service\UserService;
|
use PHPCI\Service\UserService;
|
||||||
|
use PHPCI\Helper\Lang;
|
||||||
|
use PHPCI\Store\UserStore;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use b8\Store\Factory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create admin command - creates an admin user
|
* Create admin command - creates an admin user
|
||||||
* @author Wogan May (@woganmay)
|
* @author Wogan May (@woganmay)
|
||||||
* @package PHPCI
|
* @package PHPCI
|
||||||
* @subpackage Console
|
* @subpackage Console
|
||||||
*/
|
*/
|
||||||
class CreateAdminCommand extends Command
|
class CreateAdminCommand extends Command
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var UserStore
|
||||||
|
*/
|
||||||
|
protected $userStore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UserStore $userStore
|
||||||
|
*/
|
||||||
|
public function __construct(UserStore $userStore)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->userStore = $userStore;
|
||||||
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$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)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$userStore = Factory::getStore('User');
|
$userService = new UserService($this->userStore);
|
||||||
$userService = new UserService($userStore);
|
|
||||||
|
|
||||||
require(PHPCI_DIR . 'bootstrap.php');
|
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
|
||||||
|
$dialog = $this->getHelperSet()->get('dialog');
|
||||||
|
|
||||||
// Try to create a user account:
|
// Function to validate mail address.
|
||||||
$adminEmail = $this->ask(Lang::get('enter_email'), true, FILTER_VALIDATE_EMAIL);
|
$mailValidator = function ($answer) {
|
||||||
|
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
throw new \InvalidArgumentException(Lang::get('must_be_valid_email'));
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($adminEmail)) {
|
return $answer;
|
||||||
return;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
$adminPass = $this->ask(Lang::get('enter_pass'));
|
$adminEmail = $dialog->askAndValidate($output, Lang::get('enter_email'), $mailValidator, false);
|
||||||
$adminName = $this->ask(Lang::get('enter_name'));
|
$adminName = $dialog->ask($output, Lang::get('enter_name'));
|
||||||
|
$adminPass = $dialog->askHiddenResponse($output, Lang::get('enter_password'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$userService->createUser($adminName, $adminEmail, $adminPass, 1);
|
$userService->createUser($adminName, $adminEmail, $adminPass, true);
|
||||||
print Lang::get('user_created') . PHP_EOL;
|
$output->writeln(Lang::get('user_created'));
|
||||||
} catch (\Exception $ex) {
|
} catch (\Exception $e) {
|
||||||
print Lang::get('failed_to_create') . PHP_EOL;
|
$output->writeln(sprintf('<error>%s</error>', Lang::get('failed_to_create')));
|
||||||
print $ex->getMessage();
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
|
||||||
print PHP_EOL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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->writeConfigFile($conf);
|
||||||
$this->setupDatabase($output);
|
$this->setupDatabase($output);
|
||||||
$admin = $this->getAdminInforamtion($input, $output);
|
$admin = $this->getAdminInformation($input, $output);
|
||||||
$this->createAdminUser($admin, $output);
|
$this->createAdminUser($admin, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ class InstallCommand extends Command
|
||||||
* @param OutputInterface $output
|
* @param OutputInterface $output
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getAdminInforamtion(InputInterface $input, OutputInterface $output)
|
protected function getAdminInformation(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$admin = array();
|
$admin = array();
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ class InstallCommand extends Command
|
||||||
// Function to validate mail address.
|
// Function to validate mail address.
|
||||||
$mailValidator = function ($answer) {
|
$mailValidator = function ($answer) {
|
||||||
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
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;
|
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());
|
||||||
|
}
|
||||||
|
}
|
5
console
5
console
|
@ -20,6 +20,7 @@ use PHPCI\Command\DaemonCommand;
|
||||||
use PHPCI\Command\PollCommand;
|
use PHPCI\Command\PollCommand;
|
||||||
use PHPCI\Command\CreateAdminCommand;
|
use PHPCI\Command\CreateAdminCommand;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
|
use b8\Store\Factory;
|
||||||
|
|
||||||
$application = new Application();
|
$application = new Application();
|
||||||
|
|
||||||
|
@ -29,6 +30,6 @@ $application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand')));
|
||||||
$application->add(new GenerateCommand);
|
$application->add(new GenerateCommand);
|
||||||
$application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand')));
|
$application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand')));
|
||||||
$application->add(new PollCommand($loggerConfig->getFor('PollCommand')));
|
$application->add(new PollCommand($loggerConfig->getFor('PollCommand')));
|
||||||
$application->add(new CreateAdminCommand);
|
$application->add(new CreateAdminCommand(Factory::getStore('User')));
|
||||||
|
|
||||||
$application->run();
|
$application->run();
|
||||||
|
|
Loading…
Reference in a new issue