Skip to content

Commit

Permalink
Test sending email
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 9, 2023
1 parent 8d8962d commit e7bee04
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/Http/Controllers/OrderShipmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Controllers;

use App\Mail\OrderShipped;
use App\Models\Order;
use Illuminate\Support\Facades\Mail;

class OrderShipmentController extends Controller
{
public function shipOrderBasic(Order $order)
{
$order->status = 'shipped';
$order->save();

Mail::to(auth()->user())->send(new OrderShipped($order));

return ['message' => 'Your order has been shipped.'];
}
}
2 changes: 2 additions & 0 deletions database/migrations/2023_01_06_152053_create_orders_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function up()
$table->unsignedBigInteger('shipper_id');
$table->string('name');
$table->unsignedDecimal('price');
$table->enum('status', ['pending', 'processing', 'shipped', 'cancelled', 'completed'])
->default('pending');
$table->timestamps();
});
}
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\HomeController;
use App\Http\Controllers\MediaUploaderController;
use App\Http\Controllers\OrderShipmentController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\PostPublishController;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -61,5 +62,8 @@
Route::prefix('mailable')->group(function() {
Route::get('template/{id}', [PostPublishController::class, 'previewMailTemplate'])
->name('mailable.preview');

Route::post('orders/{order}/shipped/basic', [OrderShipmentController::class, 'shipOrderBasic'])
->name('order.shipped.basic');
});
});
18 changes: 18 additions & 0 deletions tests/Feature/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;

class MailTest extends TestCase
Expand Down Expand Up @@ -136,4 +137,21 @@ public function mailable_can_have_attachments_at_runtime()

$mailable->assertHasAttachmentFromStorageDisk('public', 'your-order.pdf');
}

/** @test */
public function email_api_can_be_instructed_to_send_a_mailable()
{
$this->actingAs($user = User::factory()->create());

Mail::fake();

Mail::assertNotSent(OrderShipped::class);

$this->post(route(
'order.shipped.basic',
Order::factory()->create(['user_id' => $user->id])
));

Mail::assertSent(OrderShipped::class);
}
}

0 comments on commit e7bee04

Please sign in to comment.