簡體   English   中英

Laravel 5.2: How to test helper function?

[英]Laravel 5.2: How to test helper function?

I'm new to laravel. I have read the laravel documentation on testing but I do not seem to understand it. Could someone guide me on the steps to test this helper function?

下面是function。

function getUnreadInboxMessagesForAUserById($id)
{

    $past_period_months = Carbon::now()->subMonths(6);

    $message = App\Message::select("*")
    ->whereIn("conversation_id", App\Conversation::select("id")
    ->where([
        ["user_one", "=", $id],
        ["delete_user_one", "=", 0]
    ])
    ->where(function($query) {
        $query->where("created_at", ">", $user_one_convo_deleted_at)->orWhereNull("user_one_convo_deleted_at");
    })
    ->where(function($query) {
        $query->where("created_at", ">", $user_one_msgs_deleted_at)->orWhereNull("user_one_msgs_deleted_at");
    })
    ->orWhere(function($query) {
        $query->where([
        ["user_two", "=", $id],
        ["delete_user_two", "=", 0]
        ])
        ->where(function($query) {
        $query->where("created_at", ">", $user_two_convo_deleted_at)->orWhereNull("user_two_convo_deleted_at");
        })
        ->where(function($query) {
        $query->where("created_at", ">", $user_two_msgs_deleted_at)->orWhereNull("user_two_msgs_deleted_at");
        });
    })
    )
    ->where([
    ["is_seen", "=", 0],
    ["user_id", "<>", $id]
    ])
    ->whereNotIn("user_id", App\DeactivateAccount::select("user_id"))
    ->where(function($query) {
    $query->whereNotIn("user_id", App\User::select("id")
        ->where([
        ["flag", "=", 0],
        ["id", "=", $user_id]
        ])
        ->orWhereIn("user_id", App\User::select("id")
            ->where("id", "=", $user_id)
            ->whereIn("membership", ["admin","premium","free-premium","diamond"]))
        );
    })
    ->where(function($query) {
    $query->where("hard_delete", "=", 0)->orWhereNull("hard_delete");
    })
    ->where(function($query) {
    $query->where("deleted_from_receiver", "=", 0)->orWhereNull("deleted_from_receiver");
    })
    ->where(function($query) {
    $query->where("isflaged", "!=", 1)->orWhereNull("isflaged");
    })
    ->where("created_at", ">=", $past_period_months)
    ->orderBy("created_at", "desc")
    ->get()
    ->count();

    return $messages[0];
}

It would be really helpful if you could list the steps in detail.

使用 laravel 思考者:

php artisan thinker

然后運行你的class of function 教程

在 app 文件夾中為 Helper 函數創建單獨的文件夾,假設 helper 文件名為common.php ,路徑為app/Helpers/Common.php

您可以創建一個服務提供者並將文件加載到該提供者中並將其導入到 config/app.php 中的提供者中,或者您可以嘗試使用 composer 自動加載

如果你想使用服務提供商

創建一個輔助服務提供者 class: app/Providers/HelperServiceProvider.php並使用下面提到的代碼

<?php

    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;

    class HelperServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap the application services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }

        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {   
            /**
             * Path to the helper file
             */
            require_once __DIR__ . '/../Helpers/Common.php';
        }
    }

 

如果你想使用 composer autoload 打開 composer.json

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Helpers/Common.php" // <---- ADD THIS PATH TO THE HELPER FILE
    ]
},

一旦更新了 composer.json 運行composer dump-autoload

我個人推薦使用Service Provider。

正確導入所有內容后,您可以在項目的任何位置使用getUnreadInboxMessagesForAUserById($id) function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM