beforeBootstrapping(\Illuminate\Foundation\Bootstrap\RegisterProviders::class,function($app){ $app->instance('env','testing'); $app['config']->set(['app.env'=>'testing','database.default'=>'sqlite','database.connections.sqlite.database'=>':memory:', 'cache.default'=>'array','cache.stores.redis'=>['driver'=>'array'],'mail.default'=>'array','queue.default'=>'sync']); }); $app->make(Kernel::class)->bootstrap();return $app; } protected function beforeRefreshingDatabase(): void { DB::purge('sqlite');Cache::forgetDriver(['array','redis']);Http::preventStrayRequests();Mail::fake();Queue::fake(); } protected function setUp(): void { parent::setUp();HookManager::reset(); (require base_path('plugins/VipLoyalty/database/migrations/2026_11_03_000001_create_vip_loyalty_tables.php'))->up(); app()->forgetInstance(\App\Support\Setting::class);Cache::flush(); } private function enable(array $config=[]): Service { Plugin::updateOrCreate(['code'=>'vip_loyalty'],['name'=>'VIP','version'=>'1.0.0','type'=>'feature','is_enabled'=>true,'config'=>json_encode(array_replace(Settings::DEFAULTS,['enabled'=>true],$config))]); app()->register(\Plugin\VipLoyalty\Providers\PluginServiceProvider::class); (new \Plugin\VipLoyalty\Plugin('vip_loyalty'))->boot();return app(Service::class); } private function user(array $attrs=[]): User {return User::create($attrs+['email'=>bin2hex(random_bytes(6)).'@example.test','password'=>password_hash('private-test-password',PASSWORD_BCRYPT),'uuid'=>(string)\Illuminate\Support\Str::uuid(),'token'=>bin2hex(random_bytes(16)),'balance'=>0,'u'=>0,'d'=>0,'expired_at'=>0]);} private function plan(int $price=100): Plan {return Plan::create(['name'=>'VIP test','prices'=>['monthly'=>$price,'onetime'=>$price,'reset_traffic'=>5],'transfer_enable'=>100,'show'=>true,'sell'=>true,'renew'=>true,'reset_traffic_method'=>2]);} private function processing(User $u,Plan $p,array $attrs=[]): Order {return Order::create($attrs+['user_id'=>$u->id,'plan_id'=>$p->id,'trade_no'=>bin2hex(random_bytes(12)),'type'=>Order::TYPE_NEW_PURCHASE,'period'=>'monthly','status'=>Order::STATUS_PROCESSING,'total_amount'=>10000,'paid_at'=>time()]);} private function cumulative(User $u): int {return (int)DB::table('fuc_vip_accounts')->where('user_id',$u->id)->value('cumulative_cents');} public function test_default_install_and_feature_switch_are_off(): void { $this->assertFalse(app(Service::class)->enabled()); \Illuminate\Support\Facades\Schema::drop('fuc_vip_entries');\Illuminate\Support\Facades\Schema::drop('fuc_vip_accounts'); app(PluginManager::class)->install('vip_loyalty'); $this->assertFalse(Plugin::where('code','vip_loyalty')->first()->is_enabled); Plugin::where('code','vip_loyalty')->update(['is_enabled'=>true]); (new \Plugin\VipLoyalty\Plugin('vip_loyalty'))->boot();$this->assertFalse(app(Service::class)->enabled()); $u=$this->user(['discount'=>5]);$o=$this->processing($u,$this->plan());(new OrderService($o))->open(); $this->assertSame(0,DB::table('fuc_vip_entries')->count());$this->assertSame(5,$u->fresh()->discount); } public function test_original_discount_next_purchase_and_upgrade_example(): void { $s=$this->enable();$u=$this->user(['balance'=>100000]);$s->opening($u->id,$u->uuid,10000,str_repeat('a',64)); $p=$this->plan(300);$o=OrderService::createFromRequest($u,$p,'monthly'); $this->assertEquals(27600,$o->balance_amount);$this->assertEquals(0,$o->total_amount); $o->status=Order::STATUS_PROCESSING;$o->save();(new OrderService($o))->open(); $this->assertSame(37600,$this->cumulative($u));$this->assertSame(15,$u->fresh()->discount); $this->assertEquals(2400,$o->fresh()->discount_amount); $next=OrderService::createFromRequest($u->fresh(),$p,'monthly');$this->assertEquals(25500,$next->balance_amount); } public function test_threshold_crossing_does_not_reprice_current_order(): void { $s=$this->enable();$u=$this->user(['balance'=>100000]);$s->opening($u->id,$u->uuid,9900,str_repeat('b',64)); $o=OrderService::createFromRequest($u,$this->plan(2),'monthly');$this->assertEquals(200,$o->balance_amount); $o->status=1;$o->save();(new OrderService($o))->open(); $this->assertSame(10100,$this->cumulative($u));$this->assertSame(8,$u->fresh()->discount);$this->assertEquals(200,$o->fresh()->balance_amount); } public function test_mixed_payment_counts_principal_once_excluding_surplus_and_fees(): void { $this->enable();$u=$this->user();$o=$this->processing($u,$this->plan(),['total_amount'=>1000,'balance_amount'=>250,'surplus_amount'=>500,'handling_amount'=>35]); (new OrderService($o))->open();$this->assertSame(1250,$this->cumulative($u)); } public function test_duplicate_open_and_stale_models_do_not_duplicate_consumption(): void { $this->enable();$u=$this->user();$o=$this->processing($u,$this->plan());$stale=Order::find($o->id); (new OrderService($o))->open();(new OrderService($stale))->open(); $this->assertSame(10000,$this->cumulative($u));$this->assertSame(1,DB::table('fuc_vip_entries')->where('kind','purchase')->count()); } public function test_open_failure_rolls_back_vip_ledger_and_discount(): void { $s=$this->enable();$u=$this->user();$s->sync($u->id);$o=$this->processing($u,$this->plan()); HookManager::register('order.open.before',fn()=>throw new \RuntimeException('injected'),99); try{(new OrderService($o))->open();$this->fail('Expected failure');}catch(\RuntimeException $e){$this->assertSame('injected',$e->getMessage());} $this->assertSame(0,$this->cumulative($u));$this->assertSame(0,DB::table('fuc_vip_entries')->count());$this->assertSame(0,$u->fresh()->discount);$this->assertSame(1,$o->fresh()->status); } public function test_pending_cancel_and_retry_never_accrue(): void { $this->enable();$u=$this->user(['balance'=>10000]);$o=OrderService::createFromRequest($u,$this->plan(),'monthly'); $this->assertSame(0,$this->cumulative($u));$this->assertTrue((new OrderService($o))->cancel());$this->assertFalse((new OrderService($o))->cancel()); $this->assertSame(10000,$u->fresh()->balance);$this->assertSame(0,DB::table('fuc_vip_entries')->count()); } public function test_balance_credit_and_commission_transfer_do_not_accrue_before_package_purchase(): void { $s=$this->enable();$u=$this->user(['commission_balance'=>10000]);$s->sync($u->id); app(\App\Services\UserService::class)->addBalance($u->id,10000); $u->refresh();$u->balance+=10000;$u->commission_balance=0;$u->save(); $this->assertSame(0,$this->cumulative($u)); $o=OrderService::createFromRequest($u,$this->plan(),'monthly');$o->status=1;$o->save();(new OrderService($o))->open(); $this->assertSame(10000,$this->cumulative($u)); } public function test_reset_packages_do_not_accrue_but_onetime_packages_do(): void { $this->enable();$u=$this->user();$p=$this->plan();$r=$this->processing($u,$p,['period'=>'reset_traffic']);(new OrderService($r))->open(); $this->assertSame(0,DB::table('fuc_vip_entries')->count());$o=$this->processing($u,$p,['period'=>'onetime']);(new OrderService($o))->open();$this->assertSame(10000,$this->cumulative($u)); } public function test_opening_import_is_idempotent_and_cannot_overwrite_afterwards(): void { $s=$this->enable();$u=$this->user();$ref=str_repeat('c',64);$s->opening($u->id,$u->uuid,0,$ref);$s->opening($u->id,$u->uuid,0,$ref); $this->assertSame(1,DB::table('fuc_vip_entries')->count());$this->assertSame(0,$u->fresh()->discount); $this->expectException(\RuntimeException::class);$s->opening($u->id,$u->uuid,100,$ref); } public function test_opening_rejects_wrong_identity_and_negative_amount(): void { $s=$this->enable();$u=$this->user(); try{$s->opening($u->id,'other-uuid',0,str_repeat('a',64));$this->fail();}catch(\RuntimeException){} $this->assertSame(0,DB::table('fuc_vip_accounts')->count()); $this->expectException(\InvalidArgumentException::class);$s->opening($u->id,$u->uuid,-1,str_repeat('a',64)); } public function test_manual_discount_preserved_when_disabling_and_reenabling(): void { $s=$this->enable();$u=$this->user(['discount'=>10]);$s->sync($u->id); $o=$this->processing($u,$this->plan(),['total_amount'=>40000]);(new OrderService($o))->open();$this->assertSame(15,$u->fresh()->discount); app(PluginManager::class)->disable('vip_loyalty');$this->assertSame(10,$u->fresh()->discount); $this->assertSame(40000,$this->cumulative($u));$this->enable();$s->sync($u->id);$this->assertSame(15,$u->fresh()->discount); } public function test_native_manual_discount_edit_is_not_lost(): void { $s=$this->enable();$u=$this->user();$s->sync($u->id);$u->discount=20;$u->save(); HookManager::call('admin.user.update.after',['user'=>$u,'params'=>['discount'=>20]]);$this->assertSame(20,$u->fresh()->discount); app(PluginManager::class)->disable('vip_loyalty');$this->assertSame(20,$u->fresh()->discount); } public function test_refund_reversal_is_bounded_idempotent_and_recalculates_level(): void { $s=$this->enable();$u=$this->user();$o=$this->processing($u,$this->plan(),['total_amount'=>40000]);(new OrderService($o))->open(); $balance=$u->fresh()->balance;$s->reverse($o->id,30000,'verified-refund');$s->reverse($o->id,30000,'verified-refund'); $this->assertSame(10000,$this->cumulative($u));$this->assertSame(8,$u->fresh()->discount);$this->assertSame($balance,$u->fresh()->balance); $this->expectException(\Illuminate\Validation\ValidationException::class);$s->reverse($o->id,10001,'over-refund'); } public function test_config_edit_is_instance_scoped_and_validated(): void { $s=$this->enable();$u=$this->user();$s->opening($u->id,$u->uuid,10000,str_repeat('d',64)); $cfg=array_replace(Settings::DEFAULTS,['enabled'=>true,'tier1_discount'=>9]);Plugin::where('code','vip_loyalty')->update(['config'=>json_encode($cfg)]); $this->assertSame(9,$s->summary($u->id)['discount_percent']); $this->expectException(\Illuminate\Validation\ValidationException::class);app(Settings::class)->validate(['tier2_amount'=>50]); } public function test_native_plugin_editor_cannot_save_invalid_vip_policy(): void { $this->enable();$before=Plugin::where('code','vip_loyalty')->value('config'); try{app(\App\Services\Plugin\PluginConfigService::class)->updateConfig('vip_loyalty',['tier2_amount'=>1]);$this->fail();}catch(\Illuminate\Validation\ValidationException){} $this->assertSame($before,Plugin::where('code','vip_loyalty')->value('config')); } public function test_corrupt_enabled_policy_blocks_accrual_instead_of_silently_skipping(): void { $this->enable(['tier2_amount'=>1]);$u=$this->user(); $this->expectException(\Illuminate\Validation\ValidationException::class); OrderService::createFromRequest($u,$this->plan(),'monthly'); } public function test_user_endpoint_and_admin_settings_permissions(): void { $this->enable();app(PluginManager::class)->initializeEnabledPlugins(); $this->getJson('/api/v1/user/vip')->assertStatus(403); $u=$this->user();$this->actingAs($u,'sanctum');$this->getJson('/api/v1/user/vip')->assertOk()->assertJsonPath('data.cumulative_cents',0); $secure=admin_setting('secure_path',admin_setting('frontend_admin_path',hash('crc32b',config('app.key'))));$url='/api/v2/'.$secure.'/vip'; $this->postJson($url,['enabled'=>false])->assertStatus(403); $u->is_admin=true;$u->save();$this->actingAs($u,'sanctum');$this->postJson($url,['enabled'=>true,'tier2_amount'=>1])->assertStatus(422); $this->postJson($url,['enabled'=>false])->assertOk()->assertJsonPath('data.enabled',false); } }