| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php
- namespace App\Module\Transfer\Tests\Feature;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Enums\TransferType;
- use App\Module\OpenAPI\Models\OpenApiApp;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Foundation\Testing\WithFaker;
- /**
- * 划转API功能测试
- */
- class TransferApiTest extends TestCase
- {
- use RefreshDatabase, WithFaker;
- /**
- * 测试应用
- */
- protected TransferApp $testApp;
- /**
- * OpenAPI应用
- */
- protected OpenApiApp $apiApp;
- /**
- * 设置测试环境
- */
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试应用
- $this->testApp = TransferApp::create([
- 'keyname' => 'test_app',
- 'title' => '测试应用',
- 'description' => '用于API测试的应用',
- 'out_id' => 1001,
- 'currency_id' => 1,
- 'fund_id' => 1,
- 'exchange_rate' => 1.0000,
- 'is_enabled' => true,
- ]);
- // 创建OpenAPI应用(如果存在OpenAPI模块)
- if (class_exists(OpenApiApp::class)) {
- $this->apiApp = OpenApiApp::create([
- 'name' => 'Test API App',
- 'app_id' => 'test_app_001',
- 'app_secret' => 'test_secret_123',
- 'scopes' => ['TRANSFER_IN', 'TRANSFER_OUT', 'TRANSFER_QUERY'],
- 'is_enabled' => true,
- ]);
- }
- }
- /**
- * 测试转入API
- */
- public function testTransferInApi(): void
- {
- $data = [
- 'business_id' => 'api_test_in_' . time(),
- 'user_id' => 1001,
- 'amount' => '100.50',
- 'out_user_id' => 'ext_user_123',
- 'remark' => 'API测试转入',
- 'callback_data' => ['test' => 'data'],
- ];
- $response = $this->postJson('/api/transfer/in', $data, $this->getApiHeaders());
- $response->assertStatus(200)
- ->assertJsonStructure([
- 'code',
- 'message',
- 'data' => [
- 'order_id',
- 'business_id',
- 'amount',
- 'internal_amount',
- 'exchange_rate',
- 'status',
- 'status_text',
- 'created_at'
- ]
- ]);
- // 验证数据库中的记录
- $this->assertDatabaseHas('kku_transfer_orders', [
- 'out_order_id' => $data['business_id'],
- 'user_id' => $data['user_id'],
- 'type' => TransferType::IN->value,
- 'status' => TransferStatus::CREATED->value,
- ]);
- }
- /**
- * 测试转出API
- */
- public function testTransferOutApi(): void
- {
- $data = [
- 'business_id' => 'api_test_out_' . time(),
- 'user_id' => 1001,
- 'amount' => '50.25',
- 'out_user_id' => 'ext_user_456',
- 'remark' => 'API测试转出',
- ];
- $response = $this->postJson('/api/transfer/out', $data, $this->getApiHeaders());
- $response->assertStatus(200)
- ->assertJsonStructure([
- 'code',
- 'message',
- 'data' => [
- 'order_id',
- 'business_id',
- 'amount',
- 'out_amount',
- 'exchange_rate',
- 'status',
- 'status_text',
- 'created_at'
- ]
- ]);
- // 验证数据库中的记录
- $this->assertDatabaseHas('kku_transfer_orders', [
- 'out_order_id' => $data['business_id'],
- 'user_id' => $data['user_id'],
- 'type' => TransferType::OUT->value,
- ]);
- }
- /**
- * 测试查询API - 通过业务ID查询
- */
- public function testTransferQueryByBusinessId(): void
- {
- // 先创建一个订单
- $order = TransferOrder::create([
- 'transfer_app_id' => $this->testApp->id,
- 'out_id' => $this->testApp->out_id,
- 'out_order_id' => 'query_test_' . time(),
- 'user_id' => 1001,
- 'currency_id' => 1,
- 'fund_id' => 1,
- 'type' => TransferType::IN,
- 'status' => TransferStatus::COMPLETED,
- 'out_amount' => 100,
- 'amount' => 100,
- 'exchange_rate' => 1.0000,
- ]);
- $response = $this->getJson('/api/transfer/order?business_id=' . $order->out_order_id, $this->getApiHeaders());
- $response->assertStatus(200)
- ->assertJsonStructure([
- 'code',
- 'message',
- 'data' => [
- 'order_id',
- 'business_id',
- 'type',
- 'type_text',
- 'status',
- 'status_text',
- 'amount',
- 'out_amount',
- 'exchange_rate',
- 'user_id',
- 'created_at',
- ]
- ]);
- $responseData = $response->json('data');
- $this->assertEquals($order->id, $responseData['order_id']);
- $this->assertEquals($order->out_order_id, $responseData['business_id']);
- }
- /**
- * 测试查询API - 通过订单ID查询
- */
- public function testTransferQueryByOrderId(): void
- {
- // 先创建一个订单
- $order = TransferOrder::create([
- 'transfer_app_id' => $this->testApp->id,
- 'out_id' => $this->testApp->out_id,
- 'out_order_id' => 'query_order_test_' . time(),
- 'user_id' => 1001,
- 'currency_id' => 1,
- 'fund_id' => 1,
- 'type' => TransferType::OUT,
- 'status' => TransferStatus::PROCESSING,
- 'out_amount' => 200,
- 'amount' => 200,
- 'exchange_rate' => 1.0000,
- ]);
- $response = $this->getJson('/api/transfer/order?order_id=' . $order->id, $this->getApiHeaders());
- $response->assertStatus(200);
-
- $responseData = $response->json('data');
- $this->assertEquals($order->id, $responseData['order_id']);
- $this->assertEquals(TransferType::OUT->value, $responseData['type']);
- $this->assertEquals(TransferStatus::PROCESSING->value, $responseData['status']);
- }
- /**
- * 测试API验证 - 缺少必填字段
- */
- public function testApiValidationMissingFields(): void
- {
- $data = [
- // 缺少 business_id
- 'user_id' => 1001,
- 'amount' => '100.00',
- ];
- $response = $this->postJson('/api/transfer/in', $data, $this->getApiHeaders());
- $response->assertStatus(422)
- ->assertJsonStructure([
- 'code',
- 'message',
- 'data' => [
- 'business_id'
- ]
- ]);
- }
- /**
- * 测试API验证 - 无效金额
- */
- public function testApiValidationInvalidAmount(): void
- {
- $data = [
- 'business_id' => 'invalid_amount_test_' . time(),
- 'user_id' => 1001,
- 'amount' => 'invalid_amount',
- ];
- $response = $this->postJson('/api/transfer/in', $data, $this->getApiHeaders());
- $response->assertStatus(422)
- ->assertJsonStructure([
- 'code',
- 'message',
- 'data' => [
- 'amount'
- ]
- ]);
- }
- /**
- * 测试API权限验证
- */
- public function testApiPermissionValidation(): void
- {
- $data = [
- 'business_id' => 'permission_test_' . time(),
- 'user_id' => 1001,
- 'amount' => '100.00',
- ];
- // 不提供认证头
- $response = $this->postJson('/api/transfer/in', $data);
- $response->assertStatus(401);
- }
- /**
- * 测试重复业务ID
- */
- public function testDuplicateBusinessId(): void
- {
- $businessId = 'duplicate_api_test_' . time();
-
- $data = [
- 'business_id' => $businessId,
- 'user_id' => 1001,
- 'amount' => '100.00',
- ];
- // 第一次请求应该成功
- $response1 = $this->postJson('/api/transfer/in', $data, $this->getApiHeaders());
- $response1->assertStatus(200);
- // 第二次请求应该失败
- $response2 = $this->postJson('/api/transfer/in', $data, $this->getApiHeaders());
- $response2->assertStatus(400)
- ->assertJsonPath('message', '业务订单ID已存在');
- }
- /**
- * 测试查询不存在的订单
- */
- public function testQueryNonExistentOrder(): void
- {
- $response = $this->getJson('/api/transfer/order?business_id=non_existent_order', $this->getApiHeaders());
- $response->assertStatus(404)
- ->assertJsonPath('message', '订单不存在');
- }
- /**
- * 测试汇率转换
- */
- public function testExchangeRateInApi(): void
- {
- // 设置汇率为2.0
- $this->testApp->update(['exchange_rate' => 2.0000]);
- $data = [
- 'business_id' => 'rate_test_' . time(),
- 'user_id' => 1001,
- 'amount' => '100.00',
- ];
- $response = $this->postJson('/api/transfer/out', $data, $this->getApiHeaders());
- $response->assertStatus(200);
-
- $responseData = $response->json('data');
- $this->assertEquals('100.00', $responseData['amount']); // 内部金额
- $this->assertEquals('200.00', $responseData['out_amount']); // 外部金额
- $this->assertEquals('2.0000', $responseData['exchange_rate']);
- }
- /**
- * 获取API请求头
- */
- protected function getApiHeaders(): array
- {
- if (!isset($this->apiApp)) {
- return ['Accept' => 'application/json'];
- }
- return [
- 'Accept' => 'application/json',
- 'Authorization' => 'Bearer ' . $this->generateApiToken(),
- 'X-App-Id' => $this->apiApp->app_id,
- ];
- }
- /**
- * 生成API令牌(简化版)
- */
- protected function generateApiToken(): string
- {
- return 'test_token_' . time();
- }
- /**
- * 清理测试环境
- */
- protected function tearDown(): void
- {
- parent::tearDown();
- }
- }
|