title: Slim 3 Integration Test For POST Method Always Not Included Header
body: <p>I'm currently developing an application written using Slim 3 framework, and trying to use the TDD concept, but I'm facing several issues, including when using the test for the POST method, the headers that I embed are always considered missing.</p>
  <p>Below is my code</p>
  <pre><code>&lt;?php
  
  namespace Tests\routes\shopify;
  
  use PHPUnit\Framework\TestCase;
  use Slim\App;
  use Slim\Http\Environment;
  use Slim\Http\Headers;
  use Slim\Http\Request;
  use Slim\Http\RequestBody;
  use Slim\Http\Response;
  use Slim\Http\UploadedFile;
  use Slim\Http\Uri;
  
  class ShopifyRoutesTest extends TestCase
  {
      private $app;
  
      protected function setUp(): void
      {
          // Use the application settings
          $settings = require __DIR__ . '/../../../src/settings.php';
  
          // Instantiate the application
          $this-&gt;app = new App($settings);
  
          // Set up dependencies
          $dependencies = require __DIR__ . '/../../../src/dependencies.php';
          $dependencies($this-&gt;app);
  
          // Register middleware
          $middleware = require __DIR__ . '/../../../src/middleware.php';
          $middleware($this-&gt;app);
  
          // Register routes
          $routes = require __DIR__ . '/../../../src/app/routes/api/shopify/routes.php';
          $routes($this-&gt;app);
      }
  
      public function testPostSyncProductBySkuWithEmptyApikeyShouldReturnBadRequest()
      {
          // Create a mock environment for testing with
          $environment = Environment::mock();
          $uri = Uri::createFromString(&quot;/channel/shopify/v1/product/sync-by-sku&quot;);
          $headers = new Headers(array(
              &quot;Content-Type&quot; =&gt; &quot;application/json&quot;,
              &quot;Authorization&quot; =&gt; &quot;client-apikey&quot;,
              &quot;x-api-key&quot; =&gt; &quot;&quot;
          ));
          $cookies = [];
          $serverParams = $environment-&gt;all();
          $body = new RequestBody();
          $uploadedFiles = UploadedFile::createFromEnvironment($environment);
  
          // Set up a request object based on the environment
          $request = new Request(&quot;POST&quot;, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
  
          $reqBody = array(
              &quot;shop_name&quot; =&gt; &quot;STORE ABCE&quot;,
              &quot;sku&quot; =&gt; &quot;SKU-001&quot;
          );
  
          $body-&gt;write(json_encode($reqBody));
  
  
          // Add request data, if it exists
          $request = $request-&gt;withParsedBody($reqBody);
          // Set up a response object
          $response = new Response();
  
          $response = $this-&gt;app-&gt;process($request, $response);
  
          self::assertEquals(400, $response-&gt;getStatusCode());
          self::assertStringContainsString(&quot;Header: x-api-key cannot be empty&quot;, $response-&gt;getBody());
      }
  
      protected function tearDown(): void
      {
          $this-&gt;app = null;
      }
  }
  
  
  </code></pre>
  <p>the first assertion succeeds with a value of 400, but in the second assertion its fails, the string doesn't contain the value <strong>&quot;Header: x-api-key cannot be empty&quot;</strong> but instead this</p>
  <pre><code>Failed asserting that '{\n
      &quot;error&quot;: &quot;AUTH_FAIL&quot;,\n
      &quot;errorDetail&quot;: &quot;Header: Authorization, x-api-key required&quot;,\n
      &quot;status&quot;: 400,\n
      &quot;message&quot;: &quot;BAD_REQUEST&quot;,\n
      &quot;data&quot;: &quot;&quot;\n
  }' contains &quot;Header: x-api-key cannot be empty&quot;.
  
  </code></pre>
  <p>the strange thing is that when I <strong>var_dump($request-&gt;getHeaders())</strong> the headers value of the request I made it turns out to be <strong>there</strong></p>
  <pre><code>array(3) {
    [&quot;Content-Type&quot;]=&gt;
    array(1) {
      [0]=&gt;
      string(16) &quot;application/json&quot;
    }
    [&quot;Authorization&quot;]=&gt;
    array(1) {
      [0]=&gt;
      string(13) &quot;client-apikey&quot;
    }
    [&quot;x-api-key&quot;]=&gt;
    array(1) {
      [0]=&gt;
      string(0) &quot;&quot;
    }
  }
  </code></pre>
  <p>I've also tried testing my API endpoint using Postman, and the results are as expected</p>
  <p><strong>Request</strong></p>
  <pre><code>curl --location --request POST 'http://localhost:8080/channel/shopify/v1/product/sync-by-sku' \
  --header 'Authorization: client-apikey' \
  --header 'x-api-key: 1293129382938' \
  --header 'Content-Type: application/json' \
  --header 'Cookie: PHPSESSID=tll8s24tp253rda1harv0koapi' \
  --data-raw '{
      &quot;shop_name&quot; : &quot;STORE ABC&quot;,
      &quot;sku&quot; : &quot;SKU-991&quot;
  }'
  </code></pre>
  <p><strong>Response</strong></p>
  <pre><code>{
      &quot;error&quot;: &quot;AUTH_FAIL&quot;,
      &quot;errorDetail&quot;: &quot;Header: x-api-key cannot be empty&quot;,
      &quot;status&quot;: 400,
      &quot;message&quot;: &quot;BAD_REQUEST&quot;,
      &quot;data&quot;: &quot;&quot;
  }
  </code></pre>
  <p>Also I've read the answer from stakoverflow as described here <a href="https://stackoverflow.com/questions/40867377/mock-slim-endpoint-post-requests-with-phpunit">Mock Slim endpoint POST requests with PHPUnit</a></p>
  <p>But still I can't find the solution, the header is always presumed to be missing. I really appreciate the solution to this problem, thank you in advance</p>