【问题标题】:Testing controller with Zend_Test使用 Zend_Test 测试控制器
【发布时间】:2011-03-06 16:47:54
【问题描述】:

我在另一个forum 中问过同样的问题,但我还没有运气。所以请允许我在这里问同样的问题。

我想设置 Zend_Test 来测试我的控制器代码(我正在使用 PHP,Zend 框架)。根据official documentation,一切似乎都是正确的,但我一直收到错误消息。

有关问题的详细说明和我的设置,请参阅此处的forum post。任何人都可以给我一个线索,我的设置有什么问题?

谢谢!问候,安德烈。

【问题讨论】:

    标签: php zend-framework phpunit zend-test


    【解决方案1】:

    我之前在使用 setFallbackAutoloader(true) 时遇到过这个问题,但我一直无法找到根本原因。如果您使用 Google 搜索该错误,您会发现一些 ZF 错误报告提及它。

    您能否确认您的应用程序中也使用了 setFallbackAutoloader(true)?如果没有,那么您可以从您的 TestHelper.php 中删除该行。如果是,请尝试添加:

    $autoLoader->suppressNotFoundWarnings(true);
    

    就在它之后,这通常会为我解决问题(但可能会导致一些其他问题)。

    【讨论】:

    • 是的,我没有在我的应用程序中使用后备自动加载器,但我在 TestHelper.php 中设置了一个。在我手动删除该行并注册命名空间后,测试运行良好。谢谢蒂姆!
    【解决方案2】:

    查看本教程,然后逐步进行操作。适合我。

    Tutorial PHPUNIT + Zend Framework

    【讨论】:

      【解决方案3】:

      我在几次会议上就这个主题发表过演讲,Zend 网站上什至还有一个网络研讨会(请参阅下面的链接)。

      当我查看您的设置脚本时,您会发现其中有很多混乱,一旦您向应用程序添加更多功能,就很难维护。

      我的 TestHelper 类只包含以下内容:

      <?php
      /*
       * Example test helper script taken from the blog article of Matthew Weier
       * O'Phinney on September 11, 2008
       * 
       * {@link http://weierophinney.net/matthew/archives/190-Setting-up-your-Zend_Test-test-suites.html}
       */
      
      /*
       * Start output buffering
       */
      //ob_start();
      
      /*
       * Set error reporting to the level to which code must comply.
       */
      error_reporting( E_ALL | E_STRICT );
      
      /*
       * Set default timezone
       */
      date_default_timezone_set('Europe/Brussels');
      
      /*
       * Testing environment
       */
      if (!defined('APPLICATION_ENV'))
          define('APPLICATION_ENV', 'testing');
      
      /*
       * Determine the root, library, tests, and models directories
       */
      $root        = realpath(dirname(__FILE__) . '/../');
      $library     = $root . '/library';
      $tests       = $root . '/tests';
      $models      = $root . '/application/models';
      $controllers = $root . '/application/controllers';
      
      /*
       * Set up application and test path constant for easy access helper classes
       */
      if (!defined('APPLICATION_PATH'))
          define('APPLICATION_PATH', $root . '/application');
      define('TEST_PATH', $tests);
      
      /*
       * Prepend the library/, tests/, and models/ directories to the
       * include_path. This allows the tests to run out of the box.
       */
      $localFrameworkPaths = array (
          '/usr/local/zend/share/ZendFramework/library',
          '/opt/ZendFramework',
      );
      $include_path = get_include_path();
      foreach ($localFrameworkPaths as $zfPath) {
          $include_path = str_replace($zfPath . PATH_SEPARATOR, '', $include_path);
      }
      $path = array(
          APPLICATION_PATH,
          $models,
          $library,
          $tests,
          $include_path,
      );
      set_include_path(implode(PATH_SEPARATOR, $path));
      
      /**
       * Register autoloader
       */
      require_once 'Zend/Loader.php';
      Zend_Loader::registerAutoload();
      
      /**
       * Store application root in registry
       */
      Zend_Registry::set('testRoot', $root);
      Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');
      
      /**
       * Say to the sessions we use unit testing here
       */
      Zend_Session::$_unitTestEnabled = true;
      
      /*
       * Unset global variables that are no longer needed.
       */
      unset($root, $library, $models, $controllers, $tests, $path);
      

      这类似于他的博客上提供的原始设置 MWOP(请参阅课程顶部的链接)。

      就是这样,无需担心手动添加模块或跟踪应用程序架构中的更改,因为这将使用应用程序自己的引导程序。

      如需更多信息,请查看以下链接:

      让我知道它是否也适用于您的案例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 2015-03-22
        • 1970-01-01
        相关资源
        最近更新 更多