mardi 7 avril 2015

Laravel 5 unit testing. Unable to set JSON of Request object

How on earth do you get Laravel 5.0 to accept a JSON encoded string into it's request object? Because my REST api is returning 500 errors, and upon closer inspection, the request object has an empty json property...?


My array:



private $test_1_create_user = array(
"name" => "Mr T Est",
"email" => "mrtest@somedomain.com",
"password" => "testing1234"
);


My test method:



/**
* Attempts to Create a single user with no permissions
*/
public function testCreateUser(){
/** Obtain instance of Request object */
$req = $this->app->request->instance();
/** Set the JSON packet */
$req->json(json_encode($this->test_1_create_user));
/** Run the test */
$response = $this->call('POST', '/api/v1/user');
/** Read the response */
$this->assertResponseOk();
}


And a var_dump of $req (slimmed down a bit):



C:\wamp\www\nps>php phpunit.phar
PHPUnit 4.6.2 by Sebastian Bergmann and contributors.

Configuration read from C:\wamp\www\nps\phpunit.xml

class Illuminate\Http\Request#34 (25) {
protected $json =>
class Symfony\Component\HttpFoundation\ParameterBag#261 (1) {
protected $parameters =>
array(0) {
}
}
protected $sessionStore =>
NULL
protected $userResolver =>
NULL
protected $routeResolver =>
NULL
public $attributes =>
class Symfony\Component\HttpFoundation\ParameterBag#41 (1) {
protected $parameters =>
array(0) {
}
}
public $request =>
class Symfony\Component\HttpFoundation\ParameterBag#43 (1) {
protected $parameters =>
array(0) {
}
}


It took me quite a while to figure out how to access the request object from within a unit test. Anyone have any ideas as to why the $req->json is always empty? :( Cheers!


Aucun commentaire:

Enregistrer un commentaire