Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test error 404 with phpunit

Tags:

php

phpunit

The purpose of my test it's to check that a function returns error 404, or to be more accurate, function creates call to server ( i am using guzzle) and i expect the server to return error 404, but i can't find any @expectedException which make sense cause it's an ERROR, but does anyone know how can i test this case with phpunit ??? thx

in case it might help, this is how the function looks like (last attempt) :

public function testApi_deleteExecuted()
  {
     $path = '/adserver/src/public/api/rule/288';
     $client = new Client(['base_uri' => 'http://10.0.0.38']);
     $response = $client->get($path);
     $data = json_decode($response->getBody());
     $code = $response->getStatusCode();

     $this->assertEquals($code, 404);

  }
like image 463
Donoven Rally Avatar asked Feb 10 '23 10:02

Donoven Rally


1 Answers

so apparently guzzle has the answer, http_errors.

function didn't work before but it works now:

  public function testApiRule_deleteExecuted()
  {
     $path = '/adserver/src/public/api/rule/288';
     $client = new Client(['base_uri' => 'http://10.0.0.38']);
     $response = $client->get($path, ['http_errors' => false]);
     $err = $response->getStatusCode();

     $this->assertEquals($err, 404);
  }
like image 122
Donoven Rally Avatar answered Feb 12 '23 01:02

Donoven Rally