protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) { $this->input = $input; $this->output = $output; if (posix_getuid() > 0) { $output->writeln('You must run this as root.. Your id is: ' . posix_getuid()); die; } $helper = $this->getHelper('question'); if (!empty($input->getOption('name'))) { $this->virtualHostConfiguration['name'] = trim($input->getOption('name')); } else { $question = new \Symfony\Component\Console\Question\Question('Input name of the installation. This will be used to create user and group (lowercase letters and numbers only): '); $question->setValidator(function ($answer) { if ($answer == '') { throw new \RuntimeException('Installation name cannot be empty'); } if (in_array($answer . '_web', $this->getSystemUsers())) { throw new \RuntimeException('User exists'); } if (in_array($answer . '_grp', $this->getSystemGroups())) { throw new \RuntimeException('Group exists'); } return $answer; }); $this->virtualHostConfiguration['name'] = trim($helper->ask($input, $output, $question)); } /*************************************************************************************************************** * **************************************************************************************************************/ $this->virtualHostConfiguration['web_user'] = $this->virtualHostConfiguration['name'] . '_web'; $this->virtualHostConfiguration['login_user'] = $this->virtualHostConfiguration['name'] . '_login'; $this->virtualHostConfiguration['group'] = $this->virtualHostConfiguration['name'] . '_grp'; // Get the pseudo-group. if (!empty($input->getOption('group'))) { $this->virtualHostConfiguration['pseudoGroup'] = trim($input->getOption('group')); } else { $question = new \Symfony\Component\Console\Question\ChoiceQuestion('Please select a pseudo group for the domain: ', $this->configuration['pseudo_groups']); $question->setErrorMessage('Pseudo group %s is invalid.'); $this->virtualHostConfiguration['pseudoGroup'] = $helper->ask($input, $output, $question); } $pseudoGroupDirectory = $this->configuration['directories']['hostroot'] . '/' . $this->virtualHostConfiguration['pseudoGroup']; $virtualHostDirectory = $pseudoGroupDirectory . '/' . $this->virtualHostConfiguration['name']; $this->virtualHostConfiguration['login_homedir'] = $virtualHostDirectory; $this->virtualHostConfiguration['web_homedir'] = $virtualHostDirectory . '/htdocs/'; $this->create(); }
public function testAskAndValidate() { $inputMock = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputInterface')->getMock(); $outputMock = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface')->getMock(); $questionHelperMock = $this->getMockBuilder('Symfony\\Component\\Console\\Helper\\QuestionHelper')->getMock(); $helperMock = $this->getMockBuilder('Symfony\\Component\\Console\\Helper\\HelperSet')->getMock(); $question = new \Symfony\Component\Console\Question\Question('Is valid?', true); $question->setMaxAttempts(10); $question->setValidator(function ($answer) { return $answer; }); $questionHelperMock->expects($this->once())->method('ask')->with($this->isInstanceOf('Symfony\\Component\\Console\\Input\\InputInterface'), $this->isInstanceOf('Symfony\\Component\\Console\\Output\\OutputInterface'), $question); $helperMock->expects($this->once())->method('get')->with($this->equalTo('question'))->will($this->returnValue($questionHelperMock)); $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); $consoleIO->askAndValidate('Is valid?', function ($answer) { return $answer; }, 10, true); }