Skip to content

Commit 58175ad

Browse files
committed
- Added attachments clean command
1 parent ee36851 commit 58175ad

1 file changed

Lines changed: 33 additions & 34 deletions

File tree

tests/Feature/Commands/AttachmentsCleanCommandTest.php

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Javaabu\Mediapicker\Tests\Feature\Commands;
44

55
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\File;
68
use Javaabu\Mediapicker\Tests\TestCase;
79
use PHPUnit\Framework\Attributes\Test;
810

@@ -11,28 +13,20 @@ class AttachmentsCleanCommandTest extends TestCase
1113
use RefreshDatabase;
1214

1315
#[Test]
14-
public function it_can_clean_all_attachments(): void
15-
{
16-
$attachment = $this->getAttachment();
17-
18-
$this->assertDatabaseHas('attachments', [
19-
'id' => $attachment->id,
20-
]);
21-
22-
$this->artisan('mediapicker:clear')
23-
->assertSuccessful();
24-
25-
$this->assertDatabaseEmpty('attachments');
26-
}
27-
28-
/*#[Test]
29-
public function it_can_clear_attachments_of_a_specific_model_type(): void
16+
public function it_can_clean_orphaned_attachments(): void
3017
{
3118
$post = $this->getModel();
3219
$attachment = $this->getAttachment($post);
3320

34-
$other_model = $this->getModelWithConversions();
35-
$attachment_2 = $this->getAttachment($other_model);
21+
DB::table('posts')
22+
->where('id', $post->id)
23+
->delete();
24+
25+
$attachment_2 = $this->getAttachment();
26+
27+
$this->assertDatabaseMissing('posts', [
28+
'id' => $post->id,
29+
]);
3630

3731
$this->assertDatabaseHas('attachments', [
3832
'id' => $attachment->id,
@@ -42,7 +36,7 @@ public function it_can_clear_attachments_of_a_specific_model_type(): void
4236
'id' => $attachment_2->id,
4337
]);
4438

45-
$this->artisan('mediapicker:clear', ['modelType' => 'post'])
39+
$this->artisan('mediapicker:clean', ['--delete-orphaned' => true])
4640
->assertSuccessful();
4741

4842
$this->assertDatabaseMissing('attachments', [
@@ -55,31 +49,36 @@ public function it_can_clear_attachments_of_a_specific_model_type(): void
5549
}
5650

5751
#[Test]
58-
public function it_can_clear_attachments_of_a_specific_collection(): void
52+
public function it_can_clean_deprecated_attachment_conversion_files(): void
5953
{
60-
$post = $this->getModel();
61-
$attachment = $this->getAttachment($post, 'test1');
54+
$model = $this->getModelWithConversions();
55+
$media = $this->getMedia();
6256

63-
$other_model = $this->getModelWithConversions();
64-
$attachment_2 = $this->getAttachment($other_model, 'test');
57+
$attachment = $model->addAttachment($media)
58+
->toAttachmentCollection();
6559

66-
$this->assertDatabaseHas('attachments', [
67-
'id' => $attachment->id,
68-
]);
60+
$path = $model->getFirstAttachmentPath(conversionName: 'test');
61+
62+
$this->assertEquals($this->getMediaDirectory($media->getKey() . '/conversions/test-test.jpg'), $path);
63+
$this->assertFileExists($path);
64+
65+
$new_path = $this->getMediaDirectory($media->getKey() . '/conversions/test-test2.jpg');
66+
File::copy($path, $new_path);
67+
68+
$this->assertFileExists($new_path);
6969

7070
$this->assertDatabaseHas('attachments', [
71-
'id' => $attachment_2->id,
71+
'id' => $attachment->id,
7272
]);
7373

74-
$this->artisan('mediapicker:clear', ['collectionName' => 'test1'])
74+
$this->artisan('mediapicker:clean')
7575
->assertSuccessful();
7676

77-
$this->assertDatabaseMissing('attachments', [
77+
$this->assertDatabaseHas('attachments', [
7878
'id' => $attachment->id,
7979
]);
8080

81-
$this->assertDatabaseHas('attachments', [
82-
'id' => $attachment_2->id,
83-
]);
84-
}*/
81+
$this->assertFileExists($path);
82+
$this->assertFileDoesNotExist($new_path);
83+
}
8584
}

0 commit comments

Comments
 (0)