Skip to content

Instantly share code, notes, and snippets.

@afsardo
Last active June 29, 2017 21:58
Show Gist options
  • Save afsardo/3697ecce2ce444036c8743139143bee6 to your computer and use it in GitHub Desktop.
Save afsardo/3697ecce2ce444036c8743139143bee6 to your computer and use it in GitHub Desktop.
Wondering why the $_POST global variable has no values when running tests?

Wondering why the $_POST global variable has no values when running tests?

Today, someone asked my help when using an external package that needed to access the $_POST variable, it wasn't being set when running the test suite while working when getting a real request. It got me investigating.

Turns out Laravel instantiates the Requests and have them through the framework, so it basicly fakes an incoming native request.

Use the gist bellow to get the trick working, unless you are using an external package you should rethink when using $_POST nowadays, but hey, I'm not judging.

<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function post($uri, array $data = [], array $headers = [])
{
$_POST = array_merge($_POST, $data);
return parent::post($uri, $data, $headers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment