php - UnitTest Slim's Error Handler Responses -
i'm developing api service using slim php v3 , i'm trying unit test development using phpunit.
i have following in controller( usercontroller) mapped route in slim
try { $model = $this->model->findorfail($args['id']); $response->write($model); } catch (modelnotfoundexception $e) { throw new \exception($e->getmessage()); } return $response;
to slim "handle error" , return 500 status code( works when execute in browser)
now have following test( userstest)...
protected function request($method, $url, array $requestparameters = []) { ob_start(); $request = $this->preparerequest($method, $url, $requestparameters); $response = new response(); $app = $this->app; $this->response = $app($request, $response); return ob_get_clean(); } function testgetuserfail() { $response = $this->request('get','/users/1',[]); $this->assertthatresponsehasstatus(500, print_r($this->responsedata(), true)); // , assertequals test } protected function setup() { putenv('env=testing'); require __dir__ . '/../../src/bootstrap.php'; $this->app = $app; $c = $app->getcontainer(); $c['settings']['displayerrordetails'] = ('development' === (getenv('env') ?: 'development')); $c['errorhandler'] = function ($c) { return function ($request, $response, $exception) use ($c) { return $c['response']->withstatus(500) ->withheader('content-type', 'application/json') ->write(json_encode(['error'=>true, 'error_msg'=>$exception->getmessage()])); }; }; //$app->run(); }
the test detects whether or not mock request sends correct status code reason errors out on thrown exception rather continuing on returning propoer status code
there 1 error:
1) userstest::testgetuserfail exception: no query results model [app\model\users].
~/app/src/controller/userscontroller.php:32
failures! tests: 2, assertions: 1, errors: 1. complete.
like said if run code in browser works expected not in phpunit test...
is testing logic wrong? should testing exception , not expected status code?
Comments
Post a Comment