Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Jobs/PaymentEmailTest.php
<?php | <?php | ||||
namespace Tests\Feature\Jobs; | namespace Tests\Feature\Jobs; | ||||
use App\Jobs\PaymentEmail; | use App\Jobs\PaymentEmail; | ||||
use App\Mail\PaymentFailure; | use App\Mail\PaymentFailure; | ||||
use App\Mail\PaymentSuccess; | use App\Mail\PaymentSuccess; | ||||
use App\Payment; | use App\Payment; | ||||
use App\Providers\PaymentProvider; | |||||
use App\User; | use App\User; | ||||
use Illuminate\Support\Facades\Mail; | use Illuminate\Support\Facades\Mail; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class PaymentEmailTest extends TestCase | class PaymentEmailTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
Show All 33 Lines | public function testHandle() | ||||
$payment = new Payment(); | $payment = new Payment(); | ||||
$payment->id = 'test-payment'; | $payment->id = 'test-payment'; | ||||
$payment->wallet_id = $wallet->id; | $payment->wallet_id = $wallet->id; | ||||
$payment->amount = 100; | $payment->amount = 100; | ||||
$payment->credit_amount = 100; | $payment->credit_amount = 100; | ||||
$payment->currency_amount = 100; | $payment->currency_amount = 100; | ||||
$payment->currency = 'CHF'; | $payment->currency = 'CHF'; | ||||
$payment->status = PaymentProvider::STATUS_PAID; | $payment->status = Payment::STATUS_PAID; | ||||
$payment->description = 'test'; | $payment->description = 'test'; | ||||
$payment->provider = 'stripe'; | $payment->provider = 'stripe'; | ||||
$payment->type = PaymentProvider::TYPE_ONEOFF; | $payment->type = Payment::TYPE_ONEOFF; | ||||
$payment->save(); | $payment->save(); | ||||
Mail::fake(); | Mail::fake(); | ||||
// Assert that no jobs were pushed... | // Assert that no jobs were pushed... | ||||
Mail::assertNothingSent(); | Mail::assertNothingSent(); | ||||
$job = new PaymentEmail($payment); | $job = new PaymentEmail($payment); | ||||
$job->handle(); | $job->handle(); | ||||
// Assert the email sending job was pushed once | // Assert the email sending job was pushed once | ||||
Mail::assertSent(PaymentSuccess::class, 1); | Mail::assertSent(PaymentSuccess::class, 1); | ||||
// Assert the mail was sent to the user's email | // Assert the mail was sent to the user's email | ||||
Mail::assertSent(PaymentSuccess::class, function ($mail) { | Mail::assertSent(PaymentSuccess::class, function ($mail) { | ||||
return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); | return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); | ||||
}); | }); | ||||
$payment->status = PaymentProvider::STATUS_FAILED; | $payment->status = Payment::STATUS_FAILED; | ||||
$payment->save(); | $payment->save(); | ||||
$job = new PaymentEmail($payment); | $job = new PaymentEmail($payment); | ||||
$job->handle(); | $job->handle(); | ||||
// Assert the email sending job was pushed once | // Assert the email sending job was pushed once | ||||
Mail::assertSent(PaymentFailure::class, 1); | Mail::assertSent(PaymentFailure::class, 1); | ||||
// Assert the mail was sent to the user's email | // Assert the mail was sent to the user's email | ||||
Mail::assertSent(PaymentFailure::class, function ($mail) { | Mail::assertSent(PaymentFailure::class, function ($mail) { | ||||
return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); | return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); | ||||
}); | }); | ||||
$payment->status = PaymentProvider::STATUS_EXPIRED; | $payment->status = Payment::STATUS_EXPIRED; | ||||
$payment->save(); | $payment->save(); | ||||
$job = new PaymentEmail($payment); | $job = new PaymentEmail($payment); | ||||
$job->handle(); | $job->handle(); | ||||
// Assert the email sending job was pushed twice | // Assert the email sending job was pushed twice | ||||
Mail::assertSent(PaymentFailure::class, 2); | Mail::assertSent(PaymentFailure::class, 2); | ||||
// None of statuses below should trigger an email | // None of statuses below should trigger an email | ||||
Mail::fake(); | Mail::fake(); | ||||
$states = [ | $states = [ | ||||
PaymentProvider::STATUS_OPEN, | Payment::STATUS_OPEN, | ||||
PaymentProvider::STATUS_CANCELED, | Payment::STATUS_CANCELED, | ||||
PaymentProvider::STATUS_PENDING, | Payment::STATUS_PENDING, | ||||
PaymentProvider::STATUS_AUTHORIZED, | Payment::STATUS_AUTHORIZED, | ||||
]; | ]; | ||||
foreach ($states as $state) { | foreach ($states as $state) { | ||||
$payment->status = $state; | $payment->status = $state; | ||||
$payment->save(); | $payment->save(); | ||||
$job = new PaymentEmail($payment); | $job = new PaymentEmail($payment); | ||||
$job->handle(); | $job->handle(); | ||||
} | } | ||||
// Assert that no mailables were sent... | // Assert that no mailables were sent... | ||||
Mail::assertNothingSent(); | Mail::assertNothingSent(); | ||||
} | } | ||||
} | } |