From 562c3690a8e4c5c2eb594232c77ed3da2fdc9cb7 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 9 Aug 2019 15:36:13 +0800 Subject: [PATCH] Add tests for artisan commands --- app/Console/Commands/BsInstallCommand.php | 12 +++-- app/Console/Commands/SaltRandomCommand.php | 2 +- phpunit.xml | 1 - tests/CommandsTest/BsInstallCommandTest.php | 48 ++++++++++++++++++++ tests/CommandsTest/SaltRandomCommandTest.php | 18 ++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 tests/CommandsTest/BsInstallCommandTest.php create mode 100644 tests/CommandsTest/SaltRandomCommandTest.php diff --git a/app/Console/Commands/BsInstallCommand.php b/app/Console/Commands/BsInstallCommand.php index 022bd6d1..5d44b65e 100644 --- a/app/Console/Commands/BsInstallCommand.php +++ b/app/Console/Commands/BsInstallCommand.php @@ -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('/')]); diff --git a/app/Console/Commands/SaltRandomCommand.php b/app/Console/Commands/SaltRandomCommand.php index 7409c424..69b0139b 100644 --- a/app/Console/Commands/SaltRandomCommand.php +++ b/app/Console/Commands/SaltRandomCommand.php @@ -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')); } } diff --git a/phpunit.xml b/phpunit.xml index 9778c8a5..7f2a9889 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -18,7 +18,6 @@ ./app ./app/Services/Cipher - ./app/Console diff --git a/tests/CommandsTest/BsInstallCommandTest.php b/tests/CommandsTest/BsInstallCommandTest.php new file mode 100644 index 00000000..ca963f63 --- /dev/null +++ b/tests/CommandsTest/BsInstallCommandTest.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/tests/CommandsTest/SaltRandomCommandTest.php b/tests/CommandsTest/SaltRandomCommandTest.php new file mode 100644 index 00000000..f64a71aa --- /dev/null +++ b/tests/CommandsTest/SaltRandomCommandTest.php @@ -0,0 +1,18 @@ +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); + } +}