php - [verb]Json() methods in tests are not obeying the config('app.url') after upgrading to 7.x

I have upgraded the laravel version to 7.17.2 from 6.16.0 a few minutes ago following the upgrade guide.
But my tests are now failing with the following exception:Symfony\Component\HttpKernel\Exception\NotFoundHttpException : POST http://localhost/v1/orders
TheAPP_URL
is set tohttp://localhost
in my.env
file. But in my base TestCase class, I am overriding the config('app.url') with a custom endpoint. Before the upgrade, the tests were picking the custom endpoint.
Since the last upgrade, apparently the getJson(), postJson(), etc helper methods are not applying the URL fromconfig('app.url')
but directly from the.env
file. Because when I hardcode the URL in the.env
file, I no longer get the NotFoundHttpException.
How do I fix this?
Answer
Solution:
You could do the following.
In your phpunit.xml:
<php>
<server name="APP_URL" value="http://example.com"/>
</php>
Or in your TestCase:
use Illuminate\Support\Facades\URL;
protected function setUp(): void
{
parent::setUp();
URL::forceRootUrl('http://example.com');
}
Both of these will result inapp('url')->to('my-endpoint')
printinghttp://example.com/my-endpoint
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.