blessing-skin-server/tests/Concerns/GeneratesFakePlugins.php

146 lines
4.1 KiB
PHP
Raw Normal View History

2018-08-19 11:39:14 +08:00
<?php
namespace Tests\Concerns;
use ZipArchive;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
2018-08-19 11:39:14 +08:00
trait GeneratesFakePlugins
{
/**
* Generate fake content of a plugin's package.json.
*
* @param array $info
* @return array
*/
protected function generateFakePlguinInfo($info = [])
{
return array_replace([
2019-02-27 23:44:50 +08:00
'name' => Str::random(10),
2018-08-19 11:39:14 +08:00
'version' => '0.0.'.rand(1, 9),
2019-02-27 23:44:50 +08:00
'title' => Str::random(20),
'description' => Str::random(60),
'author' => Arr::get($info, 'author', Str::random(10)),
'url' => 'https://'.Str::random(10).'.test',
'namespace' => Str::random(10),
2018-08-19 11:39:14 +08:00
'require' => [
'blessing-skin-server' => '^3.4.0 || ^4.0.0',
],
2018-08-19 11:39:14 +08:00
], $info);
}
/**
* Generate plugin information for plugins registry (with "dist" field).
*
* @param array $info
* @return array
*/
protected function generateFakePluginsRegistryPackage($info = [])
{
return $this->generateFakePlguinInfo(array_replace([
'dist' => [
'type' => 'zip',
2019-02-27 23:44:50 +08:00
'url' => 'https://plugins-registry.test/'.Str::random(10).'.zip',
'shasum' => strtolower(Str::random(40)),
],
2018-08-19 11:39:14 +08:00
], $info));
}
/**
* Generate fake content of a plugins registry.
* You can also pass two arguments (name and version) as a shortcut.
* If no argument is passed, we will randomly generate 10 fake plugins.
*
* @param array $plugins An array of plugin information.
* @return string JSON encoded content.
*/
protected function generateFakePluginsRegistry($plugins = [])
{
if (func_num_args() == 2) {
$plugins = [
[
'name' => func_get_arg(0),
'version' => func_get_arg(1),
],
2018-08-19 11:39:14 +08:00
];
}
$packages = [];
if (count($plugins) == 0) {
// Randomly generate 10 fake plugins
for ($i = 0; $i < 10; $i++) {
$packages[] = $this->generateFakePluginsRegistryPackage();
}
} else {
foreach ($plugins as $info) {
$packages[] = $this->generateFakePluginsRegistryPackage($info);
}
}
return json_encode([
'packages' => $packages,
2018-08-19 11:39:14 +08:00
]);
}
/**
* Generate a fake plugin in plugins directory with given information.
*
* @param array $info The "name" field is required.
* @return void
*/
protected function generateFakePlugin($info)
{
2019-03-14 00:02:00 +08:00
$plugin_dir = config('plugins.directory').DIRECTORY_SEPARATOR.$info['name'];
2018-08-19 11:39:14 +08:00
if (! is_dir($plugin_dir)) {
mkdir($plugin_dir);
}
// Generate fake config view
2019-02-27 23:44:50 +08:00
if ($config = Arr::get($info, 'config')) {
2018-08-19 11:39:14 +08:00
$views_path = "$plugin_dir/views";
if (! is_dir($views_path)) {
mkdir($views_path);
}
2019-02-27 23:44:50 +08:00
file_put_contents("$views_path/$config", Str::random(64));
2018-08-19 11:39:14 +08:00
}
file_put_contents("$plugin_dir/package.json", json_encode(
$this->generateFakePlguinInfo($info)
));
file_put_contents("$plugin_dir/bootstrap.php", "<?php return function () { return '{$info['name']}'; };");
}
/**
* Generate a fake zip archive of given plugin.
*
* @param array $info Plugin information.
* @return string File path of generated zip archive.
*/
protected function generateFakePluginArchive($info)
{
2019-02-27 23:44:50 +08:00
$name = Arr::get($info, 'name');
$version = Arr::get($info, 'version');
2018-08-19 11:39:14 +08:00
$zipPath = storage_path("testing/{$name}_{$version}.zip");
if (file_exists($zipPath)) {
unlink($zipPath);
}
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
$zip->addEmptyDir($name);
$zip->addFromString("$name/package.json", json_encode(
$this->generateFakePlguinInfo($info)
));
$zip->close();
return $zipPath;
}
}