Skip to content

Commit

Permalink
Merge pull request #46 from zingimmick/generate-env-tests
Browse files Browse the repository at this point in the history
Environment tests improvement
  • Loading branch information
zingimmick authored Jun 15, 2020
2 parents ca93963 + dfd4ee4 commit df74e51
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 14 deletions.
14 changes: 0 additions & 14 deletions tests/Concerns/ServiceProviderTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

namespace Zing\LaravelSms\Tests\Concerns;

use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Illuminate\Support\Facades\Log;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Zing\LaravelSms\Commands\SmsSwitchConnectionCommand;
use Zing\LaravelSms\Connectors\Connector;
use Zing\LaravelSms\Facades\Sms;
use Zing\LaravelSms\SmsMessage;

trait ServiceProviderTests
{
use InteractsWithConsole;

public function testSms(): void
{
$this->assertInstanceOf(Connector::class, Sms::connection());
Expand All @@ -37,14 +33,4 @@ public function testSendWithAlias(): void
Log::shouldReceive('debug')->withAnyArgs()->twice();
\Sms::connection('null')->send($number, $message, new Config());
}

public function testCommand(): void
{
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
}
}
97 changes: 97 additions & 0 deletions tests/Laravel/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Zing\LaravelSms\Tests\Laravel;

use Zing\LaravelSms\Commands\SmsSwitchConnectionCommand;

class CommandTest extends LaravelTestCase
{
public function testCommand(): void
{
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
'--show' => 1,
]
)->assertExitCode(0);
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)
->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', false)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', true)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default-2');
}

public function testAlwaysNo(): void
{
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
'--always-no' => 1,
]
)->expectsOutput('Sms default connection already exists. Skipping...');
self::assertSame(config('sms.default'), 'default');
}

protected function envPath()
{
if (method_exists($this->app, 'environmentFilePath')) {
return $this->app->environmentFilePath();
}

return $this->app->basePath('.env');
}

protected function setUp(): void
{
parent::setUp();
$this->connection = \config('sms.default');
}

protected $connection;

protected function tearDown(): void
{
if (file_exists($this->envPath())) {
unlink($this->envPath());
}

\config(['sms.default' => $this->connection]);
parent::tearDown();
}
}
99 changes: 99 additions & 0 deletions tests/Lumen/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Zing\LaravelSms\Tests\Lumen;

use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Zing\LaravelSms\Commands\SmsSwitchConnectionCommand;

class CommandTest extends LumenTestCase
{
use InteractsWithConsole;

public function testCommand(): void
{
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
'--show' => 1,
]
)->assertExitCode(0);
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', false)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', true)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default-2');
}

public function testAlwaysNo(): void
{
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
'--always-no' => 1,
]
)->expectsOutput('Sms default connection already exists. Skipping...');
self::assertSame(config('sms.default'), 'default');
}

protected function envPath()
{
if (method_exists($this->app, 'environmentFilePath')) {
return $this->app->environmentFilePath();
}

return $this->app->basePath('.env');
}

protected function setUp(): void
{
parent::setUp();
$this->connection = \config('sms.default');
}

protected $connection;

protected function tearDown(): void
{
if (file_exists($this->envPath())) {
unlink($this->envPath());
}

\config(['sms.default' => $this->connection]);
parent::tearDown();
}
}

0 comments on commit df74e51

Please sign in to comment.