詳解Yaf框架PHPUnit集成測試方法
本文介紹了詳解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 測試文件基礎(chǔ)類
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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
laravel validate 設(shè)置為中文的例子(驗證提示為中文)
今天小編就為大家分享一篇laravel validate 設(shè)置為中文的例子(驗證提示為中文),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
基于PHP讀取TXT文件向數(shù)據(jù)庫導(dǎo)入海量數(shù)據(jù)的方法
本篇文章小編為大家介紹,基于PHP讀取TXT文件向數(shù)據(jù)庫導(dǎo)入海量數(shù)據(jù)的方法。需要的朋友參考下2013-04-04
淺談laravel orm 中的一對多關(guān)系 hasMany
今天小編就為大家分享一篇淺談laravel orm 中的一對多關(guān)系 hasMany,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP之深入學(xué)習(xí)Yii2緩存Cache組件詳細(xì)講解
這篇文章主要介紹了PHP之深入學(xué)習(xí)Yii2緩存Cache組件詳細(xì)講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作
yii2.0框架是PHP開發(fā)的一個比較高效率的框架,集合了作者的大量心血,下面通過用戶為例給大家詳解yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作2015-11-11

