Optimize testing

This commit is contained in:
Pig Fang 2019-03-14 00:02:00 +08:00
parent c036e72642
commit d45d287d93
9 changed files with 53 additions and 95 deletions

View File

@ -34,5 +34,5 @@ REDIS_HOST = 127.0.0.1
REDIS_PASSWORD = null
REDIS_PORT = 6379
PLUGINS_DIR = null
PLUGINS_DIR = plugins
PLUGINS_URL = null

View File

@ -23,7 +23,6 @@
"phpunit/phpunit": "~7.0",
"laravel/browser-kit-testing": "~4.0",
"league/flysystem-memory": "^1.0",
"mikey179/vfsstream": "^1.6.5",
"barryvdh/laravel-ide-helper": "^2.6",
"laravel/tinker": "^1.0"
},
@ -46,5 +45,11 @@
},
"config": {
"preferred-install": "dist"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.org/"
}
}
}

48
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "425b8e682534d97b800252cf1ec95152",
"content-hash": "4856faf86e559b07bc74419914ed0a2c",
"packages": [
{
"name": "composer/semver",
@ -3979,52 +3979,6 @@
],
"time": "2016-06-04T03:57:11+00:00"
},
{
"name": "mikey179/vfsStream",
"version": "v1.6.5",
"source": {
"type": "git",
"url": "https://github.com/mikey179/vfsStream.git",
"reference": "d5fec95f541d4d71c4823bb5e30cf9b9e5b96145"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mikey179/vfsStream/zipball/d5fec95f541d4d71c4823bb5e30cf9b9e5b96145",
"reference": "d5fec95f541d4d71c4823bb5e30cf9b9e5b96145",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6.x-dev"
}
},
"autoload": {
"psr-0": {
"org\\bovigo\\vfs\\": "src/main/php"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Frank Kleine",
"homepage": "http://frankkleine.de/",
"role": "Developer"
}
],
"description": "Virtual file system to mock the real file system in unit tests.",
"homepage": "http://vfs.bovigo.org/",
"time": "2017-08-01T08:02:14+00:00"
},
{
"name": "mockery/mockery",
"version": "1.2.2",

View File

@ -92,7 +92,7 @@ trait GeneratesFakePlugins
*/
protected function generateFakePlugin($info)
{
$plugin_dir = base_path("plugins/{$info['name']}");
$plugin_dir = config('plugins.directory').DIRECTORY_SEPARATOR.$info['name'];
if (! is_dir($plugin_dir)) {
mkdir($plugin_dir);

View File

@ -79,8 +79,10 @@ class MarketControllerTest extends TestCase
'errno' => 0,
'msg' => trans('admin.plugins.market.install-success'),
]);
$this->assertTrue(is_dir(base_path('plugins/fake-test-download')));
$this->assertTrue(empty(glob(base_path('plugins/fake-test-download_*.zip'))));
$this->assertTrue(is_dir(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-test-download'));
$this->assertTrue(
empty(glob(config('plugins.directory').DIRECTORY_SEPARATOR.'plugins/fake-test-download_*.zip'))
);
// Broken archive
file_put_contents($fakeArchive, 'broken');
@ -174,9 +176,9 @@ class MarketControllerTest extends TestCase
protected function tearDown(): void
{
// Clean fake plugins
File::deleteDirectory(base_path('plugins/fake-test-download'));
File::deleteDirectory(base_path('plugins/fake-test-update'));
File::delete(base_path('plugins/whatever'));
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-test-download');
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-test-update');
File::delete(config('plugins.directory').DIRECTORY_SEPARATOR.'whatever');
parent::tearDown();
}

View File

@ -142,8 +142,8 @@ class PluginControllerTest extends TestCase
protected function tearDown(): void
{
// Clean fake plugins
File::deleteDirectory(base_path('plugins/fake-plugin-for-test'));
File::deleteDirectory(base_path('plugins/fake-plugin-with-config-view'));
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-for-test');
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'plugins/fake-plugin-with-config-view');
parent::tearDown();
}

View File

@ -38,14 +38,14 @@ class HookTest extends TestCase
public function testRegisterPluginTransScripts()
{
$this->generateFakePlugin(['name' => 'fake-plugin-with-i18n', 'version' => '0.0.1']);
@mkdir($path = base_path('plugins/fake-plugin-with-i18n/lang/en'), 0755, true);
@mkdir($path = config('plugins.directory').DIRECTORY_SEPARATOR.'fake-plugin-with-i18n/lang/en', 0755, true);
file_put_contents("$path/locale.js", '');
Hook::registerPluginTransScripts('fake-plugin-with-i18n');
$this->get('/')
->assertSee('fake-plugin-with-i18n/lang/en/locale.js');
File::deleteDirectory(base_path('plugins/fake-plugin-with-i18n'));
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.'plugins/fake-plugin-with-i18n');
}
public function testAddStyleFileToPage()

View File

@ -3,26 +3,30 @@
namespace Tests;
use App\Services\Minecraft;
use org\bovigo\vfs\vfsStream;
use Illuminate\Http\Testing\FileFactory;
use App\Http\Controllers\TextureController;
class MinecraftTest extends TestCase
{
private $fileFactory;
protected function setUp(): void
{
parent::setUp();
vfsStream::setup();
$this->fileFactory = new FileFactory();
}
public function testGenerateAvatarFromSkin()
{
imagepng(imagecreatetruecolor(64, 32), vfsStream::url('root/skin.png'));
$avatar = Minecraft::generateAvatarFromSkin(file_get_contents(vfsStream::url('root/skin.png')), 50);
$file = $this->fileFactory->image('skin.png');
imagepng(imagecreatetruecolor(64, 32), $file->path());
$avatar = Minecraft::generateAvatarFromSkin(file_get_contents($file->path()), 50);
$this->assertEquals(50, imagesx($avatar));
$this->assertEquals(50, imagesy($avatar));
imagepng(imagecreatetruecolor(128, 64), vfsStream::url('root/skin.png'));
$avatar = Minecraft::generateAvatarFromSkin(file_get_contents(vfsStream::url('root/skin.png')), 50);
imagepng(imagecreatetruecolor(128, 64), $file->path());
$avatar = Minecraft::generateAvatarFromSkin(file_get_contents($file->path()), 50);
$this->assertEquals(50, imagesx($avatar));
$this->assertEquals(50, imagesy($avatar));
@ -35,16 +39,18 @@ class MinecraftTest extends TestCase
public function testGeneratePreviewFromSkin()
{
imagepng(imagecreatetruecolor(64, 32), vfsStream::url('root/skin.png'));
$file = $this->fileFactory->image('skin.png');
imagepng(imagecreatetruecolor(64, 32), $file->path());
$preview = Minecraft::generatePreviewFromSkin(
file_get_contents(vfsStream::url('root/skin.png')), 50, false, 'front'
file_get_contents($file->path()), 50, false, 'front'
);
$this->assertEquals(25, imagesx($preview));
$this->assertEquals(50, imagesy($preview));
imagepng(imagecreatetruecolor(64, 32), vfsStream::url('root/skin.png'));
imagepng(imagecreatetruecolor(64, 32), $file->path());
$preview = Minecraft::generatePreviewFromSkin(
file_get_contents(vfsStream::url('root/skin.png')),
file_get_contents($file->path()),
50,
true, // Alex model
'both',
@ -53,9 +59,9 @@ class MinecraftTest extends TestCase
$this->assertEquals(56, imagesx($preview));
$this->assertEquals(50, imagesy($preview));
imagepng(imagecreatetruecolor(64, 64), vfsStream::url('root/skin.png'));
imagepng(imagecreatetruecolor(64, 64), $file->path());
$preview = Minecraft::generatePreviewFromSkin(
file_get_contents(vfsStream::url('root/skin.png')),
file_get_contents($file->path()),
100,
true, // Alex model
'both',
@ -64,13 +70,13 @@ class MinecraftTest extends TestCase
$this->assertEquals(125, imagesx($preview));
$this->assertEquals(100, imagesy($preview));
imagepng(imagecreatetruecolor(128, 64), vfsStream::url('root/skin.png'));
$preview = Minecraft::generatePreviewFromSkin(file_get_contents(vfsStream::url('root/skin.png')), 50);
imagepng(imagecreatetruecolor(128, 64), $file->path());
$preview = Minecraft::generatePreviewFromSkin(file_get_contents($file->path()), 50);
$this->assertEquals(56, imagesx($preview));
$this->assertEquals(50, imagesy($preview));
imagepng(imagecreatetruecolor(128, 128), vfsStream::url('root/skin.png'));
$preview = Minecraft::generatePreviewFromSkin(file_get_contents(vfsStream::url('root/skin.png')), 50);
imagepng(imagecreatetruecolor(128, 128), $file->path());
$preview = Minecraft::generatePreviewFromSkin(file_get_contents($file->path()), 50);
$this->assertEquals(56, imagesx($preview));
$this->assertEquals(50, imagesy($preview));
@ -86,14 +92,16 @@ class MinecraftTest extends TestCase
public function testGeneratePreviewFromCape()
{
imagepng(imagecreatetruecolor(128, 64), vfsStream::url('root/cape.png'));
$preview = Minecraft::generatePreviewFromCape(file_get_contents(vfsStream::url('root/cape.png')), 64);
$file = $this->fileFactory->image('cape.png');
imagepng(imagecreatetruecolor(128, 64), $file->path());
$preview = Minecraft::generatePreviewFromCape(file_get_contents($file->path()), 64);
$this->assertEquals(40, imagesx($preview));
$this->assertEquals(64, imagesy($preview));
imagepng(imagecreatetruecolor(128, 64), vfsStream::url('root/cape.png'));
imagepng(imagecreatetruecolor(128, 64), $file->path());
$preview = Minecraft::generatePreviewFromCape(
file_get_contents(vfsStream::url('root/cape.png')),
file_get_contents($file->path()),
64,
281,
250

View File

@ -7,7 +7,6 @@ use App\Models\Closet;
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Support\Str;
use org\bovigo\vfs\vfsStream;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -16,17 +15,6 @@ class SkinlibControllerTest extends TestCase
{
use DatabaseTransactions;
/**
* @var \org\bovigo\vfs\vfsStreamDirectory
*/
private $vfs_root;
protected function setUp(): void
{
parent::setUp();
$this->vfs_root = vfsStream::setup();
}
protected function serializeTextures($textures)
{
return $textures
@ -311,6 +299,8 @@ class SkinlibControllerTest extends TestCase
public function testShow()
{
Storage::fake('textures');
// Cannot find texture
$this->get('/skinlib/show/1')
->assertSee(trans('skinlib.show.deleted'));
@ -394,11 +384,10 @@ class SkinlibControllerTest extends TestCase
Storage::fake('textures');
// Some error occurred when uploading file
$file = vfsStream::newFile('test.png')
->at($this->vfs_root);
$file = UploadedFile::fake()->image('test.png');
$upload = new UploadedFile(
$file->url(),
$file->getName(),
$file->path(),
'test.png',
'image/png',
50,
UPLOAD_ERR_NO_TMP_DIR,