您现在的位置是:网站首页> 编程资料编程资料
详解Yaf框架PHPUnit集成测试方法_php实例_
2023-05-25
268人已围观
简介 详解Yaf框架PHPUnit集成测试方法_php实例_
本文介绍了详解Yaf框架PHPUnit集成测试方法,分享给大家,具体如下:
测试目录
test ├── TestCase.php ├── bootstrap.php ├── controller │ ├── BaseControllerTest.php │ └── IndexControllerTest.php ├── model ├── phpunit.xml └── service └── TokenServiceTest.php
phpunit.xml
bootstrap.php 测试框架入口文件
define("APP_PATH", realpath(dirname(__FILE__) . '/../')); date_default_timezone_set("Asia/Shanghai"); define("TEST_DIR", __DIR__);TestCase.php 测试文件基础类
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类例子
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类例子
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); } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- Laravel中使用Queue的最基本操作教程_php实例_
- php获取ajax的headers方法与内容实例_php技巧_
- PHP实现求连续子数组最大和问题2种解决方法_php技巧_
- PHP基于双向链表与排序操作实现的会员排名功能示例_php技巧_
- thinkphp5 URL和路由的功能详解与实例_php实例_
- php-msf源码详解_php实例_
- 关于 Laravel Redis 多个进程同时取队列问题详解_php实例_
- 源码分析 Laravel 重复执行同一个队列任务的原因_php实例_
- 浅析PHP中的闭包和匿名函数_php实例_
- thinkphp5 加载静态资源路径与常量的方法_php实例_
