本文介绍了详解Yaf框架PHPUnit集成测试方法,分享给大家,具体如下:
测试目录
?
1
2
3
4
5
6
7
8
9
10
|
test
├── TestCase.php
├── bootstrap.php
├── controller
│ ├── BaseControllerTest.php
│ └── IndexControllerTest.php
├── model
├── phpunit.xml
└── service
└── TokenServiceTest.php
|
phpunit.xml
?
1
2
3
4
5
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< phpunit xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "https://schema.phpunit.de/6.2/phpunit.xsd"
extensionsDirectory = "dbunit.phar" bootstrap = "./bootstrap.php" >
</ phpunit >
|
bootstrap.php 测试框架入口文件
?
1
2
3
|
define( "APP_PATH" , realpath (dirname( __FILE__ ) . '/../' ));
date_default_timezone_set( "Asia/Shanghai" );
define( "TEST_DIR" , __DIR__);
|
TestCase.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
|
namespace test;
use PHPUnit\\Framework\\TestCase as Test;
use Yaf\\Application;
class TestCase extends Test
{
protected static $_application = null;
protected function setUp()
{
self:: $_application = $this ->getApplication();
parent::setUp();
}
public function testAppPath()
{
$this ->assertEquals( '/Users/xiong/Sites/kyYaf' , APP_PATH);
}
public function testApp()
{
$this ->assertEquals(Application::app(), self:: $_application );
}
public function testApplication()
{
$this ->assertNotNull(self:: $_application );
}
public function getApplication()
{
if (self:: $_application == null) {
$this ->setApplication();
}
return self:: $_application ;
}
public function setApplication()
{
$application = new Application(APP_PATH . '/conf/application.ini' );
$application ->bootstrap();
self:: $_application = $application ;
}
}
|
TokenServiceTest.php service类例子
?
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
|
namespace Service;
use test\\TestCase;
include TEST_DIR . '/TestCase.php' ;
include APP_PATH . '/application/library/Service/BaseService.php' ;
include APP_PATH . '/application/library/Service/TokenService.php' ;
class TokenServiceTest extends TestCase
{
/**
* @var TokenService
*/
protected static $tokenService ;
public function setUp()
{
self:: $tokenService = TokenService::getInstance();
parent::setUp();
}
public function testCreateToken()
{
$token = self:: $tokenService ->createToken( '22' );
$this ->assertInternalType( 'array' , $token );
$this ->assertInternalType( 'string' , $token [ 'token' ]);
}
}
|
BaseControllerTest.php controller类例子
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
namespace test\\controller;
include TEST_DIR . '/TestCase.php' ;
use test\\TestCase;
class BaseControllerTest extends TestCase
{
public function testGetConfigAction()
{
$request = new Simple( 'CLI' , '' , 'Index' , 'getConfig' );
$response = self:: $_application ->getDispatcher()->returnResponse(true)->dispatch( $request );
$contents = $response ->getBody();
$data = json_decode( $contents , true);
$this ->assertInternalType( 'array' , $data );
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/xiaoxiong_web/article/details/78900463
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 94
-
2025-05-29 79
-
2025-05-25 20
-
2025-05-25 85
-
Softupnotify.exe是什么?Softupnotify.exe可以删除吗?
2025-05-27 63
热门评论