Zend 1.11 UnitTesting控制器测试用例

时间:2013-11-13 20:08:41

标签: zend-framework phpunit

我有兴趣在我的工作流程中添加单元测试,并开始在运行PHP 5.3.4和PHPUnit 3.7.13的Zend Framework 1.11应用程序上创建一些测试

我编写的其中一个测试想要在加载应用程序时检查响应中是否出现<h1>。仅在渲染视图脚本时测试通过,但是当我将布局路径添加到主应用程序中的application.ini时,测试失败,因为视图脚本未在响应中返回(通过查看应用程序时没有错误)浏览器,布局和视图正确呈现。)

当杀死测试并输出assertQueryContentContains()用于检查DOM元素的内容时,它显示正在加载layout.phtml但被视为平面文件,将PHP代码显示为字符串而不是解析。

有没有办法让单元测试加载整个MVC调度循环并以与在浏览器中运行时相同的方式返回请求的内容?

提前致谢!

应用/ CONFIGS /的application.ini

includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

测试/ bootstrap.php中

/// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    'C:\wamp\bin\library',
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

测试/应用/控制器/ IndexControllerTest.php

public function setUp()
{

    $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
    parent::setUp();
}

public function testIndexAction()
{

    $params = array('action' => 'index', 'controller' => 'index', 'module' => 'default');
    $urlParams = $this->urlizeOptions($params);
    $url = $this->url($urlParams);
    $this->dispatch($url);

    // assertions
    $this->assertModule($urlParams['module']);
    $this->assertController($urlParams['controller']);
    $this->assertAction($urlParams['action']);


    $this->assertQueryContentContains("div#jt h1", "Unit Testing");

}

更新#1 只是为了在打印$ this-&gt; getResponse() - &gt; getBody();时更清楚一点。屏幕我看到了:

/**
* HTML
*/
<div class="container">
    <?= $this->partial( 'partials/header.phtml' ); ?>

    <?= $this->layout()->content; ?>

    <div class="footer">
         <p>&copy; Company 2013</p>
    </div>

</div>
/**
* More HTML
*/

当我期待layout() - &gt;内容时; ?&GT;显示我正在测试的视图脚本中的内容。

1 个答案:

答案 0 :(得分:0)

你没有回应你的变量吗?

<?= echo $this->partial('partials/header.phtml') ?>
<?= $this->layout()->content; ?>

另外,值得检查您是否可以使用短代码PHP而不是<?php ?>