使用 Zend_Test 的相对 uri 的问题
我第一次在我的 Zend Framework 应用程序上设置单元测试(这实际上是我第一次进行单元测试)。
我目前遇到的一个问题是我使用视图助手来包含我的标题和链接:
class Zend_View_Helper_HeadIncludes extends Zend_View_Helper_Abstract {
public function headIncludes($type, $folder) {
if($folder == "full" && APPLICATION_ENV == "production") {
$folder = "min";
}
$handler = opendir(getenv("DOCUMENT_ROOT") . "/". $type ."/" . $folder);
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
if($type == "js") {
$this->view->headScript()->appendFile('/js/' . $folder . '/' . $file);
} else if ($type == "css" ) {
$this->view->headLink()->appendStylesheet('/css/' . $folder . '/' . $file);
}
}
}
closedir($handler);
}
}
这包含在每个视图脚本中。当我尝试运行测试时,它失败了,因为 opendir() 尝试查找相对于文档根目录的“/css/full”,这对于测试和应用程序来说似乎不是相同的值。解决这个问题的最佳方法是什么?当 APPLICATION_ENV =“testing”时,我可以添加一个条件来执行不同的操作,但不确定这是否与设置测试应该实现的目标相反。
I'm setting up unit testing for the first time on my Zend Framework app (and it's actually the first time I'm doing unit testing at all).
A problem I'm getting at the moment is that I use a view helper to include my headscripts and links:
class Zend_View_Helper_HeadIncludes extends Zend_View_Helper_Abstract {
public function headIncludes($type, $folder) {
if($folder == "full" && APPLICATION_ENV == "production") {
$folder = "min";
}
$handler = opendir(getenv("DOCUMENT_ROOT") . "/". $type ."/" . $folder);
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
if($type == "js") {
$this->view->headScript()->appendFile('/js/' . $folder . '/' . $file);
} else if ($type == "css" ) {
$this->view->headLink()->appendStylesheet('/css/' . $folder . '/' . $file);
}
}
}
closedir($handler);
}
}
This is included in each view script. When I try and run a test it fails because opendir() tries to find eg "/css/full" relative to the document root, which seems to not be the same value for tests and the application. What's the best way to resolve this? I could add in a conditional to do something different when APPLICATION_ENV = "testing", but am not sure if this would run contrary to what setting up testing is supposed to achieve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
环境变量“DOCUMENT_ROOT”仅由您的 Web 服务器设置。您可能希望使用“APPLICATION_PATH”作为参考,因为它在虚拟主机和命令行使用中更可靠。
The environment variable 'DOCUMENT_ROOT' is only going to be set by your web server. You may want to be using 'APPLICATION_PATH' as a reference instead as it's more reliable across virtual hosts as well as command line usage.