tests
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Dynamic;
|
||||
use App\Models\Ledger;
|
||||
use App\Models\Mutation;
|
||||
use App\Models\Message;
|
||||
use App\Models\Media;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
test('media can be attached to mutations, ledgers, and messages', function () {
|
||||
Storage::fake('public');
|
||||
|
||||
$user = User::factory()->create();
|
||||
$dynamic = Dynamic::factory()->create();
|
||||
$dynamic->participants()->attach($user->id, ['role' => 'owner']);
|
||||
$ledger = Ledger::factory()->create(['dynamic_id' => $dynamic->id]);
|
||||
|
||||
$this->actingAs($user);
|
||||
|
||||
// 1. Test attaching media to a mutation
|
||||
$file1 = UploadedFile::fake()->image('proof1.jpg');
|
||||
$file2 = UploadedFile::fake()->image('proof2.png');
|
||||
|
||||
$response = $this->post(route('dynamics.ledgers.mutations.store', [$dynamic, $ledger]), [
|
||||
'amount' => 50,
|
||||
'description' => 'Completed deep clean',
|
||||
'media' => [$file1, $file2],
|
||||
]);
|
||||
|
||||
$response->assertSessionHasNoErrors();
|
||||
$response->assertRedirect();
|
||||
|
||||
$mutation = Mutation::firstWhere('description', 'Completed deep clean');
|
||||
expect($mutation)->not->toBeNull();
|
||||
expect($mutation->media)->toHaveCount(2);
|
||||
expect($mutation->media->first()->file_name)->toBe('proof1.jpg');
|
||||
expect($mutation->media->last()->file_name)->toBe('proof2.png');
|
||||
|
||||
Storage::disk('public')->assertExists($mutation->media->first()->file_path);
|
||||
Storage::disk('public')->assertExists($mutation->media->last()->file_path);
|
||||
|
||||
// 2. Test attaching media to a ledger
|
||||
$file3 = UploadedFile::fake()->image('rules.jpg');
|
||||
$response = $this->post(route('dynamics.ledgers.store', $dynamic), [
|
||||
'name' => 'Worship Ledger',
|
||||
'rules' => 'Specific rules',
|
||||
'alignment' => 'neutral',
|
||||
'media' => [$file3],
|
||||
]);
|
||||
|
||||
$response->assertSessionHasNoErrors();
|
||||
$response->assertRedirect();
|
||||
|
||||
$newLedger = Ledger::firstWhere('name', 'Worship Ledger');
|
||||
expect($newLedger)->not->toBeNull();
|
||||
expect($newLedger->media)->toHaveCount(1);
|
||||
expect($newLedger->media->first()->file_name)->toBe('rules.jpg');
|
||||
|
||||
// 3. Test attaching media to a chat message
|
||||
$chat = $dynamic->chat;
|
||||
$file4 = UploadedFile::fake()->image('chat_img.jpg');
|
||||
$response = $this->post(route('chats.messages.store', $chat), [
|
||||
'content' => 'Check this out!',
|
||||
'media' => [$file4],
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$message = Message::firstWhere('content', 'Check this out!');
|
||||
expect($message)->not->toBeNull();
|
||||
expect($message->media)->toHaveCount(1);
|
||||
expect($message->media->first()->file_name)->toBe('chat_img.jpg');
|
||||
});
|
||||
Reference in New Issue
Block a user