本文介绍了详解Yaf框架PHPUnit集成测试方法,分享给大家,具体如下:
测试目录
PHP
├── bootstrap.PHP
├── controller
│ ├── BaseControllerTest.PHP
│ └── IndexControllerTest.PHP
├── model
├── PHPunit.xml
└── service
└── TokenServiceTest.PHP
PHPunit.xml
<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">
PHP;">
define("APP_PATH",realpath(dirname(__FILE__) . '/../'));
date_default_timezone_set("Asia/Shanghai");
define("TEST_DIR",__DIR__);
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类例子
public function testCreateToken()
{
$token = self::$tokenService->createToken('22');
$this->assertInternalType('array',$token);
$this->assertInternalType('string',$token['token']);
}
{
$token = self::$tokenService->createToken('22');
$this->assertInternalType('array',$token);
$this->assertInternalType('string',$token['token']);
}
}
BaseControllerTest.PHP controller类例子
getDispatcher()->returnResponse(true)->dispatch($request);
$contents = $response->getBody();
$data = json_decode($contents,true);
$this->assertInternalType('array',$data);
}
}