I\'m using the response() helper very often and I just return the data with a message to the user. Now I have to include the http status code as well, but I don
I've just had to do this in order to override the now() helper so I can control the apparent time when running tests. I followed the regular advice of creating app/Http/helpers.php and then adding it to bootstrap/autoload.php like so:
require __DIR__.'/../app/Http/helpers.php'; // added
require __DIR__.'/../vendor/autoload.php';
This usually works because as Marty says, all helpers are only defined if there's not an existing function with that name. So the two lines above load your custom helpers, then perform all vendor autoloading, which includes Laravel's helpers, and your already-defined function takes precedence.
But unfortunately, autoload.php doesn't seem to be used when testing with Behat, which is what I'm using. So I needed an alternative solution. Long story short, the only easy way to ensure that files get autoloaded before vendor files is by using the https://github.com/funkjedi/composer-include-files package. To quote from its readme:
In the past simply modifying
bootstrap/autoload.phpto include helpers was sufficient. However new versions of PHPUnit include the Composer Autoloader prior to executing the PHPUnit bootstrap file. Consequently this method of overriding helpers is no longer viable as it will trigger a fatal error when your bootstrap file is included.
So I installed this package using composer require funkjedi/composer-include-files and then added this to composer.json:
"extra": {
"include_files": [
"app/Http/helpers.php"
]
},
Once that's done, run composer dump-autoload to regenerate the autoload files. Now the overrides work both during regular app operation, and when running tests!