php-censor/tests/src/Command/InstallCommandTest.php

278 lines
9.1 KiB
PHP
Raw Permalink Normal View History

<?php
2018-03-04 12:04:15 +01:00
namespace Tests\PHPCensor\Command;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Helper\HelperSet;
class InstallCommandTest extends \PHPUnit\Framework\TestCase
{
public $config;
public $admin;
/**
* @var Application
*/
protected $application;
public function setUp()
{
parent::setUp();
2015-02-16 12:54:52 +01:00
$this->application = new Application();
$this->application->setHelperSet(new HelperSet());
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
2015-02-16 12:54:52 +01:00
*/
protected function getHelperMock()
2015-02-16 12:54:52 +01:00
{
// We check that there's no interaction with user.
return $this
->getMockBuilder('Symfony\\Component\\Console\\Helper\\QuestionHelper')
->setMethods(['ask'])
->getMock();
2015-02-16 12:54:52 +01:00
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
2015-02-16 12:54:52 +01:00
*/
protected function getInstallCommandMock()
{
// Current command, we need to mock all method that interact with
// Database & File system.
2016-07-19 20:28:11 +02:00
$command = $this->getMockBuilder('PHPCensor\\Command\\InstallCommand')
2016-04-21 09:46:38 +02:00
->setMethods([
'reloadConfig',
'verifyNotInstalled',
'verifyDatabaseDetails',
'setupDatabase',
'createAdminUser',
2017-01-29 04:14:46 +01:00
'createDefaultGroup',
'writeConfigFile',
'checkRequirements',
2016-04-21 09:46:38 +02:00
])->getMock();
$self = $this;
2015-02-16 12:54:52 +01:00
$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;
})
);
2015-02-16 12:54:52 +01:00
$command->expects($this->once())->method('writeConfigFile')->will(
$this->returnCallback(function ($cfg) use ($self) {
$self->config = $cfg;
})
);
$command->expects($this->once())->method('checkRequirements');
2017-01-29 04:14:46 +01:00
$command->expects($this->once())->method('createDefaultGroup');
2015-02-16 12:54:52 +01:00
return $command;
}
protected function getCommandTester($helper)
{
$this->application->getHelperSet()->set($helper, 'question');
2015-02-16 12:54:52 +01:00
$this->application->add($this->getInstallCommandMock());
2016-07-21 19:02:11 +02:00
$command = $this->application->find('php-censor:install');
$commandTester = new CommandTester($command);
return $commandTester;
}
protected function getConfig($exclude = null)
{
2016-04-21 09:46:38 +02:00
$config = [
'--db-host' => 'localhost',
'--db-port' => '3306',
2016-07-22 08:14:10 +02:00
'--db-name' => 'php-censor-db',
'--db-user' => 'php-censor-user',
'--db-password' => 'php-censor-password',
2017-01-29 04:14:46 +01:00
'--db-type' => 'mysql',
'--admin-email' => 'admin@php-censor.local',
2016-07-22 08:14:10 +02:00
'--admin-name' => 'admin',
'--admin-password' => 'admin-password',
2016-07-22 08:14:10 +02:00
'--url' => 'http://php-censor.local',
2017-02-05 09:21:51 +01:00
'--queue-use' => false,
2016-04-21 09:46:38 +02:00
];
if (!is_null($exclude)) {
unset($config[$exclude]);
}
return $config;
}
2015-02-16 12:54:52 +01:00
protected function executeWithoutParam($param = null, $dialog)
{
// Clean result variables.
2016-04-21 06:58:09 +02:00
$this->admin = [];
$this->config = [];
// Get tester and execute with extracted parameters.
2015-02-16 12:54:52 +01:00
$commandTester = $this->getCommandTester($dialog);
$parameters = $this->getConfig($param);
$commandTester->execute($parameters);
}
2015-02-16 12:54:52 +01:00
public function testAutomaticInstallation()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
$dialog->expects($this->never())->method('ask');
$this->executeWithoutParam(null, $dialog);
}
2017-02-05 05:18:33 +01:00
public function testDatabaseTypeConfig()
{
$dialog = $this->getHelperMock();
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
$this->executeWithoutParam('--db-type', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->config['b8']['database']['type']);
2017-02-05 05:18:33 +01:00
}
public function testDatabaseHostConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
2015-02-16 12:54:52 +01:00
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
2015-02-16 12:54:52 +01:00
$this->executeWithoutParam('--db-host', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->config['b8']['database']['servers']['read'][0]['host']);
self::assertEquals('testedvalue', $this->config['b8']['database']['servers']['write'][0]['host']);
}
2017-02-05 05:18:33 +01:00
public function testDatabaseStringPortConfig()
{
$dialog = $this->getHelperMock();
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
$this->executeWithoutParam('--db-port', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertArrayNotHasKey('port', $this->config['b8']['database']['servers']['read'][0]);
self::assertArrayNotHasKey('port', $this->config['b8']['database']['servers']['write'][0]);
2017-02-05 05:18:33 +01:00
}
public function testDatabasePortConfig()
{
$dialog = $this->getHelperMock();
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('333');
$this->executeWithoutParam('--db-port', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals(333, $this->config['b8']['database']['servers']['read'][0]['port']);
self::assertEquals(333, $this->config['b8']['database']['servers']['write'][0]['port']);
2017-02-05 05:18:33 +01:00
}
public function testDatabaseNameConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
2015-02-16 12:54:52 +01:00
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
2015-02-16 12:54:52 +01:00
$this->executeWithoutParam('--db-name', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->config['b8']['database']['name']);
}
public function testDatabaseUserConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
2015-02-16 12:54:52 +01:00
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
2015-02-16 12:54:52 +01:00
$this->executeWithoutParam('--db-user', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->config['b8']['database']['username']);
}
public function testDatabasePasswordConfig()
{
$dialog = $this->getHelperMock();
2018-02-16 10:41:56 +01:00
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
$this->executeWithoutParam('--db-password', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->config['b8']['database']['password']);
}
2016-07-21 19:02:11 +02:00
public function testUrlConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('http://testedvalue.com');
2015-02-16 12:54:52 +01:00
$this->executeWithoutParam('--url', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('http://testedvalue.com', $this->config['php-censor']['url']);
}
public function testAdminEmailConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('admin@php-censor.local');
$this->executeWithoutParam('--admin-email', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('admin@php-censor.local', $this->admin['email']);
}
public function testAdminNameConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// Define expectation for dialog.
2015-02-16 12:54:52 +01:00
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
2015-02-16 12:54:52 +01:00
$this->executeWithoutParam('--admin-name', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->admin['name']);
}
public function testAdminPasswordConfig()
{
$dialog = $this->getHelperMock();
2015-02-16 12:54:52 +01:00
// We specified an input value for hostname.
$dialog->expects($this->once())->method('ask')->willReturn('testedvalue');
$this->executeWithoutParam('--admin-password', $dialog);
// Check that specified arguments are correctly loaded.
2018-02-16 10:41:56 +01:00
self::assertEquals('testedvalue', $this->admin['password']);
}
}