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. 到此结束。

使用phpunit测试yaf项目操作步骤的更多相关文章

  1. monkey测试 -- 原理和操作步骤

     Monkey测试原理: Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序 ...

  2. Git:上传GitHub项目操作步骤

    git教程:git详解.gitbook #首次上传步骤 首先在工程文件位置处右键git bash here 本地创建ssh key $ ssh-keygen -t rsa -C "your_ ...

  3. 将项目Demo上传到Github上的操作步骤

    之前我有很多代码直接上传到了CSDN上,主要是因为操作方便,今天我就说说将源码Demo上传到Github上的操作步骤. 首先,你要先确定自己在Github上有自己的账户名,账户邮箱和密码.如果没有可以 ...

  4. 版本控制之五:SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤(转)

    使用场景: 假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时 ...

  5. SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤

    使用场景: 假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时 ...

  6. monkey测试入门2--测试步骤、常用参数、常规monkey命令

    <凤栖梧> 柳永 伫倚危楼风细细,望极春愁,黯然生天际.草色烟光残照里,无言谁会凭栏意? 拟把疏狂图一醉,对酒当歌,强乐还无味,衣带渐宽终不悔,为伊消得人憔悴. 简要步骤:adb devi ...

  7. Springboot集成Swagger操作步骤

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. vue-cli项目创建步骤

    vue-cli项目创建步骤: 1.cmd打开命令行窗口 2.输入cnpm install vue-cli -g,然后回车等待(想在哪个目录建立vue项目就要在进入到对应目录再输入命令) 3.安装结束后 ...

  9. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

随机推荐

  1. vim安装

    vim的下载与安装 在线安装 sudo yum install vim 源码编译安装 1)   下载源码 git clone https://github.com/vim/vim.git 2)   编 ...

  2. vue跳坑笔记

    序号 报错截图 关键词 解决办法 1 - code EPERM errno 4048 syscall scandir operation not permitted 清除npm缓存,命令: npm c ...

  3. L358 World Book Day

    World Book Day is celebrated by UNESCO and other related organisations every year on the 23rd of Apr ...

  4. 解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题

    安装fiddler后,使用Chrome访问https网站时,可能会出现以下错误,本文说明如何解决此类问题: “您的连接不是私密连接”.“NET::ERR_CERT_AUTHORITY_INVALID” ...

  5. Linux命名空间

    Linux Namespaces机制提供一种资源隔离方案.PID,IPC,Network等系统资源不再是全局性的,而是属于特定的Namespace.每个Namespace里面的资源对其他Namespa ...

  6. wpf binging Class 双向绑定 需要实现的接口

    public class HuMan : INotifyPropertyChanged { private string name; private string age; private strin ...

  7. nw 引用 sqlite

    0.好吧,这对于我这个c 小白来说,真的有点难度. 1.安装Python 2.7.14 https://www.python.org/downloads/ 2.安装最新的nodejs+npm http ...

  8. Nginx+Tomcat配置负载均衡-动静分离(二)

    配置动静分离的时候遇到了一些问题,一个是配置nginx配置文件有问题导致访问不到服务器,另一个问题是配置静态资源的路径和实际的资源目录不匹配导致404,502等错误 结合上一篇的基础,在此将动静分离的 ...

  9. Tajima's D

    Three ways to assess the nucleotide diversity (heterozygosity).The first is mean pairwise difference ...

  10. jmeter 写正则表达式

    ():括起来的部分就是要提取的. .:匹配任何字符串. +:一次或多次. ?:不要太贪婪,在找到第一个匹配项后停止.   需要根据要取的数据取值 jt: eyJhbGciOiJSUzI1NiJ9.ey ...