Skip to content

Commit ba6c93d

Browse files
committed
Refine Pest Setup
1 parent e98bd6b commit ba6c93d

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ phpstan.neon
99
testbench.yaml
1010
vendor
1111
node_modules
12+
.phpunit.cache/

tests/LaravelMediaSecureTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@
8181
it('can download media if media do exist', function () {
8282
$user = user();
8383
$media = media($user);
84+
85+
// Make sure media was created successfully
86+
expect($media)->not->toBeNull();
87+
8488
login($user)
8589
->get(route('media', [
86-
'type' => MediaAccess::view()->value,
90+
'type' => MediaAccess::download()->value, // Changed from view to download
8791
'uuid' => $media->uuid,
8892
]))->assertStatus(200);
8993
})->group('download')->skip('The test unable to add media at the moment.');

tests/Pest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
uses(TestCase::class)->in(__DIR__);
99

10-
function login($user = null)
10+
function login(?User $user = null)
1111
{
1212
return test()->actingAs($user ?? user());
1313
}
1414

15-
function user($name = 'pest', $email = '[email protected]', $password = 'password')
15+
function user($name = 'pest', $email = '[email protected]', $password = 'password'): User
1616
{
1717
return User::updateOrCreate([
1818
'name' => $name,
@@ -23,17 +23,33 @@ function user($name = 'pest', $email = '[email protected]', $password = 'password')
2323
]);
2424
}
2525

26-
function media($user)
26+
function media(User $user)
2727
{
2828
$file = __DIR__.'/file.test';
2929
if (file_exists($file)) {
3030
unlink($file);
3131
}
3232

33-
touch($file);
34-
$user
35-
->addMedia($file)
36-
->toMediaCollection();
33+
file_put_contents($file, 'Test content');
3734

38-
return $user->getFirstMedia();
35+
try {
36+
$media = $user
37+
->addMedia($file)
38+
->usingName('Test File')
39+
->usingFileName('file.test')
40+
->toMediaCollection('private'); // Use a private collection name
41+
42+
if (!$media) {
43+
throw new \Exception('Media not created');
44+
}
45+
46+
return $user->fresh()->getFirstMedia('private');
47+
} catch (\Exception $e) {
48+
var_dump($e->getMessage());
49+
throw $e;
50+
} finally {
51+
if (file_exists($file)) {
52+
unlink($file);
53+
}
54+
}
3955
}

tests/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CleaniqueCoders\LaravelMediaSecure\LaravelMediaSecureServiceProvider;
66
use Illuminate\Database\Eloquent\Factories\Factory;
77
use Orchestra\Testbench\TestCase as Orchestra;
8+
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
89

910
class TestCase extends Orchestra
1011
{
@@ -21,13 +22,27 @@ protected function getPackageProviders($app)
2122
{
2223
return [
2324
LaravelMediaSecureServiceProvider::class,
25+
MediaLibraryServiceProvider::class,
2426
];
2527
}
2628

2729
public function getEnvironmentSetUp($app)
2830
{
2931
config()->set('database.default', 'testing');
3032

33+
config()->set('filesystems.disks.private_media', [
34+
'driver' => 'local',
35+
'root' => storage_path('app/private/media'),
36+
'visibility' => 'private',
37+
]);
38+
39+
config()->set('media-library', [
40+
'disk_name' => 'private_media',
41+
'max_file_size' => 1024 * 1024 * 10, // 10MB
42+
'queue_conversions_by_default' => false,
43+
'media_model' => \Spatie\MediaLibrary\MediaCollections\Models\Media::class,
44+
]);
45+
3146
/*
3247
$migration = include __DIR__.'/../database/migrations/create_laravel-media-secure_table.php.stub';
3348
$migration->up();

tests/file.test

Whitespace-only changes.

0 commit comments

Comments
 (0)