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);
}
}