From 65d82fba645899acee5c36f2fc50917d23086b37 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Tue, 10 Dec 2019 23:40:32 +0800 Subject: [PATCH] Refactor --- app/Http/Controllers/SetupController.php | 21 -------- app/Http/Controllers/UpdateController.php | 23 +++++++++ routes/web.php | 2 +- .../ControllersTest/SetupControllerTest.php | 43 ----------------- .../ControllersTest/UpdateControllerTest.php | 48 +++++++++++++++++++ 5 files changed, 72 insertions(+), 65 deletions(-) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 762f2943..5a35b96c 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -10,7 +10,6 @@ use Illuminate\Database\Connection; use Illuminate\Filesystem\Filesystem; use App\Exceptions\PrettyPageException; use Illuminate\Database\DatabaseManager; -use Symfony\Component\Finder\SplFileInfo; use Illuminate\Contracts\Console\Kernel as Artisan; class SetupController extends Controller @@ -165,24 +164,4 @@ class SetupController extends Controller 'email' => $request->input('email'), ]); } - - public function update(Filesystem $filesystem, Artisan $artisan) - { - collect($filesystem->files(database_path('update_scripts'))) - ->filter(function (SplFileInfo $file) { - $name = $file->getFilenameWithoutExtension(); - - return preg_match('/^\d+\.\d+\.\d+$/', $name) > 0 - && Comparator::greaterThanOrEqualTo($name, option('version')); - }) - ->each(function (SplFileInfo $file) use ($filesystem) { - $filesystem->getRequire($file->getPathname()); - }); - - option(['version' => config('app.version')]); - $artisan->call('view:clear'); - $filesystem->put(storage_path('install.lock'), ''); - - return view('setup.updates.success'); - } } diff --git a/app/Http/Controllers/UpdateController.php b/app/Http/Controllers/UpdateController.php index a4801cc4..d0dc5580 100644 --- a/app/Http/Controllers/UpdateController.php +++ b/app/Http/Controllers/UpdateController.php @@ -8,6 +8,8 @@ use Illuminate\Http\Request; use Composer\Semver\Comparator; use App\Services\PackageManager; use Illuminate\Filesystem\Filesystem; +use Symfony\Component\Finder\SplFileInfo; +use Illuminate\Contracts\Console\Kernel as Artisan; class UpdateController extends Controller { @@ -69,6 +71,27 @@ class UpdateController extends Controller } } + public function update(Filesystem $filesystem, Artisan $artisan) + { + collect($filesystem->files(database_path('update_scripts'))) + ->filter(function (SplFileInfo $file) { + $name = $file->getFilenameWithoutExtension(); + + return preg_match('/^\d+\.\d+\.\d+$/', $name) > 0 + && Comparator::greaterThanOrEqualTo($name, option('version')); + }) + ->each(function (SplFileInfo $file) use ($filesystem) { + $filesystem->getRequire($file->getPathname()); + }); + + option(['version' => config('app.version')]); + $artisan->call('migrate', ['--force' => true]); + $artisan->call('view:clear'); + $filesystem->put(storage_path('install.lock'), ''); + + return view('setup.updates.success'); + } + protected function getUpdateInfo() { $acceptableSpec = 2; diff --git a/routes/web.php b/routes/web.php index 11b897bd..7ac356b0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -179,6 +179,6 @@ Route::group(['prefix' => 'setup'], function () { Route::group(['middleware' => 'authorize'], function () { Route::view('/update', 'setup.updates.welcome')->middleware('setup'); - Route::any('/exec-update', 'SetupController@update')->middleware('setup'); + Route::any('/exec-update', 'UpdateController@update')->middleware('setup'); }); }); diff --git a/tests/HttpTest/ControllersTest/SetupControllerTest.php b/tests/HttpTest/ControllersTest/SetupControllerTest.php index 7a2e99a6..ea11bc8a 100644 --- a/tests/HttpTest/ControllersTest/SetupControllerTest.php +++ b/tests/HttpTest/ControllersTest/SetupControllerTest.php @@ -4,7 +4,6 @@ namespace Tests; use Illuminate\Support\Str; use Illuminate\Filesystem\Filesystem; -use Symfony\Component\Finder\SplFileInfo; use Illuminate\Contracts\Console\Kernel as Artisan; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -192,46 +191,4 @@ class SetupControllerTest extends TestCase $this->assertEquals('nickname', $superAdmin->nickname); $this->assertEquals('bs', option('site_name')); } - - public function testUpdate() - { - $this->mock(Filesystem::class, function ($mock) { - $mock->shouldReceive('exists') - ->with(storage_path('install.lock')) - ->andReturn(true); - - $mock->shouldReceive('put') - ->with(storage_path('install.lock'), '') - ->once() - ->andReturn(true); - - $mock->shouldReceive('files') - ->with(database_path('update_scripts')) - ->once() - ->andReturn([ - new SplFileInfo('/1.0.0.php', '', ''), - new SplFileInfo('/99.0.0.php', '', ''), - new SplFileInfo('/100.0.0.php', '', ''), - ]); - - $mock->shouldNotReceive('getRequire')->with('/1.0.0.php'); - - $mock->shouldReceive('getRequire') - ->with('/99.0.0.php') - ->once(); - - $mock->shouldReceive('getRequire') - ->with('/100.0.0.php') - ->once(); - }); - $this->spy(Artisan::class, function ($spy) { - $spy->shouldReceive('call')->with('view:clear')->once(); - }); - config(['app.version' => '100.0.0']); - - $this->actAs('superAdmin') - ->get('/setup/exec-update') - ->assertViewIs('setup.updates.success'); - $this->assertEquals('100.0.0', option('version')); - } } diff --git a/tests/HttpTest/ControllersTest/UpdateControllerTest.php b/tests/HttpTest/ControllersTest/UpdateControllerTest.php index ae9f837f..fcc3deb8 100644 --- a/tests/HttpTest/ControllersTest/UpdateControllerTest.php +++ b/tests/HttpTest/ControllersTest/UpdateControllerTest.php @@ -5,8 +5,11 @@ namespace Tests; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use App\Services\PackageManager; +use Illuminate\Filesystem\Filesystem; use Tests\Concerns\MocksGuzzleClient; +use Symfony\Component\Finder\SplFileInfo; use GuzzleHttp\Exception\RequestException; +use Illuminate\Contracts\Console\Kernel as Artisan; use Illuminate\Foundation\Testing\DatabaseTransactions; class UpdateControllerTest extends TestCase @@ -109,6 +112,51 @@ class UpdateControllerTest extends TestCase ]); } + public function testUpdate() + { + $this->mock(Filesystem::class, function ($mock) { + $mock->shouldReceive('exists') + ->with(storage_path('install.lock')) + ->andReturn(true); + + $mock->shouldReceive('put') + ->with(storage_path('install.lock'), '') + ->once() + ->andReturn(true); + + $mock->shouldReceive('files') + ->with(database_path('update_scripts')) + ->once() + ->andReturn([ + new SplFileInfo('/1.0.0.php', '', ''), + new SplFileInfo('/99.0.0.php', '', ''), + new SplFileInfo('/100.0.0.php', '', ''), + ]); + + $mock->shouldNotReceive('getRequire')->with('/1.0.0.php'); + + $mock->shouldReceive('getRequire') + ->with('/99.0.0.php') + ->once(); + + $mock->shouldReceive('getRequire') + ->with('/100.0.0.php') + ->once(); + }); + $this->spy(Artisan::class, function ($spy) { + $spy->shouldReceive('call') + ->with('migrate', ['--force' => true]) + ->once(); + $spy->shouldReceive('call')->with('view:clear')->once(); + }); + config(['app.version' => '100.0.0']); + + $this->actAs('superAdmin') + ->get('/setup/exec-update') + ->assertViewIs('setup.updates.success'); + $this->assertEquals('100.0.0', option('version')); + } + protected function mockFakeUpdateInfo(string $version, $extra = []) { return json_encode(array_merge([