mc-blessing-skin/tests/HttpTest/ControllersTest/MarketControllerTest.php

163 lines
5 KiB
PHP
Raw Permalink Normal View History

2018-08-19 11:39:14 +08:00
<?php
namespace Tests;
2020-10-14 11:56:34 +08:00
use App\Models\User;
2019-08-13 22:44:32 +08:00
use App\Services\Plugin;
2019-12-14 11:10:37 +08:00
use App\Services\PluginManager;
2020-01-14 10:58:20 +08:00
use App\Services\Unzip;
use Illuminate\Support\Facades\Http;
2018-08-19 11:39:14 +08:00
class MarketControllerTest extends TestCase
{
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2018-08-19 11:39:14 +08:00
{
parent::setUp();
2020-10-14 11:56:34 +08:00
$this->actingAs(User::factory()->superAdmin()->create());
2018-08-19 11:39:14 +08:00
}
public function testDownload()
{
2020-04-07 16:09:30 +08:00
$registryUrl = str_replace('{lang}', 'en', config('plugins.registry'));
Http::fake([
2020-04-07 16:09:30 +08:00
$registryUrl => Http::sequence()
->push(['version' => 1, 'packages' => []])
->push([
'version' => 1,
'packages' => [
[
'name' => 'fake',
'version' => '0.0.0',
'require' => ['a' => '^4.0.0'],
],
],
])
->whenEmpty([
'version' => 1,
'packages' => [
[
'name' => 'fake',
'version' => '0.0.0',
'dist' => [
'url' => 'http://nowhere.test/',
'shasum' => 'deadbeef',
],
],
],
]),
'http://nowhere.test/' => Http::sequence()
->pushStatus(404)
->pushStatus(200),
]);
2018-08-19 11:39:14 +08:00
2020-01-14 11:37:21 +08:00
$this->postJson('/admin/plugins/market/download', ['name' => 'nope'])
->assertJson([
'code' => 1,
'message' => trans('admin.plugins.market.non-existent', ['plugin' => 'nope']),
]);
2018-08-19 11:39:14 +08:00
// Unresolved plugin.
$this->postJson('/admin/plugins/market/download', ['name' => 'fake'])
->assertJson([
'message' => trans('admin.plugins.market.unresolved'),
'code' => 1,
'data' => [
'reason' => [
trans('admin.plugins.operations.unsatisfied.disabled', ['name' => 'a']),
],
],
]);
2019-04-05 17:23:27 +08:00
// Download
$this->postJson('/admin/plugins/market/download', ['name' => 'fake'])
->assertJson(['code' => 1]);
2018-08-19 11:39:14 +08:00
2020-01-14 10:58:20 +08:00
$this->mock(Unzip::class, function ($mock) {
$mock->shouldReceive('extract')->once();
2019-08-13 22:44:32 +08:00
});
$this->postJson('/admin/plugins/market/download', ['name' => 'fake'])
->assertJson([
'code' => 0,
2019-12-14 11:10:37 +08:00
'message' => trans('admin.plugins.market.install-success'),
]);
2018-08-19 11:39:14 +08:00
}
public function testMarketData()
{
2020-04-07 16:09:30 +08:00
$registry = [
'version' => 1,
'packages' => [
[
'name' => 'fake1',
'title' => 'Fake',
'version' => '1.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
[
'name' => 'fake2',
'title' => 'Fake',
'version' => '0.0.0',
'description' => '',
'author' => '',
'dist' => [],
'require' => [],
],
],
];
Http::fakeSequence()
->pushStatus(404)
2020-04-07 16:09:30 +08:00
->push($registry)
->push($registry);
2018-08-19 11:39:14 +08:00
2020-01-14 11:37:21 +08:00
$this->getJson('/admin/plugins/market/list')->assertStatus(500);
2018-08-19 11:39:14 +08:00
2019-08-13 22:44:32 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('get')
->with('fake1')
2020-04-07 16:09:30 +08:00
->atLeast()
2019-08-13 22:44:32 +08:00
->once()
->andReturn(new Plugin('', ['name' => 'fake1', 'version' => '0.0.1']));
$mock->shouldReceive('get')
->with('fake2')
2020-04-07 16:09:30 +08:00
->atLeast()
2019-08-13 22:44:32 +08:00
->once()
->andReturn(null);
2020-04-07 16:09:30 +08:00
$mock->shouldReceive('getUnsatisfied')->atLeast()->once();
2019-08-13 22:44:32 +08:00
});
2020-01-13 08:37:57 +08:00
$this->getJson('/admin/plugins/market/list')
2018-08-19 11:39:14 +08:00
->assertJsonStructure([
[
'name',
'title',
'version',
'installed',
'description',
'author',
'dist',
'dependencies',
],
2018-08-19 11:39:14 +08:00
]);
2020-04-07 16:09:30 +08:00
// with fallback locale
app()->setLocale('es_ES');
$this->getJson('/admin/plugins/market/list')
->assertJsonStructure([
[
'name',
'title',
'version',
'installed',
'description',
'author',
'dist',
'dependencies',
],
]);
app()->setLocale('en');
2018-08-19 11:39:14 +08:00
}
}