title: Php Slim framework unittest mock Router
body: <p>I have created a wrapper for the Slim request in my application to have the ability to create some custom methods on the Request object.</p>
  
  <pre><code>class Request extends SlimRequest
  {
     /**
      * Get authorization token.
      *
      * @return null|string
      */
     public function getAuthToken(): ?string
     {
         return $this-&gt;getHeaderLine('FOOBAR-TOKEN');
     }
  
     /**
      * Retrieves a route parameter.
      *
      * @param string $name
      * @return string|null
      */
     public function getRouteParam(string $name): ?string
     {
         return $this-&gt;getRoute()-&gt;getArgument($name);
     }
  
     /**
      * Retrieves the route instance.
      *
      * @return Route
      */
     public function getRoute(): Route
     {
         return $this-&gt;getAttribute('route');
     }
  </code></pre>
  
  <p>}</p>
  
  <p>My problem comes when trying to create unit test for this class. The way I have been testing requests is by using Slims build in environment mocks. The first function I added a header to the request which can be seen below, but I can't figure out how to add a Route object to the request</p>
  
  <pre><code>$request = Request::createFromEnvironment(Environment::mock());
  $request = $request-&gt;withHeader('FOOBAR-TOKEN', 'superSafeExampleToken');
  </code></pre>
  
  <p>I tried creating the request with a request options but $this->getAttribute('route'); returns null</p>
  
  <pre><code>   $requestOptions = [
          'REQUEST_METHOD' =&gt; 'POST,
          'REQUEST_URI' =&gt; '/foo/bar',
          'QUERY_STRING' =&gt; http_build_query($requestParameters),
      ];
      $environment = Environment::mock($requestOptions);
  </code></pre>