1 安装PHPUnit等
composer
{
"require": {
"phalcon/incubator": "dev-master",
"phpunit/phpunit": "4.8.*"
}
}
2. 整合PHPUnit到Phalcon
在app、public同级目录下面创建文件夹 tests
app/
public/
tests/
3. 在tests目录下面创建文件
TestHelper.php -- 测试辅助文件
PHPunit.xml -- 配置文件
UnitTestCase.php -- 测试基础类 -- 继承自 PhalconTestCase 继承自 PHPUnit_Framework_TestCase
UnitTest.php -- demo
贴源代码
TestHelper.php
<?php
use Phalcon\DI;
use Phalcon\DI\FactoryDefault;
ini_set('display_errors',1);
error_reporting(E_ALL);
define('ROOT_PATH', __DIR__);
define('PATH_LIBRARY', __DIR__ . '/../app/library/');
define('PATH_SERVICES', __DIR__ . '/../app/services/');
define('PATH_RESOURCES', __DIR__ . '/../app/resources/');
set_include_path(
ROOT_PATH . PATH_SEPARATOR . get_include_path()
);
// Required for phalcon/incubator
include __DIR__ . "/../../vendor/autoload.php";
// Use the application autoloader to autoload the classes
// Autoload the dependencies found in composer
$loader = new \Phalcon\Loader();
$loader->registerDirs(
array(
ROOT_PATH
)
);
$loader->register();
$di = new FactoryDefault();
DI::reset();
// Add any needed services to the DI here
DI::setDefault($di);
PHPunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./TestHelper.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true">
<testsuite name="Phalcon - Testsuite">
<directory>./</directory>
</testsuite>
</phpunit>
UnitTestCase.php
<?php
use Phalcon\DI;
use Phalcon\Test\UnitTestCase as PhalconTestCase;
abstract class UnitTestCase extends PhalconTestCase
{
/**
* @var \Voice\Cache
*/
protected $_cache;
/**
* @var \Phalcon\Config
*/
protected $_config;
/**
* @var bool
*/
private $_loaded = false;
public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL)
{
// Load any additional services that might be required during testing
$di = DI::getDefault();
// Get any DI components here. If you have a config, be sure to pass it to the parent
parent::setUp($di);
$this->_loaded = true;
}
/**
* Check if the test case is setup properly
*
* @throws \PHPUnit_Framework_IncompleteTestError;
*/
public function __destruct()
{
if (!$this->_loaded) {
throw new \PHPUnit_Framework_IncompleteTestError('Please run parent::setUp().');
}
}
}
UnitTest.php
<?php
namespace Test;
/**
* Class UnitTest
*/
class UnitTest extends \UnitTestCase
{
public function testTestCase()
{
$this->assertEquals('works',
'works',
'This is OK'
);
$this->assertEquals('works',
'works1',
'This will fail'
);
}
}
4. 执行PHPUnit
命令行切换到工程下面tests目录:
PHPUnit 3.7.23 by Sebastian Bergmann. Configuration read from /private/var/www/tests/phpunit.xml Time: 3 ms, Memory: 3.25Mb There was 1 failure: 1) Test\UnitTest::testTestCase This will fail Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'works' +'works1' /private/var/www/tests/Test/UnitTest.php:25 FAILURES! Tests: 1, Assertions: 2, Failures: 1.