Problem background: There is a common problem in single testing,
The private method in the side class cannot be called directly
. During the processing, Xiaoyan changed the method permissions throughreflection, conducted a single test
, shared it, and directly uploaded the code.
Simple test class
Generate a simple test class with only one private method.
<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>
Single test 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>
Running results
<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>
Key Code analysis
encapsulates a reflection call of the method of the class under test; at the same time, The access permission of the processing method before the return method is true
, so Can access private function methods.
<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>
【Please indicate when reprinting: calling private method in phpunit single test | Reliable Cui Xiaoyan】
The above introduces the processing of calling private methods in phpunit single test, including require, code analysis, and Exception. I hope it will be helpful to friends who are interested in PHP tutorials.