Как мне проверить функцию фильтра таблицы в люменах? - PullRequest
2 голосов
/ 20 февраля 2020

Это мой внутренний код. Я написал тест для этой функции следующим образом:

public function index(Request $request)
    {
        $fields = 'in:' . implode(',', Schema::getColumnListing('suppliers'));

        $messages = [
            'order_by.in' => 'The selected column name is invalid.',
        ];
        $this->validate($request, [
            'page' => ['numeric'],
            'per_page' => ['numeric'],
            'order_direction' => ['in:desc,asc,DESC,ASC'],
            'order_by' => [$fields],
        ], $messages);
        $perPage = 10;
        $filter = '';
        $orderBy = 'id';
        $orderDirection = 'DESC';

        try {
            if ($request->has('per_page')) $perPage = (int)$request->per_page;

            if ($request->has('filter')) $filter = $request->filter;

            if ($request->has('order_by')) $orderBy = $request->order_by;

            if ($request->has('order_direction')) $orderDirection = strtoupper($request->order_direction);

            $suppliers = Supplier::select('id', 'firstname', 'lastname', 'phone', 'email', 'custom_field_1', 'custom_field_2')->where('store_id', Auth::user()->store);
            if (!!$filter) {
                $suppliers->where('id', 'LIKE', "%{$filter}%")
                    ->orWhere('firstname', 'LIKE', "%{$filter}%")
                    ->orWhere('lastname', 'LIKE', "%{$filter}%")
                    ->orWhere('email', 'LIKE', "%{$filter}%")
                    ->orWhere('phone', 'LIKE', "%{$filter}%");
            }
            $suppliers->orderBy($orderBy, $orderDirection);

            $suppliers = $suppliers->paginate($perPage);

            return response()->json([
                'success' => true,
                'data' => $suppliers->toArray(),
            ], Response::HTTP_OK);
        } catch (Exception $e) {
            report($e);
            return serverExceptionMessage();
        }
    }

Это то, что я пробовал и проверяю. Я создаю магазин, потому что store_id - это внешний ключ в моей таблице поставщиков , динамически добавляю поставщиков, фильтрую firstname по URL и получаю результат из seeJson в виде массива:

    public function testSupplierListViewPaginationFilterTest()
        {
            $user = factory('App\Models\User')->make();
            $store = (factory('App\Models\Store')->make())->getAttributes();

            $this->actingAs($user)
                ->post('/create-new-store', $store)
                ->seeStatusCode(Response::HTTP_OK);
            $attributes = [
                'id' => 1,
                'firstname' => 'ashid',
                'lastname' => 'mhd',
                'phone' => 776358547,
                'email' => 'ashid@email.com',
                'custom_field_1' => 'test',
                'custom_field_2' => 'test',
            ];
            $user->store = Store::latest()->first()->id;
            $attributes['store_id'] = Auth::user()->store;
            $supplier = Supplier::create($attributes);
            $this->actingAs($user)
                ->get('/suppliers?filter=' . $supplier['firstname'], $attributes)
                ->seeStatusCode(Response::HTTP_OK)->seeJson([
                    'success' => true,
                    "data" =>
                        [
                            'id' => 1,
                            'firstname' => 'ashid',
                            'lastname' => 'mhd',
                            'phone' => 776358547,
                            'email' => 'ashid@email.com',
                            'custom_field_1' => 'test',
                            'custom_field_2' => 'test',
                        ]
                ]);
        }

Я получаю следующую ошибку:

    1) SupplierTest::testSupplierListViewPaginationFilterTest
Unable to find JSON fragment 
["data":"custom_field_1":"test","custom_field_2":"test","email":"ashid@email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":776358547}] within [{"data":{"current_page":1,"data":[{"custom_field_1":"test","custom_field_2":"test","email":"ashid@email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":"776358547"}],"first_page_url":"http:\/\/localhost\/suppliers?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/suppliers?page=1","next_page_url":null,"path":"http:\/\/localhost\/suppliers","per_page":10,"prev_page_url":null,"to":1,"total":1},"success":true}].

Failed asserting that false is true.

Мне нужно лучшее решение этой проблемы.

...