|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cone\Bazar\Tests\Http\Controllers; |
| 6 | + |
| 7 | +use Cone\Bazar\Gateway\CashDriver; |
| 8 | +use Cone\Bazar\Gateway\Response; |
| 9 | +use Cone\Bazar\Http\Controllers\GatewayController; |
| 10 | +use Cone\Bazar\Models\Order; |
| 11 | +use Cone\Bazar\Models\Product; |
| 12 | +use Cone\Bazar\Support\Facades\Gateway; |
| 13 | +use Cone\Bazar\Tests\TestCase; |
| 14 | +use Illuminate\Http\Request; |
| 15 | + |
| 16 | +class GatewayControllerTest extends TestCase |
| 17 | +{ |
| 18 | + protected GatewayController $controller; |
| 19 | + |
| 20 | + protected Order $order; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->controller = new GatewayController(); |
| 27 | + |
| 28 | + $this->order = Order::factory()->create(); |
| 29 | + |
| 30 | + Product::factory()->count(2)->create()->each(function ($product) { |
| 31 | + $this->order->items()->create([ |
| 32 | + 'buyable_id' => $product->id, |
| 33 | + 'buyable_type' => Product::class, |
| 34 | + 'quantity' => 2, |
| 35 | + 'price' => $product->price, |
| 36 | + 'name' => $product->name, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_controller_handles_capture_request(): void |
| 42 | + { |
| 43 | + $request = Request::create('/gateway/cash/capture', 'POST', [ |
| 44 | + 'order_id' => $this->order->id, |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->mock(CashDriver::class, function ($mock) { |
| 48 | + $mock->shouldReceive('resolveOrderForCapture') |
| 49 | + ->once() |
| 50 | + ->andReturn($this->order); |
| 51 | + |
| 52 | + $mock->shouldReceive('handleCapture') |
| 53 | + ->once() |
| 54 | + ->andReturn(new Response(['status' => 'success'])); |
| 55 | + }); |
| 56 | + |
| 57 | + Gateway::shouldReceive('driver') |
| 58 | + ->with('cash') |
| 59 | + ->andReturn($this->app->make(CashDriver::class)); |
| 60 | + |
| 61 | + $response = $this->controller->capture($request, 'cash'); |
| 62 | + |
| 63 | + $this->assertEquals(200, $response->getStatusCode()); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_controller_handles_invalid_capture_request(): void |
| 67 | + { |
| 68 | + $request = Request::create('/gateway/invalid/capture', 'POST'); |
| 69 | + |
| 70 | + Gateway::shouldReceive('driver') |
| 71 | + ->with('invalid') |
| 72 | + ->andThrow(new \Exception('Invalid driver')); |
| 73 | + |
| 74 | + $response = $this->controller->capture($request, 'invalid'); |
| 75 | + |
| 76 | + $this->assertEquals(400, $response->getStatusCode()); |
| 77 | + $this->assertStringContainsString('Invalid request', $response->getContent()); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_controller_handles_notification_request(): void |
| 81 | + { |
| 82 | + $request = Request::create('/gateway/cash/notification', 'POST', [ |
| 83 | + 'order_id' => $this->order->id, |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->mock(CashDriver::class, function ($mock) { |
| 87 | + $mock->shouldReceive('resolveOrderForNotification') |
| 88 | + ->once() |
| 89 | + ->andReturn($this->order); |
| 90 | + |
| 91 | + $mock->shouldReceive('handleNotification') |
| 92 | + ->once() |
| 93 | + ->andReturn(new Response(['status' => 'success'])); |
| 94 | + }); |
| 95 | + |
| 96 | + Gateway::shouldReceive('driver') |
| 97 | + ->with('cash') |
| 98 | + ->andReturn($this->app->make(CashDriver::class)); |
| 99 | + |
| 100 | + $response = $this->controller->notification($request, 'cash'); |
| 101 | + |
| 102 | + $this->assertEquals(200, $response->getStatusCode()); |
| 103 | + } |
| 104 | + |
| 105 | + public function test_controller_handles_invalid_notification_request(): void |
| 106 | + { |
| 107 | + $request = Request::create('/gateway/invalid/notification', 'POST'); |
| 108 | + |
| 109 | + Gateway::shouldReceive('driver') |
| 110 | + ->with('invalid') |
| 111 | + ->andThrow(new \Exception('Invalid driver')); |
| 112 | + |
| 113 | + $response = $this->controller->notification($request, 'invalid'); |
| 114 | + |
| 115 | + $this->assertEquals(400, $response->getStatusCode()); |
| 116 | + $this->assertStringContainsString('Invalid request', $response->getContent()); |
| 117 | + } |
| 118 | +} |
0 commit comments