yaf,全称 yet another framework,是一个c语言编写的php框架。自从接触yaf以来也快1年了,yaf的性能确实非常不错,但是相应的资料比较少。一直想把phpunit使用在yaf上,网上搜了一圈这方面的资料非常非常少。于是自己折腾了下这个,初步处理了一下关于控制
yaf,全称 yet another framework,是一个c语言编写的php框架。自从接触yaf以来也快1年了,yaf的性能确实非常不错,但是相应的资料比较少。一直想把phpunit使用在yaf上,网上搜了一圈这方面的资料非常非常少。于是自己折腾了下这个,初步处理了一下关于控制器(controller)和模型(model)的测试。
代码可以直接在github下载:https://github.com/chenjiebin/yaf-phpunit-test/
先报下运行环境:
php:5.3.13或者5.4.16yaf:2.2.9phpunit:3.7.29
在php 5.3.13和5.4.16下都没有发现问题,yaf扩展要注意下载相应的版本。
下面说明下相关文件和代码。
测试目录为tests。
phpunit.xml文件
phpunit配置文件,内容如下:
表示测试执行时从bootstrap.php开始引导。bootstrap.php文件测试执行的引导文件,声明常量等一些全局配置都可以在这里做。内容如下:define(application_path, realpath(dirname(__file__) . '/../'));
application\library\test\phpunit\testcase.php文件
自定义的测试基类文件,控制器和模型的测试类都继承该类。代码如下:
namespace test\phpunit;/** * 测试基类 */class testcase extends \phpunit_framework_testcase { /** * yaf运行实例 * * @var \yaf\application */ protected $_application = null; /** * 构造方法,初始化yaf运行实例 */ 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); } /** * 获取application * * @return \yaf\application */ public function getapplication() { $application = \yaf\registry::get('application'); if (!$application) { $this->setapplication(); } return \yaf\registry::get('application'); }}
因为yaf在运行的时候,全局只能实例化一次,所以在初始化yaf运行实例后,就保存到yaf的注册表里避免多次实例化。此外在构造方法里就调用初始化yaf运行实例的方法,是为了在数据模型的测试中,可以直接new出相应的数据模型,而不用导入相关的文件。
application\library\test\phpunit\controllertestcase.php文件
控制器测试基类,控制器的测试类都继承该类。内容如下:
namespace test\phpunit;require_once application_path . '/tests/application/library/test/phpunit/testcase.php';/** * 控制器测试基类 */class controllertestcase extends \test\phpunit\testcase {}
暂时没有代码,为将来扩展预留。
application\library\test\phpunit\modeltestcase.php文件
模型测试基类,模型的测试类都继承该类。内容如下:
namespace test\phpunit;require_once application_path . '/tests/application/library/test/phpunit/testcase.php';/** * 数据模型测试基类 */class modeltestcase extends \test\phpunit\testcase {}
application\controllers\indextest.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); }}
测试控制器主要是使用\yaf\request\simple类,设定参数为cli则为命令行运行。
application\controllers\models\usertest.php模型测试文件
数据模型测试文件,内容如下:
require_once application_path . '/tests/application/library/test/phpunit/modeltestcase.php';class usertest extends \test\phpunit\modeltestcase { public function testgetusername() { $model = new \usermodel(); $userid = 1; $result = $model->getusername($userid); $this->assertequals('iceup', $result); $userid = 100; $result = $model->getusername($userid); $this->assertfalse($result); }}
这里可以直接new \usermodel()出来,因为在测试基类的构造方法里已经初始化了yaf运行实例。
小结
执行测试的时候一般最头疼的是文件的自动加载问题,基本上只要解决了这个问题,剩下都比较容易处理了。目前在项目的运用中碰到一些实际的问题,比如输出json格式的数据,抛出异常,post传参等,以后会陆续补上相应的解决方式。
转载请注明:快乐编程 » yaf框架结合phpunit的集成测试