Laravel-Testing-Decoded icon indicating copy to clipboard operation
Laravel-Testing-Decoded copied to clipboard

TestCase Overloading

Open roggel2000 opened this issue 13 years ago • 1 comments

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;
}

roggel2000 avatar Jun 07 '13 21:06 roggel2000

+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.

laurencei avatar Jul 06 '13 12:07 laurencei