blessing-skin-server/tests/ServicesTest/PluginManagerTest.php

320 lines
11 KiB
PHP
Raw Normal View History

2019-08-11 18:00:00 +08:00
<?php
namespace Tests;
2019-08-12 15:21:50 +08:00
use Event;
2019-08-11 18:00:00 +08:00
use ReflectionClass;
2019-08-12 15:59:01 +08:00
use App\Services\Plugin;
2019-08-11 18:00:00 +08:00
use App\Services\PluginManager;
use Illuminate\Filesystem\Filesystem;
class PluginManagerTest extends TestCase
{
public function rebootPluginManager(PluginManager $manager)
{
$reflection = new ReflectionClass($manager);
$property = $reflection->getProperty('booted');
$property->setAccessible(true);
$property->setValue($manager, false);
$manager->boot();
return $manager;
}
2019-08-11 19:10:27 +08:00
public function testPreventBootingAgain()
2019-08-11 18:00:00 +08:00
{
// TODO: modify asserting 0 times here
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')->times(1);
});
app('plugins')->boot();
app('plugins')->boot();
}
2019-08-12 10:52:40 +08:00
public function testDoNotLoadDisabled()
{
$dir = config('plugins.directory');
config(['plugins.directory' => storage_path('mocks')]);
$manager = $this->rebootPluginManager(app('plugins'));
$this->assertFalse(class_exists('Fake\Faker'));
config(['plugins.directory' => $dir]);
}
2019-08-11 18:00:00 +08:00
public function testReportDuplicatedPlugins()
{
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
2019-08-12 09:56:51 +08:00
->with(base_path('plugins'))
2019-08-11 18:00:00 +08:00
->once()
->andReturn(collect(['/nano', '/yuko']));
$mock->shouldReceive('exists')
2019-08-12 09:56:51 +08:00
->with('/nano'.DIRECTORY_SEPARATOR.'package.json')
2019-08-11 18:00:00 +08:00
->once()
->andReturn(true);
$mock->shouldReceive('get')
2019-08-12 09:56:51 +08:00
->with('/nano'.DIRECTORY_SEPARATOR.'package.json')
2019-08-11 18:00:00 +08:00
->once()
->andReturn(json_encode([
'name' => 'fake',
'version' => '0.0.0',
]));
$mock->shouldReceive('exists')
2019-08-12 09:56:51 +08:00
->with('/yuko'.DIRECTORY_SEPARATOR.'package.json')
2019-08-11 18:00:00 +08:00
->once()
->andReturn(true);
$mock->shouldReceive('get')
2019-08-12 09:56:51 +08:00
->with('/yuko'.DIRECTORY_SEPARATOR.'package.json')
2019-08-11 18:00:00 +08:00
->once()
->andReturn(json_encode([
'name' => 'fake',
'version' => '0.0.0',
]));
});
$this->expectExceptionMessage(trans('errors.plugins.duplicate', [
'dir1' => '/nano',
'dir2' => '/yuko',
]));
$manager = $this->rebootPluginManager(app('plugins'));
}
2019-08-11 19:10:27 +08:00
2019-08-12 15:21:50 +08:00
public function testDetectVersionChanged()
{
option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]);
Event::fake();
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with(base_path('plugins'))
->once()
->andReturn(collect(['/mayaka']));
$mock->shouldReceive('exists')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(true);
$mock->shouldReceive('get')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(json_encode([
'name' => 'mayaka',
'version' => '0.1.0',
]));
$mock->shouldReceive('exists')
->with('/mayaka/vendor/autoload.php')
->once()
->andReturn(false);
$mock->shouldReceive('exists')
->with('/mayaka/bootstrap.php')
->once()
->andReturn(false);
});
$manager = $this->rebootPluginManager(app('plugins'));
Event::assertDispatched(\App\Events\PluginVersionChanged::class, function ($event) {
$this->assertEquals('0.1.0', $event->plugin->version);
return true;
});
option(['plugins_enabled' => '[]']);
}
2019-08-12 14:35:36 +08:00
public function testLoadComposer()
{
option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]);
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with(base_path('plugins'))
->once()
->andReturn(collect(['/mayaka']));
$mock->shouldReceive('exists')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(true);
$mock->shouldReceive('get')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(json_encode([
'name' => 'mayaka',
'version' => '0.0.0',
]));
$mock->shouldReceive('exists')
->with('/mayaka/vendor/autoload.php')
->once()
->andReturn(true);
$mock->shouldReceive('getRequire')
->with('/mayaka/vendor/autoload.php')
->once();
$mock->shouldReceive('exists')
->with('/mayaka/bootstrap.php')
->once()
->andReturn(false);
});
$manager = $this->rebootPluginManager(app('plugins'));
option(['plugins_enabled' => '[]']);
}
public function testLoadViewsAndTranslations()
{
option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]);
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with(base_path('plugins'))
->once()
->andReturn(collect(['/mayaka']));
$mock->shouldReceive('exists')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(true);
$mock->shouldReceive('get')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(json_encode([
'name' => 'mayaka',
'version' => '0.0.0',
'namespace' => 'Mayaka',
]));
$mock->shouldReceive('exists')
->with('/mayaka/vendor/autoload.php')
->once()
->andReturn(false);
$mock->shouldReceive('exists')
->with('/mayaka/bootstrap.php')
->once()
->andReturn(false);
});
$this->mock('view', function ($mock) {
$mock->shouldReceive('addNamespace')
->withArgs(['Mayaka', '/mayaka/views'])
->once();
});
$this->instance('translation.loader', \Mockery::mock(\App\Services\TranslationLoader::class, function ($mock) {
$mock->shouldReceive('addNamespace')
->withArgs(['Mayaka', '/mayaka/lang'])
->once();
}));
$manager = $this->rebootPluginManager(app('plugins'));
option(['plugins_enabled' => '[]']);
}
public function testLoadBootstrapper()
{
option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]);
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with(base_path('plugins'))
->once()
->andReturn(collect(['/mayaka']));
$mock->shouldReceive('exists')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(true);
$mock->shouldReceive('get')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(json_encode([
'name' => 'mayaka',
'version' => '0.0.0',
]));
$mock->shouldReceive('exists')
->with('/mayaka/vendor/autoload.php')
->once()
->andReturn(false);
$mock->shouldReceive('exists')
->with('/mayaka/bootstrap.php')
->once()
->andReturn(true);
$mock->shouldReceive('getRequire')
->with('/mayaka/bootstrap.php')
->once()
->andReturn(function (\Illuminate\Contracts\Events\Dispatcher $events) {
$this->assertTrue(method_exists($events, 'listen'));
});
});
$manager = $this->rebootPluginManager(app('plugins'));
option(['plugins_enabled' => '[]']);
}
2019-08-12 15:59:01 +08:00
public function testLifecycleHooks()
{
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with(base_path('plugins'))
->once()
->andReturn(collect(['/mayaka']));
$mock->shouldReceive('exists')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(true);
$mock->shouldReceive('get')
->with('/mayaka'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(json_encode([
'name' => 'mayaka',
'version' => '0.0.0',
'namespace' => 'Mayaka',
]));
$mock->shouldReceive('exists')
->with('/mayaka/callbacks.php')
->once()
->andReturn(true);
$mock->shouldReceive('getRequire')
->with('/mayaka/callbacks.php')
->once()
->andReturn([
\App\Events\PluginWasDeleted::class => function ($plugin) {
$this->assertInstanceOf(Plugin::class, $plugin);
$this->assertEquals('mayaka', $plugin->name);
},
]);
});
$manager = $this->rebootPluginManager(app('plugins'));
event(new \App\Events\PluginWasDeleted(new Plugin('/mayaka', ['name' => 'mayaka'])));
}
2019-08-11 19:10:27 +08:00
public function testRegisterAutoload()
{
$dir = config('plugins.directory');
config(['plugins.directory' => storage_path('mocks')]);
2019-08-12 10:52:40 +08:00
option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]);
2019-08-11 19:10:27 +08:00
$this->assertFalse(class_exists('Fake\Faker'));
$manager = $this->rebootPluginManager(app('plugins'));
$this->assertTrue(class_exists('Fake\Faker'));
config(['plugins.directory' => $dir]);
2019-08-12 10:52:40 +08:00
option(['plugins_enabled' => '[]']);
2019-08-11 19:10:27 +08:00
}
2019-08-11 18:00:00 +08:00
}