| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace Tests\Unit;
- use App\Events\OrderPaid;
- use App\Listeners\GenerateWorldCupReferralRewardOnOrderPaid;
- use App\Services\WorldCup\WorldCupReferralRewardService;
- use Illuminate\Support\Facades\Log;
- use Tests\TestCase;
- class GenerateWorldCupReferralRewardOnOrderPaidTest extends TestCase
- {
- public function test_listener_delegates_order_paid_to_referral_reward_service()
- {
- $service = new SpyWorldCupReferralRewardService();
- $listener = new GenerateWorldCupReferralRewardOnOrderPaid($service);
- $listener->handle(new OrderPaid(1002, 120.0, 'order-120'));
- $this->assertSame([
- 'user_id' => 1002,
- 'pay_amt' => 120.0,
- 'order_sn' => 'order-120',
- ], $service->handled[0]);
- }
- public function test_listener_logs_exception_without_throwing()
- {
- Log::shouldReceive('error')
- ->once()
- ->with('GenerateWorldCupReferralRewardOnOrderPaid: exception', \Mockery::type('array'));
- $listener = new GenerateWorldCupReferralRewardOnOrderPaid(new ThrowingWorldCupReferralRewardService());
- $listener->handle(new OrderPaid(1002, 120.0, 'order-120'));
- $this->assertTrue(true);
- }
- }
- class SpyWorldCupReferralRewardService extends WorldCupReferralRewardService
- {
- public $handled = [];
- public function __construct()
- {
- }
- public function handleFirstDeposit(int $userId, float $payAmt, string $orderSn): array
- {
- $this->handled[] = [
- 'user_id' => $userId,
- 'pay_amt' => $payAmt,
- 'order_sn' => $orderSn,
- ];
- return ['success' => true, 'status' => 'created'];
- }
- }
- class ThrowingWorldCupReferralRewardService extends WorldCupReferralRewardService
- {
- public function __construct()
- {
- }
- public function handleFirstDeposit(int $userId, float $payAmt, string $orderSn): array
- {
- throw new \RuntimeException('boom');
- }
- }
|