heartsentwined/zf2-phpunit-testcase

为 ZF2 应用程序提供的 PHPUnit 测试用例基类。

v2.1.3 2016-07-06 12:27 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:41:34 UTC


README

PHPUnit 为 ZF2 应用程序提供的测试用例基类。

安装

Composer:

{
  "require": {
    "yalesov/zf2-phpunit-testcase": "2.*"
  }
}

PHPUnit 本包不包含。您可以通过 PEAR 安装它,并在 phpunit 命令行界面使用。

$ pear config-set auto_discover 1
$ pear install pear.phpunit.de/PHPUnit

用法

首先,您需要一个引导文件,它返回 Zend\Mvc\Application。以下是一个示例

use Zend\Mvc\Application;
chdir(__DIR__); // chdir() to application root
require 'vendor/autoload.php';
/*
 * why another set of config?
 * now that you're writing unit test
 * odds are that you would want to override some production settings here,
 * e.g. load only certain module; use a test database connection, etc
 */
$config = array(
  'modules'   => array(
    'Foo',
    'Bar',
  ),
  'module_listener_options' => array(
    'config_glob_paths' => array(
      __DIR__ . '/test/config/{,*.}php'
    ),
    'module_paths' => array(
      'Foo' => __DIR__,
      'vendor',
    ),
  ),
));
return Application::init($config);

接下来是测试用例基类

Zf

使用引导文件 foo/bootstrap.php 引导您的 Zf2 应用程序

use Yalesov\Phpunit\Testcase\Zf as ZfTestcase;

class FooTest extends ZfTestcase
{
  public function setUp()
  {
    $this->setBootstrap('foo/bootstrap.php'); // watch out for relative dirs! - use __DIR__ if needed
    parent::setUp();
  }

  public function tearDown()
  {
    // your tearDown() operations
    parent::tearDown();
  }

  public function testFoo()
  {
    // $this->application instance of Zend\Mvc\Application
    // $this->sm instance of Zend\ServiceManager\ServiceManager
  }
}

Doctrine

使用引导文件 foo/bootstrap.php 引导您的 Zf2 应用程序,并启用 Doctrine ORM 支持。您的 EntityManagerdoctrine.entitymanager.orm_default 被ServiceManager别名。

(可选) 您在配置文件中将目录 foo/tmp 声明为临时目录,可能用于存储代理,您希望在每次测试之前创建此目录,并在测试之后删除它。(即在 setUp() 和 teardown() 期间)

use Yalesov\Phpunit\Testcase\Doctrine as DoctrineTestcase;

class FooTest extends DoctrineTestcase
{
  public function setUp()
  {
    // fluent interface available
    $this
      ->setBootstrap('foo/bootstrap.php')
      ->setEmAlias('doctrine.entitymanager.orm_default')
      ->setTmpDir('foo/tmp'); // optional: see use case above
    parent::setUp();
  }

  public function tearDown()
  {
    // your tearDown() operations
    parent::tearDown();
  }

  public function testFoo()
  {
    // $this->application instance of Zend\Mvc\Application
    // $this->sm instance of Zend\ServiceManager\ServiceManager
    // $this->em instance of Doctrine\ORM\EntityManager
    // $this->tmpDir = 'foo/tmp'
  }
}