-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |