lundi 16 novembre 2020

Unit test orWhereHas,whereHas Conditions using fakers

I'm new to both unit testing, Fakers and laravel. I'm trying to unit test my resource controller, but I'm stuck at the serach function.

I would like to know how can I test "orwhere /orWhereHas .. " condition using unit testing and fakers

Here's my controller:

public function search(Request $request, Order $order)
    {
        $order = $order->newQuery();
        $order->with([
            'contact',
            'contact.company',
            'transport_order',
            'transport_order.driver',
        ]);
        if ($request->has('f_only_orders') && $request->f_only_orders == "true") {
            $order->where('type', 'order');
        }
        $order->orWhere('external_ref', 'ILIKE', "%$request->q%");

        $order->orWhereHas(
            'transport_order',
            function ($query) use ($request) {
                $query->whereHas(
                    'charter',
                    function ($query) use ($request) {
                        $query->where('name', 'ILIKE', "%$request->q%");
                    }
                );
                $query->orWhereHas(
                    'waypoint.address',
                    function ($query) use ($request) {
                        $query->where('zipcode', 'ILIKE', "%$request->q%");
                    }
                );
           
            }
        );
        return $order
            ->with([
                'contact',
                'contact.company',
            ])

            ->paginate(($request->size) ? Intval($request->size) : Config::get('user_settings.pagination'));
    }

Any help is welcome ! Thank you very much !

Aucun commentaire:

Enregistrer un commentaire