0

I would like to test a new function in a helper file.

function isProductionSite() {
    return !(bool) preg_match('/admin|development/', request()->getHttpHost());
}

When I call this function in my test function I get my local address. Therefore I tried to mock the request()->getHttpHost.

public function testIsProductionSiteExpectFalse(): void
{
    $request = Mockery::mock(Request::class);
    $request->shouldReceive('getHttpHost')->once()->andReturn('admin.mydomain.de');
    $this->assertFalse(isProductionSite());
}

In the Tets Function $request->getHttpHost() would now output admin.mydomain.de. But getHttpHost() from the function to be tested (isProductionSite()) gives me the correct host.

How can I set the HttpHost in the testcase so that the helper function outputs the manipulated host?

1 Answer 1

1

You're mocking an instance of $request but the instance being used by the function under test is not the mock, but presumably a global variable.

Dependencies should always be passed to the helper function. Just add $request as a parameter of isProductionSite.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer and the good advice!
glad the answer was useful. Remember to mark the answer as accepted if you think it resolved the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.