yalesov/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 19:20:07 UTC


README

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

安装

Composer:

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

PHPUnit不包含在此包中。您可以通过PEAR安装它,并使用CLI中的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支持。您的EntityManager在ServiceManager中以doctrine.entitymanager.orm_default别名存在。

(可选) 您已在配置文件中将目录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'
  }
}