博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用phpunit测试yaf项目操作步骤

Posted on 2018-07-17 16:13  jong  阅读(652)  评论(0编辑  收藏  举报

yaf + phpunit

使用phpunit对yaf进行测试的核心在于bootstrip文件的配置。
*1. 首先在项目目录下创建tests文件,并在tests中创建phpunit.xml

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    colors="true" 
    syntaxCheck="true"

    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd" bootstrap="./bootstrap.php"
    >

    <testsuites>
        <testsuite name="controller">
            <directory>./application/controllers/*Test.php</directory>
        </testsuite>
        <testsuite name="models">
            <directory>./application/models/*Test.php</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html" target="./report" charset="UTF-8"/>
    </logging>
</phpunit>

*2. 然后就是编写phpunit的bootstrap,新增bootstrap.php文件中,在其中增加即可

<?php
define("APP_PATH", realpath(dirname(__FILE__) . '/../'));

*3. 根据需要创建相应的目录。我创建的目录为
```
|-- application
| |-- BaseCase.php
| |-- controllers
| | -- EmailTest.php | -- models
|-- bootstrap.php
|-- phpunit.xml

```

*4. 在application/controllers/中创建EmailTest.php文件,这就是controller测试文件; 在application/models/ 中创建model文件,进行model数据测试。
*5. BaseCase.php是一个父类,在这个类中做的功能有两部分

    1. yaf项目中bootstrip文件配置的加载,其实就类似于复制一份项目中bootstrip的一些方法,放到此处,比如config配置,路由等;
    2. 普通的一些方法,比如get、post请求。
    ```
    
    ```php
    <?php
    
    use PHPUnit\Framework\TestCase;
    use Illuminate\Database\Capsule\Manager as Capsule;
    
    class BaseCase extends TestCase
    {
        
        public $application;
    
        public function __construct(){
    
            parent::__construct();
    
            $this->_init(); 
        }
        /**
         * init
         */
        public function _init()
        {
            if (!Yaf_Registry::get('config')) {
                
                $this->application = new Yaf_Application(APP_PATH . "/conf/application.ini");
                $config = Yaf_Application::app()->getConfig();
    
                Yaf_Registry::set('config',$config);
                echo "\n当前环境:" . Yaf_Application::app()->getConfig()->env. "\n";
                
                $this->_loader();
    
                $this->_iniDatabase();
    
                // disableView
                Yaf_dispatcher::getInstance()->disableView();
    
                
            } 
        }
        /**
         * loader
         */
        public function _loader()
        {
            Yaf_Loader::import(APP_PATH . "/application/function.php");
    
            Yaf_Loader::import(APP_PATH . "/vendor/autoload.php");
        }
        
        
        /**
         * GET 请求
         */
        public function getRequest($url) 
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 200);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $data = curl_exec($ch); 
            curl_close($ch);
            return $data;
        }
    
        /**
         * POST 请求
         */
        public function postRequest($url, $params)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 200);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
        } 
    
    }
    ```    
*6. controllers和models中的文件可以加载文件,做文件映射,进行正常操作访问请求了。    
   models中的操作
<?php
require_once APP_PATH . "/tests/application/BaseCase.php";

class BlogTest extends BaseCase
{
    public function __construct()
    {
        parent::__construct();

        $this->_model = new BlogModel();
        $this->_audit_model = new AuditModel();
    }
    
    public function test_getAuditLists()
    {
        $data = $this->_audit_model->getAuditLists();
        $this->assertNotEmpty($data);
    }
}
    controllers中的操作,其实就是get、post请求了,没什么不同的。因为controllers测试,只能是post、get请求,所以只能是测试Action方法,里面的非Action结束的方法,是不能进行测试的。只能打日志进行记录,查看执行结果。

*7. 到此结束。