参考官方文档,稍作修改。

在项目下创建目录unittests,进入目录执行:

1
composer require phpunit/phpunit

创建tests目录并在其中创建文件Bootstrap.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
use Phalcon\DI,
Phalcon\DI\FactoryDefault;

ini_set('display_errors',1);
error_reporting(E_ALL);

define('ROOT_PATH', __DIR__);
define('PROJECT_DIR', '/home/taoqi/workspace');

set_include_path(
ROOT_PATH . PATH_SEPARATOR . get_include_path()
);

// required for phalcon/incubator
include __DIR__ . "/../vendor/autoload.php";

// 加载项目文件
$config = require_once PROJECT_DIR.'/web/config/config.php';
require_once PROJECT_DIR.'/web/config/loader.php';
$loader->registerDirs(array(
ROOT_PATH
), true);

// $di = new FactoryDefault();
DI::reset();

// add any needed services to the DI here
require_once PROJECT_DIR.'/web/config/services.php';

DI::setDefault($di);

安装phalcon的phpunit辅助库:

1
composer require phalcon/incubator

创建phpunit.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./Bootstrap.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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
use Phalcon\DI,
\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().');

}

}

}

创建单元测试类testsTestUnitTest.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

namespace Test;

/**
* Class UnitTest
*/
class UnitTests extends \UnitTestCase {

public function testTestCase() {
$post = \Post::find(33);
$this->assertObjectHasAttribute('title', $post, 'where is title ?');
}

}

在tests目录下建立phpunit的软连接并执行测试:

1
2
ln -sf ../vendor/bin/phpunit run
./run

:发现个诡异的问题,如果Model中不覆盖getSource()方法,单元测试中会自动找用下划线分隔的表名,即假如Model名为FooBar,会去找foo_bar的表名,但正常执行程序时找的是foobar。在官方论坛问的问题还木有解决。phalcon坑挺多的。