Add tests for artisan commands

This commit is contained in:
Pig Fang 2019-08-09 15:36:13 +08:00
parent 29ce9d3df1
commit 562c3690a8
5 changed files with 75 additions and 6 deletions

View File

@ -19,11 +19,15 @@ class BsInstallCommand extends Command
return;
}
$this->call('key:generate');
$this->call('salt:random');
$this->call('migrate', ['--force' => true]);
$this->call('jwt:secret', ['--no-interaction' => true]);
$this->call('passport:keys', ['--no-interaction' => true]);
if (! $this->getLaravel()->runningUnitTests()) {
// @codeCoverageIgnoreStart
$this->call('key:generate');
$this->call('salt:random');
$this->call('jwt:secret', ['--no-interaction' => true]);
$this->call('passport:keys', ['--no-interaction' => true]);
// @codeCoverageIgnoreEnd
}
option(['site_url' => url('/')]);

View File

@ -65,6 +65,6 @@ class SaltRandomCommand extends Command
*/
protected function generateRandomSalt()
{
return bin2hex(random_bytes(16));
return bin2hex(resolve(\Illuminate\Contracts\Encryption\Encrypter::class)->generateKey('AES-128-CBC'));
}
}

View File

@ -18,7 +18,6 @@
<directory suffix=".php">./app</directory>
<exclude>
<directory suffix=".php">./app/Services/Cipher</directory>
<directory suffix=".php">./app/Console</directory>
</exclude>
</whitelist>
</filter>

View File

@ -0,0 +1,48 @@
<?php
namespace Tests;
use Schema;
use Artisan;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class BsInstallCommandTest extends TestCase
{
use DatabaseTransactions;
public function testInstallation()
{
$this->artisan('bs:install ibara.mayaka@hyouka.test 12345678 mayaka')
->expectsOutput('You have installed Blessing Skin Server. Nothing to do.');
$tables = [
'user_closet',
'migrations',
'options',
'players',
'textures',
'users',
'reports',
'oauth_auth_codes',
'oauth_access_tokens',
'oauth_clients',
'oauth_personal_access_clients',
'oauth_refresh_tokens',
'notifications',
'jobs',
];
array_walk($tables, function ($table) {
Schema::dropIfExists($table);
});
$this->artisan('bs:install ibara.mayaka@hyouka.test 12345678 mayaka')
->expectsOutput('Installation completed!');
$this->assertEquals(url('/'), option('site_url'));
$user = User::first();
$this->assertEquals('ibara.mayaka@hyouka.test', $user->email);
$this->assertTrue($user->verifyPassword('12345678'));
$this->assertEquals('mayaka', $user->nickname);
$this->assertEquals(User::SUPER_ADMIN, $user->permission);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Tests;
class SaltRandomCommandTest extends TestCase
{
public function testGenerateSalt()
{
$result = bin2hex('deadbeef');
$this->mock(\Illuminate\Contracts\Encryption\Encrypter::class, function ($mock) {
$mock->shouldReceive('generateKey')->with('AES-128-CBC')->twice()->andReturn('deadbeef');
});
$this->artisan('salt:random')
->expectsOutput("Application salt [$result] set successfully.");
$this->artisan('salt:random --show')
->expectsOutput($result);
}
}