如何在Laravel中编写针对枪口请求的单元测试?

时间:2019-03-20 06:58:56

标签: laravel guzzle

我有一个登录功能,可以通过Guzzle Http请求进行curl调用。该api正常工作,现在我需要在所有情况下为同一api编写单元测试。请帮助我如何为以下功能编写单元测试。

public function login(Request $request)
    {
        $input = $request->all();

        /*
        *  check whether all the required parameters are received
        */

        $validator = Validator::make($input,[
            'username' => 'required',
            'password' => 'required'
        ]);

        if($validator->fails())
        {
            // if the validation fails send error response

            return $this->sendError('Validation Error',$validator->errors(),400);

            //EOF
        }

        /*
        *   get the username and password from Request and url value from .env file 
        *   and make a curl to  server to check the user authentication
        */

        $client = new \GuzzleHttp\Client(['http_errors' => false]);

        $response = $client->request('POST', config('link'), [
                'form_params' => [
                    'action' => 'login',
                    'username' => $input['username'],
                    'pwd' => $input['password']
                ]
            ]);

        $response_body = json_decode($response->getBody(),true);

        if(array_key_exists('success',$response_body))
        {
            if($response_body['success'] == 'yes')
            {
                $userId = $response_body['data']['profileData']['id'];
                // check if user is already registered or not

                $userObj = new User;
                $isUserExists = $userObj->checkUserExists($userId);

                // EOF

                if($isUserExists < 1)
                {
                    $createUser['id'] = $userId;
                    $createUser['name'] = $response_body['data']['profileData']['first_name']." ".$response_body['data']['profileData']['last_name'];
                    $createUser['email'] = $response_body['data']['profileData']['email'];
                    $createUser['password'] = bcrypt($input['password']);
                    $createUser['company_id'] = $response_body['data']['profileData']['company_id'];

                    $user = User::create($createUser);
                }
                else
                {
                    $user = User::find($userId);
                }

                // check if company already exists or not else register new company

                $companyObj = new Company;

                $companyId = $response_body['data']['profileData']['company_id'];
                $isCompanyExists = $companyObj->checkCompanyExists($companyId);

                if($isCompanyExists < 1)
                {
                    $createCompany['id'] = $companyId;
                    $createCompany['name'] = $response_body['data']['profileData']['comp_name'];

                    $company = Company::create($createCompany);
                }

                // EOF

                // send the response back to the appliaction

                $success['user_id'] = $userId;
                $success['company_id'] = $response_body['data']['profileData']['company_id'];
                $success['token'] =  $user->createToken('silva-reborn')-> accessToken;


                return $this->sendResponse($success,'Login successful');

                // EOF
            }
            else
            {
                // Invalid login

                return $this->sendError($response_body['msg'], array() ,401);

                // EOF
            }
        }
        else
        {
            return $this->sendError("Invalid url", '' ,404);
        }
    }

此api现在可以正常工作,我需要为此api编写单元测试用例。该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为在Laravel中没有测试Guzzle相关代码的特殊方法,但是一般的方法是采用Guzzler或History Middleware(阅读more in the docs)。

这两个选项都可以与PHPUnit一起很好地工作,并为您提供了一种在不真正执行查询的情况下模拟和内省查询的方法。