php 单元测试 集成测试,详解Yaf框架PHPUnit集成测试方法

本文介绍了详解Yaf框架PHPUnit集成测试方法,分享给大家,具体如下:

测试目录

test

├── TestCase.php

├── bootstrap.php

├── controller

│ ├── BaseControllerTest.php

│ └── IndexControllerTest.php

├── model

├── phpunit.xml

└── service

└── TokenServiceTest.php

phpunit.xml

xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.2/phpunit.xsd"

extensionsDirectory="dbunit.phar" bootstrap="./bootstrap.php">

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

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值