Skip to content

Commit

Permalink
Add command to resync mail configuration from environment variables/c…
Browse files Browse the repository at this point in the history
…onfiguration. Closes #9

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Dec 15, 2019
1 parent 275d64c commit d1b1413
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Console/ConfigureMailCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Orchestra\Installation\Console;

use Illuminate\Console\Command;
use Orchestra\Contracts\Foundation\Foundation;
use Orchestra\Installation\MailConfigurationUpdater;

class ConfigureMailCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'orchestra:configure-email';

/**
* Handle the command.
*
* @param \Orchestra\Contracts\Foundation\Foundation $foundation
* @return int
*/
public function handle(Foundation $foundation)
{
if (! $foundation->installed()) {
$this->error('This command can only be executed when the application has been installed!');

return 1;
}

$memory = $foundation->memory();

$name = $this->ask('What is the application name?', $memory->get('email.from.name'));
$email = $this->ask('What is the e-mail address?', $memory->get('email.from.address'));

\with(new MailConfigurationUpdater($memory), static function ($updater) use ($name, $email) {
$updater($name, $email);
});

return 0;
}
}
6 changes: 6 additions & 0 deletions src/InstallerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function register()
});

$this->registerRedirection();

if ($this->app->runningInConsole() === true) {
$this->commands([
Console\ConfigureMailCommand::class,
]);
}
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/MailConfigurationUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Installation;

use Illuminate\Support\Arr;
use Orchestra\Contracts\Memory\Provider;

class MailConfigurationUpdater
Expand Down Expand Up @@ -33,7 +34,18 @@ public function __construct(Provider $memory)
*/
public function __invoke(string $siteName, string $email): void
{
$this->memory->put('email', \config('mail'));
$config = \config('mail');

$this->memory->put('email', Arr::only($config, ['driver', 'host', 'port', 'encryption', 'sendmail']));

if ($config['username'] !== null) {
$this->memory->securePut('email.username', $config['username']);
}

if ($config['password'] !== null) {
$this->memory->securePut('email.password', $config['password']);
}

$this->memory->put('email.from', [
'name' => $siteName,
'address' => $email,
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/Console/ConfigureMailCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Orchestra\Installation\Tests\Feature;

use Mockery as m;
use Orchestra\Contracts\Foundation\Foundation;
use Orchestra\Contracts\Memory\Provider as MemoryProvider;
use Orchestra\Installation\Tests\Feature\TestCase;

class ConfigureMailCommandTest extends TestCase
{
/** @test */
public function it_cant_sync_configuration_when_orchestra_is_not_installed()
{
$this->app->instance('orchestra.app', $foundation = m::mock(Foundation::class));

$foundation->shouldReceive('installed')->andReturn(false)
->shouldReceive('memory')->andReturn(m::mock(MemoryProvider::class));

$this->artisan('orchestra:configure-email')
->expectsOutput('This command can only be executed when the application has been installed!')
->assertExitCode(1);
}


/** @test */
public function it_cant_sync_configuration_when_orchestra_is_installed()
{
$this->app->instance('orchestra.app', $foundation = m::mock(Foundation::class));

$foundation->shouldReceive('installed')->andReturn(true)
->shouldReceive('memory')->andReturn($memory = m::mock(MemoryProvider::class));

$memory->shouldReceive('get')->once()->with('email.from.name')->andReturn('Orchestra Platform')
->shouldReceive('get')->once()->with('email.from.address')->andReturn('hello@orchestraplatform.com')
->shouldReceive('put')->once()->with('email', ['driver' => 'smtp', 'host' => 'smtp.mailgun.org', 'port' => 587, 'encryption' => 'tls', 'sendmail' => '/usr/sbin/sendmail -bs'])->andReturnNull()
->shouldReceive('put')->once()->with('email.from', ['name' => 'The Application', 'address' => 'crynobone@gmail.com'])->andReturnNull();

$this->artisan('orchestra:configure-email')
->expectsQuestion('What is the application name?', 'The Application')
->expectsQuestion('What is the e-mail address?', 'crynobone@gmail.com')
->assertExitCode(0);
}
}

0 comments on commit d1b1413

Please sign in to comment.