如何在laravel中为guzzle请求编写单元测试?

问题描述 投票:0回答:1

我有一个登录功能,通过Guzzle Http请求进行卷曲调用.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编写单元测试用例。怎么做?

laravel guzzle
1个回答
0
投票

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

这两个选项都适用于PHPUnit,并为您提供了一种模拟和内省查询的方法,而无需真正执行它们。

© www.soinside.com 2019 - 2024. All rights reserved.