CakePHP unittest中电子邮件中的完整URL [英] Full URLs in emails in CakePHP unittest

查看:135
本文介绍了CakePHP unittest中电子邮件中的完整URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的CakePHP 3.2中的测试触发的电子邮件中获取完整的URL.我尝试使用$this->Html->image('image.jpg', ['fullBase' => true])$this->Url->build('/', true)的完整选项,但是它们似乎在测试中不起作用.

I would like to get full URLs inside emails that get triggered by my tests in CakePHP 3.2. I tried with the full-options for $this->Html->image('image.jpg', ['fullBase' => true]) and $this->Url->build('/', true) but they don't seem to work in tests.

在测试过程中如何在电子邮件中强制使用完整URL?

How can I force full URLs in emails while testing?

推荐答案

在CLI环境中运行应用程序时没有主机,因为它不是Web请求.您必须自己配置基本URL.

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.

从文档中引用:

App.fullBaseUrl

全限定域名(包括协议) 到您应用程序的根目录.在生成绝对值时使用 网址.默认情况下,此值是使用$_SERVER生成的 环境.但是,您应该手动定义它以进行优化 性能或您是否担心有人操纵Host 标头.在CLI上下文中(从外壳),无法读取fullBaseUrl 来自$_SERVER,因为不涉及任何Web服务器.你确实需要 如果您确实需要从shell生成网址(例如, 发送电子邮件时).

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

因此您可以通过App.fullBaseUrl

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

或更具体一点,使其仅通过Router::fullBaseUrl()

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

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

您可以在应用程序配置(config/app.php)中对其进行配置,以便即使在常规Web请求上,您的应用程序也无法动态构建,因此也可以在测试环境中使用它,或者,如果您只是想应用到测试套件,将其放在测试引导程序(tests/bootstrap.php)中以使其全局应用,或者将其设置在您的单个测试用例文件中.

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.

这就是CakePHP核心测试套件正在做的事情.不确定时,看看核心测试可能会给您提示.

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.

另请参见

  • Cookbook > Configuration > General Configuration
  • API > \Cake\Routing\Router::fullBaseUrl()
  • https://github.com/cakephp/cakephp/blob/3.2.13/tests/bootstrap.php#L70
  • https://github.com/cakephp/.../blob/3.2.13/tests/TestCase/Routing/RouterTest.php#L74
  • https://github.com/cakephp/.../3.2.13/tests/TestCase/Routing/RouterTest.php#L520-L529

这篇关于CakePHP unittest中电子邮件中的完整URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆