Skip to content

Commit

Permalink
添加测试并支持 laravel 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mradang committed Jul 8, 2020
1 parent 1c3d0c1 commit d8b38a1
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "vendor/bin/phpunit"
}
]
}
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@
],
"require": {
"guzzlehttp/guzzle": "^6.5",
"laravel/framework": "~6.0",
"laravel/framework": "~6.0|~7.0",
"gumlet/php-image-resize": "^1.9"
},
"autoload": {
"psr-4": {
"mradang\\LaravelAttachment\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"mradang\\LaravelAttachment\\LaravelAttachmentServiceProvider"
]
}
},
"require-dev": {
"orchestra/testbench": "^5.3",
"phpunit/phpunit": "^9.2"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<directory>vendor/</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>
69 changes: 69 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Tests;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use mradang\LaravelAttachment\Jobs\MakeThumbnail;
use Illuminate\Support\Facades\Storage;

class FeatureTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function testBasicFeatures()
{
$user1 = User::create(['name' => 'user1']);
$this->assertSame(1, $user1->id);

// 下载 URL 并上传
$url = 'https://img.alicdn.com/tfs/TB1_uT8a5ERMeJjSspiXXbZLFXa-143-59.png';
$att1 = $user1->attachmentAddByUrl($url, [
'group' => 'photo',
'name' => 'image1',
'time' => time(),
]);
$this->assertSame('image1', Arr::get($att1->data, 'name'));
$this->assertTrue(Storage::disk($this->app['config']['attachment.disk'])->exists($att1->filename));

// 上传文件
$width = 240;
$height = 160;
$fakeImage = UploadedFile::fake()->image('image2.jpg', $width, $height);
$att2 = $user1->attachmentAddByFile($fakeImage);
$this->assertSame($width, $att2->imageInfo['width']);
$this->assertSame($height, $att2->imageInfo['height']);
$this->assertSame([], $att2->data);
$this->assertTrue(Storage::disk($this->app['config']['attachment.disk'])->exists($att2->filename));

// 现有 2 个附件
$this->assertSame(2, $user1->attachments->count());

// 生成缩略图
$this->expectsJobs(MakeThumbnail::class);
$user1->attachmentShowImage($att1->id, 40, 20);

// 删除第 2 个附件
$user1->attachmentDelete($att2->id);
$this->assertNotTrue(Storage::disk($this->app['config']['attachment.disk'])->exists($att2->filename));
$user1->load('attachments');
$this->assertSame(1, $user1->attachments->count());

// 清空所有附件
$user1->attachmentClear();
$this->assertNotTrue(Storage::disk($this->app['config']['attachment.disk'])->exists($att1->filename));
$user1->load('attachments');
$this->assertSame(0, $user1->attachments->count());

// 自动删除附件
$fakeImage = UploadedFile::fake()->image('image3.jpg');
$att3 = $user1->attachmentAddByFile($fakeImage);
$this->assertSame([], $att3->data);
$this->assertEquals($fakeImage->getSize(), $att3->filesize);
$user1->delete();
$this->assertNotTrue(Storage::disk($this->app['config']['attachment.disk'])->exists($att3->filename));
}
}
43 changes: 43 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests;

use mradang\LaravelAttachment\LaravelAttachmentServiceProvider;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{
/**
* Load package service provider.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [LaravelAttachmentServiceProvider::class];
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

protected function setUp(): void
{
parent::setUp();
$this->loadMigrationsFrom(__DIR__ . '/migrations');
$this->loadMigrationsFrom(dirname(__DIR__) . '/migrations');
}
}
13 changes: 13 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests;

use Illuminate\Database\Eloquent\Model;
use mradang\LaravelAttachment\Traits\AttachmentTrait;

class User extends Model
{
use AttachmentTrait;

protected $fillable = ['name'];
}
32 changes: 32 additions & 0 deletions tests/migrations/2020_07_06_222114_create_users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

0 comments on commit d8b38a1

Please sign in to comment.