forked from laravel-enso/files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTest.php
163 lines (124 loc) · 4.18 KB
/
FileTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use LaravelEnso\Files\Contracts\Attachable;
use LaravelEnso\Files\Contracts\Extensions;
use LaravelEnso\Files\Contracts\MimeTypes;
use LaravelEnso\Files\Exceptions\File as Exception;
use LaravelEnso\Files\Models\File;
use LaravelEnso\Users\Models\User;
use Tests\TestCase;
class FileTest extends TestCase
{
use RefreshDatabase;
private AttachableModel $model;
private UploadedFile $file;
protected function setUp(): void
{
parent::setUp();
$this->seed()
->actingAs(User::first());
$this->file = UploadedFile::fake()->image('picture.png');
$this->createTestTable();
$this->model = AttachableModel::create();
}
public function tearDown(): void
{
$this->cleanUp();
parent::tearDown();
}
/** @test */
public function can_upload_file()
{
$file = File::upload($this->model, $this->file);
$this->model->file()->associate($file)->save();
$this->assertNotNull($this->model->file);
Storage::assertExists($this->model->file->path());
}
/** @test */
public function can_attach_file()
{
$folder = Config::get('enso.files.testingFolder');
$filename = 'test.txt';
Storage::put("{$folder}/{$filename}", 'test');
$file = File::attach($this->model, $filename, $filename);
$this->model->file()->associate($file)->save();
$this->assertNotNull($this->model->file);
Storage::assertExists($this->model->file->path());
}
/** @test */
public function cant_upload_file_with_invalid_extension()
{
$file = UploadedFile::fake()->image('image.jpg');
$this->expectException(Exception::class);
$this->expectExceptionMessage(
Exception::invalidExtension($file->getClientOriginalExtension(), 'png')
->getMessage()
);
File::upload($this->model, $file);
}
/** @test */
public function cant_upload_file_with_invalid_mime_type()
{
$file = UploadedFile::fake()->create('doc.doc', 0, 'application/msword');
$this->expectException(Exception::class);
$this->expectExceptionMessage(
Exception::invalidMimeType($file->getMimeType(), 'image/png')
->getMessage()
);
$this->model->extension = 'doc';
File::upload($this->model, $file);
}
/** @test */
public function can_display_file_inline()
{
$file = File::upload($this->model, $this->file);
$this->model->file()->associate($file)->save();
$response = $this->model->file->inline($this->file->hashname());
$this->assertEquals(200, $response->getStatusCode());
}
/** @test */
public function can_download_file()
{
$file = File::upload($this->model, $this->file);
$this->model->file()->associate($file)->save();
$response = $this->model->file->download();
$this->assertEquals(200, $response->getStatusCode());
}
private function createTestTable(): self
{
Schema::create('attachable_models', function ($table) {
$table->increments('id');
$table->unsignedBigInteger('file_id')->nullable();
$table->foreign('file_id')->references('id')->on('files')
->onUpdate('restrict')->onDelete('restrict');
$table->timestamps();
});
return $this;
}
private function cleanUp()
{
Storage::deleteDirectory(Config::get('enso.files.testingFolder'));
}
}
class AttachableModel extends Model implements Attachable, Extensions, MimeTypes
{
public string $extension = 'png';
public function file(): Relation
{
return $this->belongsTo(File::class);
}
public function extensions(): array
{
return [$this->extension];
}
public function mimeTypes(): array
{
return ['image/png'];
}
}