blessing-skin-server/tests/HttpTest/ControllersTest/UpdateControllerTest.php

143 lines
4.8 KiB
PHP
Raw Normal View History

2017-11-18 20:36:31 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2020-01-14 10:58:20 +08:00
use App\Services\Unzip;
2018-08-18 09:48:39 +08:00
use GuzzleHttp\Exception\RequestException;
2019-12-14 11:10:37 +08:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2019-12-10 23:40:32 +08:00
use Illuminate\Contracts\Console\Kernel as Artisan;
2019-12-14 11:10:37 +08:00
use Illuminate\Filesystem\Filesystem;
2017-11-18 20:36:31 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-12-14 11:10:37 +08:00
use Symfony\Component\Finder\SplFileInfo;
use Tests\Concerns\MocksGuzzleClient;
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
class UpdateControllerTest extends TestCase
2017-11-18 20:36:31 +08:00
{
use DatabaseTransactions;
2018-08-18 09:48:39 +08:00
use MocksGuzzleClient;
2017-11-18 20:36:31 +08:00
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-18 20:36:31 +08:00
{
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('superAdmin');
2017-11-18 20:36:31 +08:00
}
public function testShowUpdatePage()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
// Can't connect to update source
$this->appendToGuzzleQueue([
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
$this->get('/admin/update')->assertSee(config('app.version'));
2019-04-06 22:52:43 +08:00
// Missing `spec` field
$this->appendToGuzzleQueue([
2019-07-05 14:48:12 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3', ['spec' => 0])),
2019-04-06 22:52:43 +08:00
]);
2019-07-05 14:48:12 +08:00
$this->get('/admin/update')->assertSee(trans('admin.update.errors.spec'));
// Low PHP version
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3', ['php' => '100.0.0'])),
]);
$this->get('/admin/update')->assertSee(trans('admin.update.errors.php', ['version' => '100.0.0']));
2019-04-06 22:52:43 +08:00
2018-08-18 09:48:39 +08:00
// New version available
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
]);
2019-04-05 22:53:07 +08:00
$this->get('/admin/update')->assertSee(config('app.version'))->assertSee('8.9.3');
2017-11-18 20:36:31 +08:00
}
public function testDownload()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Already up-to-date
2020-01-06 22:47:29 +08:00
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('1.2.3')),
]);
2020-01-13 08:37:57 +08:00
$this->postJson('/admin/update/download')
2020-01-06 22:47:29 +08:00
->assertJson([
'code' => 1,
'message' => trans('admin.update.info.up-to-date'),
]);
2018-08-18 09:48:39 +08:00
2019-04-05 17:23:27 +08:00
// Download
2018-08-18 09:48:39 +08:00
$this->appendToGuzzleQueue([
2019-04-06 22:52:43 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
2020-01-14 10:58:20 +08:00
new Response(404),
2019-04-06 22:52:43 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
2020-01-14 10:58:20 +08:00
new Response(200),
2018-08-18 09:48:39 +08:00
]);
2020-01-13 08:37:57 +08:00
$this->postJson('/admin/update/download')->assertJson(['code' => 1]);
2020-01-14 10:58:20 +08:00
$this->mock(Unzip::class, function ($mock) {
$mock->shouldReceive('extract')->once()->andReturn();
2019-08-22 09:11:04 +08:00
});
2019-08-26 11:01:49 +08:00
$this->mock(\Illuminate\Filesystem\Filesystem::class, function ($mock) {
2019-12-23 23:28:46 +08:00
$mock->shouldReceive('delete')->with(storage_path('options.php'))->once();
2019-09-06 23:53:47 +08:00
$mock->shouldReceive('exists')->with(storage_path('install.lock'))->andReturn(true);
2019-08-26 11:01:49 +08:00
});
2020-01-13 08:37:57 +08:00
$this->postJson('/admin/update/download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]);
2017-11-18 20:36:31 +08:00
}
2018-07-22 10:50:01 +08:00
2019-12-10 23:40:32 +08:00
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'));
}
2019-07-05 14:48:12 +08:00
protected function mockFakeUpdateInfo(string $version, $extra = [])
2018-08-18 09:48:39 +08:00
{
2019-07-05 14:48:12 +08:00
return json_encode(array_merge([
'spec' => 2,
'php' => '7.2.0',
2019-04-06 22:52:43 +08:00
'latest' => $version,
'url' => "https://whatever.test/$version/update.zip",
2019-07-05 14:48:12 +08:00
], $extra));
2018-08-18 09:48:39 +08:00
}
2017-11-18 20:36:31 +08:00
}