Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

scwall's avatar

mock guzzle before testing a job

Hello Here I am doing tasks in workers every 5 minutes, I would like to do some tests to see if it responds well. I try to mock guzzle but it doesn't work here's what I did:

in test :

 $guzzleMock = Mockery::mock(Client::class);
        $guzzleMock ->shouldReceive('request') ->andReturn(true);
        $this->app->instance(Client::class, $guzzleMock);
		$job = new \App\Jobs\ProcessCheck;
        $job->handle();

in process :

  protected static function pp_client($method,$params=[]){
        $params = json_encode($params);
        $client = new Client();
        $opts = [
            'body' =>  '{"id":"curltext","params":'.$params.'}',
            'headers' => ['Content-Type' => 'text/plain'],
        ];
       $response = $client->request('POST','').':8000', $opts);

but it doesn't work I still have a request that is made with the unpatched guzzle

GuzzleHttp\Exception\ConnectException : cURL error 7: Failed to connect to 

Thank's in advance

0 likes
2 replies
martinbean's avatar

@scwall It’s not working because you don’t resolve Guzzle via the container; you’re justing instantiating it directly with the new keyword.

scwall's avatar

Agreed that it would be the best way to proceed to test a job knowing that I have to pass parameters to it? Thank you in advance

Please or to participate in this conversation.