I have an API built in Slim like this:
$app->group('/'.$endpoint, function () use ($app, $endpoint) { $handler = Api\Rest\Handlers\Factory::load($endpoint); if (is_null($handler)) { throw new \Exception('No REST handler found for '.$endpoint); } $app->get('(/:id)', function ($id) use ($app, $handler) { echo json_encode($handler->get($id)); }); $app->post('', function () use ($app, $handler) { echo json_encode($handler->post($app->request->post())); }); $app->put(':id', function ($id) use ($app, $handler) { echo json_encode($handler->put($id, $app->request->put())); }); $app->delete(':id', function ($id) use ($app, $handler) { echo json_encode($handler->delete($id)); }); });
$endpoint
is a string, for example 'user';
How do I go about writing unit tests for it?
Ideally;
class RestUserTest extends PHPUnitFrameworkTestCase { public function testGet() { $slim = ''; // my slim app // set route 'api/user/1' with method GET // invoke slim // ensure that what we expected to happen did }}
(The REST handler class would make it trivial to mock the results that would ordinarily be backed by a DB.)
It's the nitty gritty of how to spoof the request into Slim that I don't know how to do.