Full URLs in emails in CakePHP unittest

北城余情 提交于 2019-12-02 05:52:31

There is no host when running an app in the CLI environment as it's not a web request. You'll have to configure the base URL on your own.

Quote from the docs:

App.fullBaseUrl

The fully qualified domain name (including protocol) to your application’s root. This is used when generating absolute URLs. By default this value is generated using the $_SERVER environment. However, you should define it manually to optimize performance or if you are concerned about people manipulating the Host header. In a CLI context (from shells) the fullBaseUrl cannot be read from $_SERVER, as there is no webserver involved. You do need to specify it yourself if you do need to generate URLs from a shell (e.g. when sending emails).

So you can either configure it via App.fullBaseUrl

Configure::write('App.fullBaseUrl', 'http://localhost');

or a little more specific so that it only applies to the router, via Router::fullBaseUrl()

Router::fullBaseUrl('http://localhost');

You can either configure it in your application configuration (config/app.php), so that even on regular web requests your app isn't building it dynamically anmore, and consequently have it available in the test environment too, or, if you just want to apply to the test suite, put it either in your tests bootstrap (tests/bootstrap.php) to have it apply globally, or set it in your individual test case files.

That's what the CakePHP core test suite is doing it too btw. Whenever you're unsure, having a look at the core tests might give you a hint.

See also

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!