blessing-skin-server/tests/MarketControllerTest.php

184 lines
6.0 KiB
PHP
Raw Normal View History

2018-08-19 11:39:14 +08:00
<?php
namespace Tests;
2019-08-13 22:44:32 +08:00
use App\Services\Plugin;
2018-08-19 11:39:14 +08:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2019-08-13 22:44:32 +08:00
use App\Services\PluginManager;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2018-08-19 11:39:14 +08:00
use Tests\Concerns\MocksGuzzleClient;
use GuzzleHttp\Exception\RequestException;
class MarketControllerTest extends TestCase
{
use MocksGuzzleClient;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2018-08-19 11:39:14 +08:00
{
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('superAdmin');
2018-08-19 11:39:14 +08:00
}
public function testDownload()
{
$this->setupGuzzleClientMock();
// Try to download a non-existent plugin
2019-08-13 22:44:32 +08:00
$this->appendToGuzzleQueue(200, [], json_encode([
'version' => 1,
'packages' => [],
]));
2018-08-19 11:39:14 +08:00
$this->postJson('/admin/plugins/market/download', [
'name' => 'non-existent-plugin',
2018-08-19 11:39:14 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('admin.plugins.market.non-existent', ['plugin' => 'non-existent-plugin']),
2018-08-19 11:39:14 +08:00
]);
2019-04-05 17:23:27 +08:00
// Download
2019-08-13 22:44:32 +08:00
$fakeRegistry = json_encode(['packages' => [
[
'name' => 'fake',
'version' => '0.0.0',
'dist' => ['url' => 'http://nowhere.test/', 'shasum' => 'deadbeef'],
],
]]);
2019-04-05 17:23:27 +08:00
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
2019-08-13 22:44:32 +08:00
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')
->withArgs(['http://nowhere.test/', storage_path('packages/fake_0.0.0.zip'), 'deadbeef'])
->once()
->andThrow(new \Exception());
});
2018-08-19 11:39:14 +08:00
$this->postJson('/admin/plugins/market/download', [
2019-08-13 22:44:32 +08:00
'name' => 'fake',
2019-04-23 11:47:45 +08:00
])->assertJson(['code' => 1]);
2018-08-19 11:39:14 +08:00
2019-04-05 17:23:27 +08:00
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
2019-08-13 22:44:32 +08:00
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')
->withArgs(['http://nowhere.test/', storage_path('packages/fake_0.0.0.zip'), 'deadbeef'])
->once()
->andReturnSelf();
$mock->shouldReceive('extract')
->with(base_path('plugins'))
->once();
});
2018-08-19 11:39:14 +08:00
$this->postJson('/admin/plugins/market/download', [
2019-08-13 22:44:32 +08:00
'name' => 'fake',
2019-04-23 11:47:45 +08:00
])->assertJson(['code' => 0, 'message' => trans('admin.plugins.market.install-success')]);
2018-08-19 11:39:14 +08:00
}
public function testCheckUpdates()
{
$this->setupGuzzleClientMock();
2019-08-13 22:44:32 +08:00
$fakeRegistry = json_encode(['packages' => [
[
'name' => 'fake',
'version' => '0.0.1',
'dist' => ['url' => 'http://nowhere.test/', 'shasum' => 'deadbeef'],
],
]]);
2018-08-19 11:39:14 +08:00
// Not installed
2019-08-13 22:44:32 +08:00
$this->appendToGuzzleQueue(200, [], $fakeRegistry);
2018-08-19 11:39:14 +08:00
$this->getJson('/admin/plugins/market/check')
->assertJson([
'available' => false,
'plugins' => [],
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('fake')
->twice()
->andReturn(new Plugin('', ['name' => 'fake', 'version' => '0.0.1']));
});
2018-08-19 11:39:14 +08:00
// Plugin up-to-date
2019-08-13 22:44:32 +08:00
$this->appendToGuzzleQueue(200, [], $fakeRegistry);
2018-08-19 11:39:14 +08:00
$this->getJson('/admin/plugins/market/check')
->assertJson([
'available' => false,
'plugins' => [],
2018-08-19 11:39:14 +08:00
]);
// New version available
2019-08-13 22:44:32 +08:00
$fakeRegistry = json_encode(['packages' => [
[
'name' => 'fake',
'version' => '2.3.3',
'dist' => ['url' => 'http://nowhere.test/', 'shasum' => 'deadbeef'],
],
]]);
$this->appendToGuzzleQueue(200, [], $fakeRegistry);
2018-08-19 11:39:14 +08:00
$this->getJson('/admin/plugins/market/check')
->assertJson([
'available' => true,
'plugins' => [[
2019-08-13 22:44:32 +08:00
'name' => 'fake',
]],
2018-08-19 11:39:14 +08:00
]);
}
public function testMarketData()
{
$this->setupGuzzleClientMock([
new RequestException('Connection Error', new Request('POST', 'whatever')),
2019-08-13 22:44:32 +08:00
new Response(200, [], json_encode(['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' => [],
],
]])),
new Response(200, [], json_encode(['version' => 0])),
2018-08-19 11:39:14 +08:00
]);
// Expected an exception, but unable to be asserted.
$this->getJson('/admin/plugins/market-data');
2019-08-13 22:44:32 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('get')
->with('fake1')
->once()
->andReturn(new Plugin('', ['name' => 'fake1', 'version' => '0.0.1']));
$mock->shouldReceive('get')
->with('fake2')
->once()
->andReturn(null);
$mock->shouldReceive('getUnsatisfied')->twice();
});
2018-08-19 11:39:14 +08:00
$this->getJson('/admin/plugins/market-data')
->assertJsonStructure([
[
'name',
'title',
'version',
'installed',
'description',
'author',
'dist',
'dependencies',
],
2018-08-19 11:39:14 +08:00
]);
2018-08-19 18:38:05 +08:00
2019-08-13 22:44:32 +08:00
$this->getJson('/admin/plugins/market-data')
2019-04-05 17:48:36 +08:00
->assertJson(['message' => 'Only version 1 of market registry is accepted.']);
2018-08-19 11:39:14 +08:00
}
}