Commit f7eebdcb authored by wwccw0591's avatar wwccw0591

pc

parent 137c10a2
单元测试目录,关于单元测试可以参考:http://www.01happy.com/yaf-phpunit/
\ No newline at end of file
<?php
require_once APPLICATION_PATH . '/tests/application/library/Test/PHPUnit/ControllerTestCase.php';
/**
* 首页控制器测试类
*/
class IndexTest extends \Test\PHPUnit\ControllerTestCase {
/**
* 测试index方法
*/
public function testIndex() {
$request = new \Yaf\Request\Simple("CLI", "Index", "Index", 'index');
$response = $this->_application->getDispatcher()
->returnResponse(true)
->dispatch($request);
$content = $response->getBody();
$this->assertEquals('index phtml', $content);
}
}
<?php
namespace Test\PHPUnit;
require_once APPLICATION_PATH . '/tests/application/library/Test/PHPUnit/TestCase.php';
class ControllerTestCase extends \Test\PHPUnit\TestCase {
protected function _dispatch($request) {
try {
$response = $this->getApplication()->getDispatcher()
->catchException(false)
->returnResponse(true)
->dispatch($request);
$content = $response->getBody();
} catch (Exception $exc) {
$content = json_encode(array('errno' => $exc->getCode()));
}
return json_decode($content, true);
}
protected function _test($listTestData) {
foreach ($listTestData as $testData) {
if (isset($testData['cookie'])) {
$_COOKIE = $testData['cookie'];
}
if (isset($testData['post'])) {
$_POST = $testData['post'];
}
if (isset($testData['get'])) {
$_GET = $testData['get'];
}
$request = new \Yaf\Request\Simple("CLI", $testData['request'][0], $testData['request'][1], $testData['request'][2], $_GET);
$data = $this->_dispatch($request);
$this->assertSame($testData['code'], $data['errno']);
if (isset($testData['data'])) {
$this->assertEquals($testData['data'], $data['data']);
}
}
}
}
<?php
namespace Test\PHPUnit;
require_once APPLICATION_PATH . '/tests/application/library/Test/PHPUnit/TestCase.php';
/**
* 数据模型测试基类
*/
class ModelTestCase extends \Test\PHPUnit\TestCase {
}
<?php
namespace Test\PHPUnit;
class TestCase extends \PHPUnit_Framework_TestCase {
/**
* yaf运行实例
*
* @var \Yaf\Application
*/
protected $_application = null;
/**
* 构造方法,调用application实例化方法
*/
public function __construct() {
$this->_application = $this->getApplication();
parent::__construct();
}
/**
* 设置application
*/
public function setApplication() {
$application = new \Yaf\Application(APPLICATION_PATH . "/conf/application.ini");
$application->bootstrap();
\Yaf\Registry::set('application', $application);
return $application;
}
/**
* 获取application
*
* @return \Yaf\Application
*/
public function getApplication() {
$application = \Yaf\Registry::get('application');
if (!$application) {
$application = $this->setApplication();
}
return $application;
}
}
<?php
date_default_timezone_set("Asia/Shanghai");
mb_internal_encoding("UTF-8");
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../'));
\ No newline at end of file
<phpunit bootstrap="./bootstrap.php"></phpunit>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment