kenjis/ci-phpunit-test

Trying to request Raw input

shuvojittechpro opened this issue · 2 comments

Hi Kenjis,

I am trying to test an API of mine which excepts a single parameter and run some logics

Below is my API function

public function get_all_branches_by_userID_regional()
	{
		$data = json_decode($this->input->raw_input_stream);
		echo 'try';
		print_r($data);die;
		try{
			if(!empty($data)){
				$user_id = $data->user_id;
				$res = $this->summer_pay_model->get_branches_by_userID_regional($user_id);
				if($res['status'] == 'true'){
					$result = array('status' => 'true');
					$result['records'] = $res['records'];
				}
				else{
					throw new Exception($res['message']);
				}
			}
			else{
				throw new Exception("Data is empty");
			}
		}catch(Exception $e){
			$result = array('status' => 'false');
			$result['message'] = $e->getMessage();
		}

		echo json_encode($result);
	}

And the following is my test function

public function test_getBranchesByUserIDRegional()
 {
        $this->request->setHeader('Content-Type', 'application/json');
        $this->request->setHeader('year', '2021');
        $output = $this->request('POST', 'summer_pay/get_all_branches_by_userID_regional', 'user_id=1');
       
        print_r($output);die;
    }

as it's written in API function I am trying to print the raw data but it shows blank while calling from test although it is printing the "try" string.
I tested the same thing from Postman and the call and payloads are working.

Can you please help me with this!

currently bypassing the issue by using Guzzle HTTP. But it would be better if we can use the one with this setup.

@shuvojittechpro It seems 'user_id=1' is not a valid JSON string.