Permalink
Browse files

Testing env file writer

  • Loading branch information...
REBELinBLUE committed Feb 28, 2017
1 parent c5af376 commit 4eeec49391d635e6ecb06bf12dfc6d561a94cde5
View
@@ -9,7 +9,6 @@
phpunit.xml
phpcs.xml
phpmd.xml
phpstan.neon
_ide_helper.php
_ide_helper_models.php
.phpstorm.meta.php
View
@@ -66,8 +66,7 @@ phpdoc-check: ##@tests PHPDoc Checker
@php vendor/bin/phpdoccheck --directory=app --files-per-line 60
phpstan: ##@tests PHPStan
@if [ -f phpstan.neon ]; then php vendor/bin/phpstan analyse -l 0 -c phpstan.neon app/; fi
@if [ ! -f phpstan.neon ]; then php vendor/bin/phpstan analyse -l 0 -c phpstan.dist.neon app/; fi
php vendor/bin/phpstan analyse -l 0 -c phpstan.neon app/
phpmd: ##@tests PHP Mess Detector
@echo "${GREEN}PHP Mess Detector${RESET}"
@@ -92,6 +91,7 @@ coverage: ##@tests Test Coverage HTML
--testsuite "Integration Tests"
@phpdbg -qrr vendor/bin/phpcov merge storage/app/tmp/ \
--html storage/app/tmp/coverage/ --clover storage/app/tmp/coverage.xml
@rm storage/app/tmp/*.cov
phpunit-fast: ##@tests Unit Tests - Excluding slow model tests which touch the database
@echo "${GREEN}Fast unit tests${RESET}"
@@ -57,7 +57,7 @@ public function save(array $input)
// Remove keys not needed for sqlite
if ($input['db']['connection'] === 'sqlite') {
foreach (['host', 'database', 'username', 'password'] as $key) {
foreach (['host', 'database', 'username', 'password', 'port'] as $key) {
$key = strtoupper($key);
$config = preg_replace('/DB_' . $key . '=(.*)[\n]/', '', $config);
@@ -136,6 +136,8 @@ public function update()
// Copy the example file so that new values are copied
$this->filesystem->copy($dist, $current);
// FIXME: Not sure these 2 if statements are needed anymore
// Move the socket value to the correct key
if (isset($config['app']['socket'])) {
$config['socket']['url'] = $config['app']['socket'];
@@ -150,9 +152,7 @@ public function update()
unset($config['app']['ssl']);
}
$result = $this->save($config);
if (!$result) {
if (!$this->save($config)) {
return false;
}
@@ -80,7 +80,7 @@ public function handle(Dispatcher $dispatcher, Requirements $requirements, EnvFi
{
$this->line('');
if (!$this->checkCanInstall($requirements) || !$requirements->check($this)) {
if (!$this->checkCanInstall() || !$requirements->check($this)) {
return -1;
}
File renamed without changes.
@@ -39,9 +39,6 @@ class InstallAppTest extends TestCase
private $validator;
private $manager;
/**
*
*/
public function setUp()
{
parent::setUp();
@@ -69,6 +66,7 @@ public function setUp()
* @covers ::__construct
* @covers ::handle
* @covers ::verifyNotInstalled
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testVerifyNotInstalled()
{
@@ -86,6 +84,7 @@ public function testVerifyNotInstalled()
* @covers ::__construct
* @covers ::handle
* @covers ::verifyNotInstalled
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testCheckRequirements()
{
@@ -105,7 +104,6 @@ public function testCheckRequirements()
*/
public function testHandleSuccessful()
{
// FIXME: Clean up, lots of duplication
$this->config->shouldReceive('get')->with('app.key')->andReturn(false);
@@ -0,0 +1,239 @@
<?php
namespace REBELinBLUE\Deployer\Tests\Unit\Console\Commands\Installer;
use Mockery as m;
use REBELinBLUE\Deployer\Console\Commands\Installer\EnvFile;
use REBELinBLUE\Deployer\Services\Filesystem\Filesystem;
use REBELinBLUE\Deployer\Tests\TestCase;
/**
* @coversDefaultClass \REBELinBLUE\Deployer\Console\Commands\Installer\EnvFile
*/
class EnvFileTest extends TestCase
{
private $filesystem;
public function setUp()
{
parent::setUp();
$this->filesystem = m::mock(Filesystem::class);
$this->prev = base_path('.env.prev');
$this->env = base_path('.env');
$this->dist = base_path('.env.dist');
}
/**
* @covers ::__construct
* @covers ::save
*/
public function testSave()
{
$original_config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=https://deployer.app
SOCKET_URL=https://deployer.app
SOCKET_SSL_KEY_FILE=/var/ssl/key
SOCKET_SSL_CERT_FILE=/var/ssl/cert
SOCKET_SSL_CA_FILE=/var/ssl/ca
SOCKET_SSL_KEY_PASSPHRASE=password
DB_CONNECTION=mysql
DB_PORT=3306
DB_HOST=localhost
DB_DATABASE=deployer
DB_USERNAME=deployer
DB_PASSWORD=secret
REDIS_PASSWORD=null
GITHUB_OAUTH_TOKEN=token
TRUSTED_PROXIES=
# Comment should be removed
# As should multiple blank lines
MAIL_DRIVER=smtp
MAIL_HOST=mail.deployer.app
MAIL_PORT=2525
MAIL_USERNAME=mailuser
MAIL_PASSWORD=mailpass
EOF;
$input = [
'app' => [
'env' => 'local',
'debug' => 'true',
'url' => 'http://deployer.app',
],
'socket' => [
'url' => 'http://deployer.app',
],
'db' => [
'connection' => 'sqlite',
],
'mail' => [
'driver' => 'mail',
],
];
$updated_config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=http://deployer.app
SOCKET_URL=http://deployer.app
DB_CONNECTION=sqlite
MAIL_DRIVER=mail
EOF;
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($original_config);
$this->filesystem->shouldReceive('put')->once()->with($this->env, $updated_config)->andReturn(true);
$writer = new EnvFile($this->filesystem);
$actual = $writer->save($input);
$this->assertTrue($actual);
}
/**
* @covers ::__construct
* @covers ::update
*/
public function testUpdate()
{
$original_config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=http://deployer.app
SOCKET_URL=http://deployer.app
DB_CONNECTION=sqlite
# Comment
MAIL_DRIVER=sendmail
EOF;
$dist_config = <<< EOF
APP_ENV=production
APP_DEBUG=false
# Some comment
APP_URL=http://localhost
SOCKET_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=localhost
MAIL_DRIVER=sendmail
# More blank lines
SOMETHING_NEW=foo
EOF;
$updated_config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=http://deployer.app
SOCKET_URL=http://deployer.app
DB_CONNECTION=sqlite
MAIL_DRIVER=sendmail
SOMETHING_NEW=foo
EOF;
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($original_config);
$this->filesystem->shouldReceive('copy')->with($this->env, $this->prev);
$this->filesystem->shouldReceive('copy')->with($this->dist, $this->env);
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($dist_config);
$this->filesystem->shouldReceive('put')->with($this->env, $updated_config)->andReturn(true);
$this->filesystem->shouldReceive('md5')->with($this->env)->andReturn('hash');
$this->filesystem->shouldReceive('md5')->with($this->prev)->andReturn('hash');
$this->filesystem->shouldReceive('delete')->with($this->prev);
$writer = new EnvFile($this->filesystem);
$actual = $writer->update();
$this->assertTrue($actual);
}
/**
* @covers ::__construct
* @covers ::save
*/
public function testUpdateReturnsFalseOnError()
{
$config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=http://deployer.app
SOCKET_URL=http://deployer.app
DB_CONNECTION=sqlite
MAIL_DRIVER=sendmail
EOF;
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($config);
$this->filesystem->shouldReceive('copy')->with($this->env, $this->prev);
$this->filesystem->shouldReceive('copy')->with($this->dist, $this->env);
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($config);
$this->filesystem->shouldReceive('put')->with($this->env, $config)->andReturn(false);
$this->filesystem->shouldNotReceive('md5');
$this->filesystem->shouldNotReceive('delete');
$writer = new EnvFile($this->filesystem);
$actual = $writer->update();
$this->assertFalse($actual);
}
/**
* @covers ::__construct
* @covers ::save
*/
public function testUpdateDoesNotDeleteBackupIfConfigChanged()
{
$config = <<< EOF
APP_ENV=local
APP_DEBUG=true
APP_URL=http://deployer.app
SOCKET_URL=http://deployer.app
DB_CONNECTION=sqlite
MAIL_DRIVER=sendmail
EOF;
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($config);
$this->filesystem->shouldReceive('copy')->with($this->env, $this->prev);
$this->filesystem->shouldReceive('copy')->with($this->dist, $this->env);
$this->filesystem->shouldReceive('get')->once()->with($this->env)->andReturn($config);
$this->filesystem->shouldReceive('put')->with($this->env, $config)->andReturn(true);
$this->filesystem->shouldReceive('md5')->with($this->env)->andReturn('original-hash');
$this->filesystem->shouldReceive('md5')->with($this->prev)->andReturn('backup-hash');
$this->filesystem->shouldNotReceive('delete');
$writer = new EnvFile($this->filesystem);
$actual = $writer->update();
$this->assertTrue($actual);
}
}
@@ -61,6 +61,7 @@ public function setUp()
* @covers ::handle
* @covers ::verifyInstalled
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testVerifyInstalled($key)
{
@@ -87,6 +88,7 @@ public function provideAppKey()
* @covers ::handle
* @covers ::hasDeprecatedConfig
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testHasDeprecatedConfig()
{
@@ -107,6 +109,7 @@ public function testHasDeprecatedConfig()
* @covers ::handle
* @covers ::composerOutdated
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testComposerOutdated()
{
@@ -135,6 +138,7 @@ public function testComposerOutdated()
* @covers ::handle
* @covers ::nodeOutdated
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testNodeOutdated()
{
@@ -168,6 +172,7 @@ public function testNodeOutdated()
* @covers ::handle
* @covers ::hasRunningDeployments
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testHasRunningDeployments($running, $pending)
{
@@ -201,6 +206,7 @@ public function provideDeploymentCount()
* @covers ::__construct
* @covers ::handle
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testCheckRequirements()
{
@@ -228,6 +234,7 @@ public function testCheckRequirements()
* @covers ::verifyInstalled
* @covers ::hasDeprecatedConfig
* @covers ::checkCanInstall
* @covers \REBELinBLUE\Deployer\Console\Commands\Traits\OutputStyles::failure
*/
public function testHandleWhenConfirmationDenied()
{

0 comments on commit 4eeec49

Please sign in to comment.