主頁 > 知識庫 > 詳解Yaf框架PHPUnit集成測試方法

詳解Yaf框架PHPUnit集成測試方法

熱門標簽:服務器配置 團購網站 銀行業(yè)務 阿里云 Mysql連接數(shù)設置 科大訊飛語音識別系統(tǒng) Linux服務器 電子圍欄

本文介紹了詳解Yaf框架PHPUnit集成測試方法,分享給大家,具體如下:

測試目錄

test
├── TestCase.php
├── bootstrap.php
├── controller
│  ├── BaseControllerTest.php
│  └── IndexControllerTest.php
├── model
├── phpunit.xml
└── service
  └── TokenServiceTest.php

phpunit.xml

?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 測試框架入口文件

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);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP單元測試配置與使用方法詳解
  • PHPUnit + Laravel單元測試常用技能
  • PHP使用phpunit進行單元測試示例
  • php使用yield對性能提升的測試實例分析
  • 高質量PHP代碼的50個實用技巧必備(下)
  • 高質量PHP代碼的50個實用技巧必備(上)
  • 很讓人受教的 提高php代碼質量36計
  • 寫出高質量的PHP程序
  • 淺談如何提高PHP代碼質量之端到端集成測試

標簽:衢州 棗莊 大理 衡水 萍鄉(xiāng) 廣元 江蘇 蚌埠

巨人網絡通訊聲明:本文標題《詳解Yaf框架PHPUnit集成測試方法》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266