首頁  >  文章  >  後端開發  >  實例詳解Yaf框架PHPUnit整合測試方法

實例詳解Yaf框架PHPUnit整合測試方法

小云云
小云云原創
2017-12-27 16:23:131672瀏覽

本文主要介紹了詳解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__) . &#39;/../&#39;));
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(&#39;/Users/xiong/Sites/kyYaf&#39;, 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 . &#39;/conf/application.ini&#39;);
    $application->bootstrap();
    self::$_application = $application;
  }
}

TokenServiceTest.php service類別範例


namespace Service;
use test\TestCase;
include TEST_DIR . &#39;/TestCase.php&#39;;
include APP_PATH . &#39;/application/library/Service/BaseService.php&#39;;
include APP_PATH . &#39;/application/library/Service/TokenService.php&#39;;
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(&#39;22&#39;);
    $this->assertInternalType(&#39;array&#39;, $token);
    $this->assertInternalType(&#39;string&#39;, $token[&#39;token&#39;]);
  }

}

BaseControllerTest.php controller類別範例


#
namespace test\controller;
include TEST_DIR .&#39;/TestCase.php&#39;;
use test\TestCase;
class BaseControllerTest extends TestCase
{
  public function testGetConfigAction()
  {
    $request = new Simple(&#39;CLI&#39;, &#39;&#39;, &#39;Index&#39;, &#39;getConfig&#39;);
    $response = self::$_application->getDispatcher()->returnResponse(true)->dispatch($request);
    $contents = $response->getBody();
    $data = json_decode($contents, true);
    $this->assertInternalType(&#39;array&#39;, $data);
  }
}

相關推薦;

PHPYaf執行流程原始碼

分享在PHP 7下安裝Swoole與Yar,Yaf的方法教學

Yaf的hello world 範例

以上是實例詳解Yaf框架PHPUnit整合測試方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn