问题背景:单测中有个普遍性的问题,被侧类中的private方法无法直接调用。小拽在处理过程中通过反射改变方法权限,进行单测,分享一下,直接上代码。
简单被测试类
生成一个简单的被测试类,只有个private方法。
<code><?php
/**
* 崔小涣单测的基本模板。
*
* @author cuihuan
* @date 2015/11/12 22:15:31
* @version $Revision:1.0$
**/
class MyClass {
/**
* 私有方法
*
* @param $params
* @return bool
*/
private function privateFunc($params){
if(!isset($params)){
return false;
}
echo "test success";
return $params;
}
}</code>单测代码
<code><?php
/***************************************************************************
*
* $Id: MyClassTest T,v 1.0 PsCaseTest cuihuan Exp$
*
**************************************************************************/
/**
* 崔小涣单测的基本模板。
*
* @author cuihuan
* @date 2015/11/12 22:09:31
* @version $Revision:1.0$
**/
<strong>require</strong>_once ('./MyClass.php');
class MyClassTest extends PHPUnit_Framework_TestCase {
const CLASS_NAME = 'MyClass';
const FAIL = 'fail';
protected $objMyClass;
/**
* @brief setup: Sets up the fixture, for example, opens a network connection.
*
* 可以看做phpunit的构造函数
*/
public function setup() {
date_default_timezone_set('PRC');
$this->objMyClass = new MyClass();
}
/**
* 利用反射,对类中的private 和 protect 方法进行单元测试
*
* @param $strMethodName string :反射函数名
* @return ReflectionMethod obj :回调对象
*/
protected static function getPrivateMethod($strMethodName) {
$objReflectClass = new ReflectionClass(self::CLASS_NAME);
$method = $objReflectClass->getMethod($strMethodName);
$method->setAccessible(true);
return $method;
}
/**
* @brief :测试private函数的调用
*/
public function testPrivateFunc()
{
$testCase = 'just a test string';
// 反射该类
$testFunc = self::getPrivateMethod('privateFunc');
$res = $testFunc->invokeArgs($this->objMyClass, array($testCase));
$this->assertEquals($testCase, $res);
$this->expectOutputRegex('/success/i');
// 捕获没有参数异常测试
try {
$testFunc->invokeArgs($this->transfer2Pscase, array());
} catch (<strong>Exception</strong> $expected) {
$this->assertNotNull($expected);
return true;
}
$this->fail(self::FAIL);
}
}</code>运行结果
<code>cuihuan:test cuixiaohuan$ phpunit MyClassTest.php PHPUnit 4.8.6 by Sebastian Bergmann and contributors. Time: 103 ms, Memory: 11.75Mb OK (1 test, 3 assertions)</code>
关键代码分析
立即学习“PHP免费学习笔记(深入)”;
封装了一个,被测类方法的反射调用;同时,返回方法之前处理方法的接入权限为true,便可以访问private的函数方法。
<code>/**
* 利用反射,对类中的private 和 protect 方法进行单元测试
*
* @param $strMethodName string :反射函数名
* @return ReflectionMethod obj :回调对象
*/
protected static function getPrivateMethod($strMethodName) {
$objReflectClass = new ReflectionClass(self::CLASS_NAME);
$method = $objReflectClass->getMethod($strMethodName);
$method->setAccessible(true);
return $method;
}
</code>【转载请注明:phpunit单测中调用private方法处理 | 靠谱崔小拽 】
以上就介绍了phpunit单测中调用private方法处理,包括了require,代码分析,Exception方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号