PaymentChannelOptionsTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\PaymentConfiguration;
  4. use App\Http\Controllers\admin\RechargeChannel;
  5. use App\Services\PaymentChannelLinkService;
  6. use Illuminate\Config\Repository;
  7. use Illuminate\Database\Capsule\Manager;
  8. use Illuminate\Database\Schema\Blueprint;
  9. use Illuminate\Foundation\Application;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Facade;
  14. use Illuminate\Translation\ArrayLoader;
  15. use Illuminate\Translation\Translator;
  16. use Illuminate\Validation\Factory;
  17. use PHPUnit\Framework\TestCase;
  18. class PaymentChannelOptionsTest extends TestCase
  19. {
  20. private Application $app;
  21. protected function setUp(): void
  22. {
  23. Facade::clearResolvedInstances();
  24. $this->app = new Application(dirname(__DIR__, 2));
  25. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
  26. Facade::setFacadeApplication($this->app);
  27. $db = new Manager($this->app);
  28. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  29. $db->setAsGlobal();
  30. $db->bootEloquent();
  31. $this->app->instance('db', $db->getDatabaseManager());
  32. $translator = new Translator(new ArrayLoader(), 'zh');
  33. $this->app->instance('translator', $translator);
  34. $this->app->instance('validator', new Factory($translator, $this->app));
  35. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
  36. public function json($data, $status = 200, $headers = [], $options = 0) {
  37. return new JsonResponse($data, $status, $headers, $options);
  38. }
  39. });
  40. Request::macro('validate', function (array $rules) {
  41. return app('validator')->make($this->all(), $rules)->validate();
  42. });
  43. $db->schema()->create('recharge_channel', function (Blueprint $t) {
  44. $t->id(); $t->integer('from'); $t->integer('data_type'); $t->string('key');
  45. $t->string('name'); $t->string('type'); $t->decimal('rate', 8, 4);
  46. $t->decimal('min')->nullable(); $t->decimal('max')->nullable(); $t->string('fixed')->nullable();
  47. $t->integer('status'); $t->integer('sort'); $t->timestamps();
  48. });
  49. $db->schema()->create('recharge_channel_group', function (Blueprint $t) {
  50. $t->id(); $t->string('name'); $t->text('recharge_type')->nullable();
  51. $t->text('withdraw_type')->nullable(); $t->text('activity_type')->nullable(); $t->timestamps();
  52. });
  53. $fixture = require dirname(__DIR__) . '/fixtures/payment_provider_channels.php';
  54. DB::table('recharge_channel')->insert($fixture['channels']);
  55. DB::table('recharge_channel_group')->insert($fixture['groups']);
  56. }
  57. protected function tearDown(): void
  58. {
  59. DB::disconnect();
  60. Facade::clearResolvedInstances();
  61. parent::tearDown();
  62. }
  63. private function request(string $path, array $params): void
  64. {
  65. $this->app->instance('request', Request::create($path, 'GET', $params));
  66. }
  67. private function channelList(array $params): array
  68. {
  69. $this->request('/admin/rechargeChannel/list', $params);
  70. $response = (new RechargeChannel())->list()->getData(true);
  71. $this->assertSame(0, $response['code']);
  72. return $response['data'];
  73. }
  74. public function test_company_options_use_all_rows_not_first_page(): void
  75. {
  76. $page1 = $this->channelList(['data_type' => '1']);
  77. $this->assertSame(19, $page1['total']);
  78. $this->assertCount(15, $page1['data']);
  79. $this->assertNotContains('jd', array_column($page1['data'], 'payment_company'));
  80. $page2 = $this->channelList(['data_type' => '1', 'page' => 2]);
  81. $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company'));
  82. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  83. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true);
  84. $this->assertSame(0, $data['code']);
  85. $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled'));
  86. }
  87. public function test_channel_list_displays_provider_separately_from_channel_name(): void
  88. {
  89. $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']);
  90. $this->assertSame(1, $data['total']);
  91. $this->assertSame('JD钱包', $data['data'][0]['name']);
  92. $this->assertSame('JD支付', $data['data'][0]['payment_company_label']);
  93. $this->assertSame(1, $data['data'][0]['from']);
  94. $this->assertSame(24, $data['data'][0]['id']);
  95. $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']);
  96. $this->assertSame(25, $withdraw['data'][0]['id']);
  97. $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']);
  98. }
  99. public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void
  100. {
  101. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']);
  102. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  103. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  104. $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled'));
  105. $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code'));
  106. $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type'));
  107. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  108. }
  109. public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void
  110. {
  111. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']);
  112. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  113. $response = (new RechargeChannel())->getChannel()->getData(true);
  114. $this->assertSame(0, $response['code']);
  115. $names = array_column($response['data']['data'], 'name', 'type');
  116. $this->assertSame('JD钱包', $names['JDpay']);
  117. $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']);
  118. $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']);
  119. $this->assertSame('808充值', $names['ZIMUpay']);
  120. $this->assertSame(13, $response['data']['total']);
  121. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]);
  122. $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type');
  123. $this->assertSame('JD钱包', $names['JDpay']);
  124. $this->assertSame('NO快捷提现', $names['NOwithdraw']);
  125. $this->assertSame('808账户提现', $names['ZIMUwithdraw']);
  126. $this->assertArrayNotHasKey('ZIMUcash', $names);
  127. $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
  128. }
  129. }