【发布时间】:2013-12-24 10:51:51
【问题描述】:
这是一个关于在 Zend Framework 2 中正确编码 PHPUnit 测试的问题,使用面向对象的代码原则。
我有以下 PHPUnit 测试用例:
namespace FedcoUserTest;
use FedcoUser\Controller\MachinistController;
class MachinistControllerTest extends Framework\TestCase
{...}
我的 MachinistController 类是这样的:
namespace FedcoUser\Controller;
class MachinistController extends \ZfcUser\Controller\UserController
{...}
在尝试运行测试用例时,我会收到以下错误:
Debug Error: MachinistController.php - Class 'ZfcUser\Controller\UserController' not found
这很奇怪,因为我的 MachinistController 类运行得很好,并且在作为 Web 应用程序运行时找到了 ZfcUser 的控制器,而不是通过 PHPUnit 测试。
不知何故,我决定放一个
require_once 'vendor/zf-commons/zfc-user/src/ZfcUser/Controller/UserController.php';
插入我的 MachinistControllerTest 类,现在我的 PHPUnit 测试运行没有错误。
我觉得这很奇怪。
为什么:到目前为止,在我使用 ZF2 的经验中,我还没有需要使用 require 函数。 ZF2 是完全 OO 的,那为什么是现在呢?这可能是正确的,但有没有更好的方法,如果有,是什么?
更具体的问题:
- (正确性)我是否可以在代码中使用此 require_once 行来使测试用例通过?
- (ZF2 最佳实践)为什么我要在我原本完全 OO ZF2 代码中使用
require_once()语句(在我看来,使用require就像是程序代码的味道)? - (OO 最佳实践)为什么在我的 MachinistController 完美扩展 ZfcUser 的控制器时找不到它?
【问题讨论】:
标签: php zend-framework zend-framework2 phpunit