PHP

Tutorials and guides on PHP

PHPUnit change http host with Symfony

Written by: Peter Fisher on April 7, 2016
Tagged under: , , ,
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

fe13d95fd6d75d2ba275b55bcbfe5dd2_400x400A quick post to remind me how a domain can be changed when using PHPUnit and Symfony routes
By default tests will run against localhost and certain projects like Sylius may require a different domain for different channels so localhost will not do.
To change the domain in a test do this:

$client = static::createClient();
$client->setServerParameter('HTTP_HOST', 'channel-mobile.local');
$crawler = $client->request('GET', '/do-somthing');

The request will now be http://channel-mobile.local/do-somthing

Don’t include http:// or https:// as the route will fail!

I hit this issue when running a functional test against a Sylius channel. Without setting the HTTP_HOST the request was http://localhost/do-somthing

The error I got was:

Sylius\\Component\\Channel\\Context\\ChannelNotFoundException(code: 0): Channel could not be found! at /vendor/sylius/sylius/src/Sylius/Component/Channel/Context/CompositeChannelContext.php:53)"

Debugging the error showed that the channel was being set by the requested domain and the requested domain was localhost.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Peter Fisher is a web developer working in Gloucester UK. Founder of the digital agency Websomatic, author of this blog and the HowToCodeWell Youtube channel. Peter has over ten years of web development experience under his belt

Read all about Peter Fisher

Leave a Reply

Your email address will not be published. Required fields are marked *