application = new Application(); $this->application->setHelperSet(new HelperSet()); } /** * @return \PHPUnit_Framework_MockObject_MockObject */ protected function getHelperMock() { // We check that there's no interaction with user. return $this ->getMockBuilder('Symfony\\Component\\Console\\Helper\\QuestionHelper') ->setMethods(['ask']) ->getMock(); } /** * @return \PHPUnit_Framework_MockObject_MockObject */ protected function getInstallCommandMock() { // Current command, we need to mock all method that interact with // Database & File system. $command = $this->getMockBuilder('PHPCensor\\Command\\InstallCommand') ->setMethods([ 'reloadConfig', 'verifyNotInstalled', 'verifyDatabaseDetails', 'setupDatabase', 'createAdminUser', 'writeConfigFile', 'checkRequirements', ])->getMock(); $self = $this; $command->expects($this->once())->method('verifyNotInstalled')->willReturn(true); $command->expects($this->once())->method('verifyDatabaseDetails')->willReturn(true); $command->expects($this->once())->method('setupDatabase')->willReturn(true); $command->expects($this->once())->method('createAdminUser')->will( $this->returnCallback(function ($adm) use ($self) { $self->admin = $adm; }) ); $command->expects($this->once())->method('writeConfigFile')->will( $this->returnCallback(function ($cfg) use ($self) { $self->config = $cfg; }) ); $command->expects($this->once())->method('checkRequirements'); return $command; } protected function getCommandTester($helper) { $this->application->getHelperSet()->set($helper, 'question'); $this->application->add($this->getInstallCommandMock()); $command = $this->application->find('php-censor:install'); $commandTester = new CommandTester($command); return $commandTester; } protected function getConfig($exclude = null) { $config = [ '--db-host' => 'localhost', '--db-port' => '3306', '--db-name' => 'php-censor-db', '--db-user' => 'php-censor-user', '--db-pass' => 'php-censor-password', '--admin-mail' => 'admin@php-censor.local', '--admin-name' => 'admin', '--admin-pass' => 'admin-password', '--url' => 'http://php-censor.local', '--queue-disabled' => null, ]; if (!is_null($exclude)) { unset($config[$exclude]); } return $config; } protected function executeWithoutParam($param = null, $dialog) { // Clean result variables. $this->admin = []; $this->config = []; // Get tester and execute with extracted parameters. $commandTester = $this->getCommandTester($dialog); $parameters = $this->getConfig($param); $commandTester->execute($parameters); } public function testAutomaticInstallation() { $dialog = $this->getHelperMock(); $dialog->expects($this->never())->method('ask'); $this->executeWithoutParam(null, $dialog); } public function testDatabaseHostnameConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--db-host', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->config['b8']['database']['servers']['read'][0]['host']); $this->assertEquals('testedvalue', $this->config['b8']['database']['servers']['write'][0]['host']); } public function testDatabaseNameConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--db-name', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->config['b8']['database']['name']); } public function testDatabaseUserConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--db-user', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->config['b8']['database']['username']); } public function testDatabasePasswordConfig() { $dialog = $this->getHelperMock(); $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--db-pass', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->config['b8']['database']['password']); } public function testUrlConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('http://testedvalue.com'); $this->executeWithoutParam('--url', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('http://testedvalue.com', $this->config['php-censor']['url']); } public function testAdminEmailConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('admin@php-censor.local'); $this->executeWithoutParam('--admin-mail', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('admin@php-censor.local', $this->admin['mail']); } public function testAdminNameConfig() { $dialog = $this->getHelperMock(); // Define expectation for dialog. $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--admin-name', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->admin['name']); } public function testAdminPasswordConfig() { $dialog = $this->getHelperMock(); // We specified an input value for hostname. $dialog->expects($this->once())->method('ask')->willReturn('testedvalue'); $this->executeWithoutParam('--admin-pass', $dialog); // Check that specified arguments are correctly loaded. $this->assertEquals('testedvalue', $this->admin['pass']); } }