使用 Zend_Test 的相对 uri 的问题

发布于 2024-10-05 00:05:43 字数 1102 浏览 3 评论 0原文

我第一次在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-10-12 00:05:43

环境变量“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文