首頁  >  問答  >  主體

如何將函數從閉包轉移到普通函數

<p>我正在嘗試讓害蟲測試文件更易於閱讀。 </p> <p>目前,我有一些標準測試:</p> <pre class="brush:php;toolbar:false;">test('can get subscribers latest subscription', function () { $this->seed(PlansTestSeeder::class); $this->seed(SubscriptionsTestSeeder::class); $this->assertDatabaseCount('plans', 2); $this->assertDatabaseCount('subscriptions', 0); Subscription::factory()->create([ "plan_id" => Plan::where("slug", "bronze")->first()->id ]); Subscription::factory()->create([ "plan_id" => Plan::where("slug", "silver")->first()->id ]); Subscription::factory()->create([ "plan_id" => Plan::where("slug", "silver")->first()->id, "status" => "expired" ]); Subscription::factory()->trashed()->create(); $this->assertDatabaseCount('subscriptions', 4); }); test('can get subscribers active subscriptions', function () { $this->seed(PlansTestSeeder::class); $this->seed(SubscriptionsTestSeeder::class); $silverPlan = Plan::where("slug", "silver")->first(); $subscription1 = Subscription::factory()->create([ "plan_id" => Plan::where("slug", "silver")->first()->id, "subscriber_id" => 1, "subscriber_type" => "ApresourcingFramework\Billing\Tests\Models\Subscriber", "created_at" => now()->subDays(2), "started_at" => now()->subDays(2) ]); $subscription2 = Subscription::factory()->create([ "plan_id" => $silverPlan->id, "subscriber_id" => 1, "subscriber_type" => "ApresourcingFramework\Billing\Tests\Models\Subscriber", "created_at" => now()->subDays(1), "started_at" => now()->subDays(1) ]); $user = Subscriber::find(1); $subscription = $user->latestSubscription(); expect($subscription->id)->toBe($subscription2->id); });</pre> <p>但為了提醒自己我寫了哪些測試,我必須一遍又一遍地上下滾動頁面。</p> <p>我想做的是更改為以下內容:</p> <pre class="brush:php;toolbar:false;">test('can get subscribers latest subscription', getLatestSubscription()); test('can get subscribers active subscriptions', getActiveSubscriptions()); function getLatestSubscription() { /// function code here }); function getActiveSubscriptions() { // function code here });</pre> <p>但是,測試函數包含對 $this 的引用,它在正常閉包中可用,但在標準函數中不可用,因為我在此處設定了它。 </p> <p>編輯:我正在使用 laravel pest 外掛 - 我不確定這是否會對 $this 的使用產生影響</p> <p>有辦法解決這個問題嗎? </p>
P粉831310404P粉831310404382 天前536

全部回覆(1)我來回復

  • P粉563446579

    P粉5634465792023-09-06 21:24:29

    感謝回覆中的一些提示。不像我希望的那樣整潔,但至少這意味著所有測試(“測試描述”)呼叫都位於 php 檔案底部的一個位置。

    $createSubscription = function () {
    
        $this->seed(PlansTestSeeder::class);
        $this->seed(SubscriptionsTestSeeder::class);
    
        $this->assertDatabaseCount('plans', 2);
        $this->assertDatabaseCount('subscriptions', 0);
    
    
        Subscription::factory()->create([
            "plan_id" => Plan::where("slug", "bronze")->first()->id
        ]);
        Subscription::factory()->create([
            "plan_id" => Plan::where("slug", "silver")->first()->id
        ]);
        Subscription::factory()->create([
            "plan_id" => Plan::where("slug", "silver")->first()->id,
            "status"  => "expired"
        ]);
        Subscription::factory()->trashed()->create();
    
        $this->assertDatabaseCount('subscriptions', 4);
    
    };
    
    $createBronzeSubscription = function () {
        $this->seed(PlansTestSeeder::class);
        $this->seed(SubscriptionsTestSeeder::class);
    
        Subscription::factory()->create([
            "plan_id" => Plan::where("slug", "bronze")->first()->id
        ]);
    
        $this->assertDatabaseCount('subscriptions', 1);
    };
    
    
    test('can create subscription', function () use ($createSubscription) {
        return \Closure::bind(\Closure::fromCallable($createSubscription), $this, get_class($this))($this);
    });
    
    test('can create bronze subscription', function () use ($createBronzeSubscription) {
        return \Closure::bind(\Closure::fromCallable($createBronzeSubscription), $this, get_class($this))($this);
    });

    回覆
    0
  • 取消回覆