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