首页 > 代码库 > Phalcon中使用UnitTest

Phalcon中使用UnitTest

Phalcon框架本身并未集成UnitTest

而原生的PHPUnitTest有些复杂,为了使开发更容易开故移植CI的UnitTest组件,以下是添加此组件的方法。

1.复制如下文本到components(若不存在则自己添加)文件夹下

<?php
namespace components;

/**
 * CodeIgniter
 * UnitTest.php
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package CodeIgniter
 * @author ExpressionEngine Dev Team
 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
 * @license http://codeigniter.com/user_guide/license.html
 * @link http://codeigniter.com
 * @since Version 1.3.1
 * @filesource
 *
 */

// ------------------------------------------------------------------------

/**
 * Unit Testing Class
 *
 * Simple testing class
 *
 * @package CodeIgniter
 * @subpackage Libraries
 * @category UnitTesting
 * @author ExpressionEngine Dev Team
 * @link http://codeigniter.com/user_guide/libraries/uri.html
 */
class UnitTest implements \Phalcon\DI\InjectionAwareInterface
{

    var $active = TRUE;

    var $results = array();

    var $strict = FALSE;

    var $_template = NULL;

    var $_template_rows = NULL;

    var $_test_items_visible = array();

    protected $_arrLangUnitTest = NULL;

    protected $_di;

    public function __construct()
    {
        // These are the default items visible when a test is run.
        $this->_test_items_visible = array(
            'test_name',
            'test_datatype',
            'res_datatype',
            'result',
            'file',
            'line',
            'notes'
        );
        
        $this->loadLanguage();
        // log_message('debug', "Unit Testing Class Initialized");
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Run the tests
     *
     * Runs the supplied tests
     *
     * @access public
     * @param
     *            array
     * @return void
     */
    function set_test_items( $items = array() )
    {
        if( ! empty($items) and is_array($items) ) {
            $this->_test_items_visible = $items;
        }
    }

    function loadLanguage()
    {
        if( ! $this->_arrLangUnitTest ) {
            $this->_arrLangUnitTest = include APP_PATH . 'language/unit_test_lang.php';
        }
        
        return $this->_arrLangUnitTest;
    }

    function getLine( $line = '' )
    {
        $value = http://www.mamicode.com/($line == '' or ! isset($this->_arrLangUnitTest[$line])) ? FALSE : $this->_arrLangUnitTest[$line];>
添加如下文件到app/language文件夹下:

<?php

$langUnitTest['ut_test_name']		= 'Test Name';
$langUnitTest['ut_test_datatype']	= 'Test Datatype';
$langUnitTest['ut_res_datatype']	= 'Expected Datatype';
$langUnitTest['ut_result']			= 'Result';
$langUnitTest['ut_undefined']		= 'Undefined Test Name';
$langUnitTest['ut_file']			= 'File Name';
$langUnitTest['ut_line']			= 'Line Number';
$langUnitTest['ut_passed']			= 'Passed';
$langUnitTest['ut_failed']			= 'Failed';
$langUnitTest['ut_boolean']			= 'Boolean';
$langUnitTest['ut_integer']			= 'Integer';
$langUnitTest['ut_float']			= 'Float';
$langUnitTest['ut_double']			= 'Float'; // can be the same as float
$langUnitTest['ut_string']			= 'String';
$langUnitTest['ut_array']			= 'Array';
$langUnitTest['ut_object']			= 'Object';
$langUnitTest['ut_resource']		= 'Resource';
$langUnitTest['ut_null']			= 'Null';
$langUnitTest['ut_notes']			= 'Notes';

return $langUnitTest;
/* End of file unit_test_lang.php */
/* Location: ./system/language/english/unit_test_lang.php */

3.注册命名空间conponents => APP_PATH . ‘components/‘;


4.测试一下是否正确


<?php
//UTestController.php
namespace controllers;

class UTestController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {
        echo $this->utest->run('abc', 'abc');
        
        echo $this->utest->run('1+1', '2');
    }
}

?>




OK成功
 这个样我们在Phalcon即可很简易的使用UnitTest了

Phalcon中使用UnitTest