Laravel-Testing-Decoded
Laravel-Testing-Decoded copied to clipboard
TestCase Overloading
On page 143 you discuss overloading. You state you add the following method to TestCase.php:
// TestCase.php
public function __call($method, $args)
{
if ( in_array($method, ['get', 'post', 'put', 'patch', 'delete']) ) {
return $this->call($method, $args[0]);
}
throw new BadMethodCallException;
}
However, when you were to call this with $method being 'post', 'put' or 'patch', you'd want to set extra arguments, like so:
$this->post('users', $someArrayOfUserInfo);
Since you only use $args[0] in return $this->call($method, $args[0]);, the array of user info never gets passed.
I've been able to make my tests work again with the following change, although I think it's rather ugly:
// TestCase.php
public function __call($method, $args)
{
if ( in_array($method, ['get', 'post', 'put', 'patch', 'delete']) ) {
// I added this next line:
if ( count($args) > 1 ) return $this->call($method, $args[0], $args[1]);
return $this->call($method, $args[0]);
}
throw new BadMethodCallException;
}
+1 for this bug. It took me an hour to work out why I couldnt post anything - I thought it was my controller!!
+1 for the solution - it works perfectly.