vu/zf2-test-extensions

简化了在 Zf2 中测试控制器和服务的流程

1.0.1 2014-08-07 20:08 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:40:42 UTC


README

简介

vu/zf2-test-extensions 使得测试我们的 Zf2 控制器和服务更加简单。本项目中的大部分代码专门用于与 VeteransUnited Zf2 项目配合使用,但也对任何人开放。

使用 Composer 安装

将 "vu/zf2-test-extensions" 添加到 composer.json 文件的 require 部分,然后运行相应的安装或更新操作。有关 Composer 的更多信息,请访问 他们的网站

控制器

vu/zf2-test-extensions 中的抽象控制器有助于确保控制器能够全面且易于测试。这是通过在测试创建时模拟 ServiceManager、Request 和 Response 对象来实现的。

### 动作控制器 应该使用 AbstractActionController 代替 Zf2 的 ActionController。这个控制器将扩展 Zf2 的 ActionController,并增加易于测试的优点。

namespace NewNamespace;

use Vu\Zf2TestExtensions\Controller\AbstractActionController;

class NewActionController extends AbstractActionController{
    //typical controller code goes here
}

### RESTful 控制器 应该使用 AbstractRestfulController 代替 Zf2 的 AbstractRestfulController。这个控制器将扩展 Zf2 的 AbstractRestfulController,并增加易于测试的优点。

namespace NewNamespace;

use Vu\Zf2TestExtensions\Controller\AbstractRestfulController;

class NewRestfulController extends AbstractRestfulController{
    //typical restful controller code goes here
}

测试控制器

在测试控制器时,您将想要在测试类中扩展相应的测试控制器。这样您就可以充分利用库。扩展的类将设置您要测试的类并注入 ServiceLocator 的实例。请注意,所有测试类都必须实现 getControllerName() 方法。

### 动作控制器 在测试扩展 AbstractActionController 的控制器时,您希望测试类扩展 AbstractActionControllerTest。示例

namespace NewNamespace;

use Vu\Zf2TestExtensions\Test\Controller\AbstractActionControllerTest;

class NewActionControllerTest extends AbstractActionControllerTest{

    public function getControllerName(){
        return "NewNamespace\NewActionController";
    }

    //Other test code goes here
}

### RESTful 控制器 在测试扩展 AbstractRestfulController 的控制器时,您希望测试类扩展 AbstractRestfulControllerTest。示例

namespace NewNamespace;

use Vu\Zf2TestExtensions\Test\Controller\AbstractRestfulControllerTest;

class NewRestfulControllerTest extends AbstractRestfulControllerTest{

    public function getControllerName(){
        return "NewNamespace\NewRestfulController";
    }

    //Other test code goes here
}

服务

### 服务定位器 提供的 AbstractServiceLocatorAwareService 有助于自动将 Zf2 的 ServiceLocator 注入到您的类中。将通过 ServiceLocator 加载的类应扩展此类。

namespace NewNamespace;

use Vu\Zf2TestExtensions\Service\AbstractServiceLocatorAwareService;

class NewBusinessLogicClass extends AbstractServiceLocatorAwareService{
    //your code...
}

测试服务

### 服务定位器 AbstractServiceLocatorAwareServiceTest 为要测试的服务设置并为您注入 ServiceLocator 的实例。这应用于测试所有扩展 AbstractServiceLocatorAwareService 的类。请注意,所有使用 AbstractServiceLocatorAwareServiceTest 的测试类都必须实现 getClassName() 方法

namespace NewNamespace;

use Vu\Zf2TestExtensions\Test\Service\AbstractServiceLocatorAwareServiceTest;

class NewBusinessLogicClassTest extends AbstractServiceLocatorAwareServiceTest{

    public function getClassName(){
        return 'NewNamespace\NewBusinessLogicClass';
    }

    //Other test code goes here
}