详解Yaf框架PHPUnit集成测试方法
发布人:shili8
发布时间:2022-12-07 22:30
阅读次数:22
本文介绍了详解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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

