当前位置:首页 > CMS教程 > 其它CMS > 列表

详解Yaf框架PHPUnit集成测试方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-25 10:43:29 浏览: 评论:0 

这篇文章主要介绍了详解Yaf框架PHPUnit集成测试方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。

本文介绍了详解Yaf框架PHPUnit集成测试方法,分享给大家,具体如下:

测试目录

  1. test 
  2. ├── TestCase.php 
  3. ├── bootstrap.php 
  4. ├── controller 
  5. │  ├── BaseControllerTest.php 
  6. │  └── IndexControllerTest.php 
  7. ├── model 
  8. ├── phpunit.xml 
  9. └── service 
  10.   └── TokenServiceTest.php 

phpunit.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.      xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.2/phpunit.xsd" 
  4.      extensionsDirectory="dbunit.phar" bootstrap="./bootstrap.php"> 
  5. </phpunit> 

bootstrap.php 测试框架入口文件

  1. define("APP_PATH", realpath(dirname(__FILE__) . '/../')); 
  2. date_default_timezone_set("Asia/Shanghai"); 
  3. define("TEST_DIR", __DIR__); 

TestCase.php 测试文件基础类

  1. namespace test; 
  2. use PHPUnit\Framework\TestCase as Test; 
  3. use Yaf\Application; 
  4. class TestCase extends Test 
  5. { 
  6.   protected static $_application = null; 
  7.   protected function setUp() 
  8.   { 
  9.     self::$_application = $this->getApplication(); 
  10.     parent::setUp(); 
  11.   } 
  12.  
  13.   public function testAppPath() 
  14.   { 
  15.     $this->assertEquals('/Users/xiong/Sites/kyYaf', APP_PATH); 
  16.   } 
  17.  
  18.   public function testApp() 
  19.   { 
  20.     $this->assertEquals(Application::app(), self::$_application); 
  21.   } 
  22.  
  23.   public function testApplication() 
  24.   { 
  25.     $this->assertNotNull(self::$_application); 
  26.   } 
  27.  
  28.   public function getApplication() 
  29.   { 
  30.     if (self::$_application == null) { 
  31.       $this->setApplication(); 
  32.     } 
  33.     return self::$_application; 
  34.   } 
  35.  
  36.   public function setApplication() 
  37.   { 
  38.     $application = new Application(APP_PATH . '/conf/application.ini'); 
  39.     $application->bootstrap(); 
  40.     self::$_application = $application; 
  41.   } 
  42. } 

TokenServiceTest.php service类例子

  1. namespace Service; 
  2. use test\TestCase; 
  3. include TEST_DIR . '/TestCase.php'; 
  4. include APP_PATH . '/application/library/Service/BaseService.php'; 
  5. include APP_PATH . '/application/library/Service/TokenService.php'; 
  6. class TokenServiceTest extends TestCase 
  7. { 
  8.   /** 
  9.    * @var TokenService 
  10.    */ 
  11.   protected static $tokenService; 
  12.   public function setUp() 
  13.   { 
  14.     self::$tokenService = TokenService::getInstance(); 
  15.     parent::setUp(); 
  16.   } 
  17.  
  18.   public function testCreateToken() 
  19.   { 
  20.     $token = self::$tokenService->createToken('22'); 
  21.     $this->assertInternalType('array', $token); 
  22.     $this->assertInternalType('string', $token['token']); 
  23.   } 
  24.  
  25. } 

BaseControllerTest.php controller类例子

  1. namespace test\controller; 
  2. include TEST_DIR .'/TestCase.php'; 
  3. use test\TestCase; 
  4. class BaseControllerTest extends TestCase 
  5. { 
  6.   public function testGetConfigAction() 
  7.   { 
  8.     $request = new Simple('CLI', '', 'Index', 'getConfig'); 
  9.     $response = self::$_application->getDispatcher()->returnResponse(true)->dispatch($request); 
  10.     $contents = $response->getBody(); 
  11.     $data = json_decode($contents, true); 
  12.     $this->assertInternalType('array', $data); 
  13.   } 
  14. }

Tags: Yaf框架 PHPUnit

分享到: