GenerateWorldCupReferralRewardOnOrderPaidTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Events\OrderPaid;
  4. use App\Listeners\GenerateWorldCupReferralRewardOnOrderPaid;
  5. use App\Services\WorldCup\WorldCupReferralRewardService;
  6. use Illuminate\Support\Facades\Log;
  7. use Tests\TestCase;
  8. class GenerateWorldCupReferralRewardOnOrderPaidTest extends TestCase
  9. {
  10. public function test_listener_delegates_order_paid_to_referral_reward_service()
  11. {
  12. $service = new SpyWorldCupReferralRewardService();
  13. $listener = new GenerateWorldCupReferralRewardOnOrderPaid($service);
  14. $listener->handle(new OrderPaid(1002, 120.0, 'order-120'));
  15. $this->assertSame([
  16. 'user_id' => 1002,
  17. 'pay_amt' => 120.0,
  18. 'order_sn' => 'order-120',
  19. ], $service->handled[0]);
  20. }
  21. public function test_listener_logs_exception_without_throwing()
  22. {
  23. Log::shouldReceive('error')
  24. ->once()
  25. ->with('GenerateWorldCupReferralRewardOnOrderPaid: exception', \Mockery::type('array'));
  26. $listener = new GenerateWorldCupReferralRewardOnOrderPaid(new ThrowingWorldCupReferralRewardService());
  27. $listener->handle(new OrderPaid(1002, 120.0, 'order-120'));
  28. $this->assertTrue(true);
  29. }
  30. }
  31. class SpyWorldCupReferralRewardService extends WorldCupReferralRewardService
  32. {
  33. public $handled = [];
  34. public function __construct()
  35. {
  36. }
  37. public function handleFirstDeposit(int $userId, float $payAmt, string $orderSn): array
  38. {
  39. $this->handled[] = [
  40. 'user_id' => $userId,
  41. 'pay_amt' => $payAmt,
  42. 'order_sn' => $orderSn,
  43. ];
  44. return ['success' => true, 'status' => 'created'];
  45. }
  46. }
  47. class ThrowingWorldCupReferralRewardService extends WorldCupReferralRewardService
  48. {
  49. public function __construct()
  50. {
  51. }
  52. public function handleFirstDeposit(int $userId, float $payAmt, string $orderSn): array
  53. {
  54. throw new \RuntimeException('boom');
  55. }
  56. }