title: How do I write unit tests for a REST API built in Slim Framework?
body: <p>I have an API built in Slim like this:</p>
  
  <pre><code>    $app-&gt;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-&gt;get('(/:id)', function ($id) use ($app, $handler) {
              echo json_encode($handler-&gt;get($id));
          });
  
          $app-&gt;post('', function () use ($app, $handler) {
              echo json_encode($handler-&gt;post($app-&gt;request-&gt;post()));
          });
  
          $app-&gt;put(':id', function ($id) use ($app, $handler) {
              echo json_encode($handler-&gt;put($id, $app-&gt;request-&gt;put()));
          });
  
          $app-&gt;delete(':id', function ($id) use ($app, $handler) {
              echo json_encode($handler-&gt;delete($id));
          });
  
      });
  </code></pre>
  
  <p><code>$endpoint</code> is a string, for example 'user';</p>
  
  <p>How do I go about writing unit tests for it? </p>
  
  <p>Ideally;</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>(The REST handler class would make it trivial to mock the results that would ordinarily be backed by a DB.)</p>
  
  <p>It's the nitty gritty of how to spoof the request into Slim that I don't know how to do.</p>