phpunit单测中调用private方法处理

php中文网
发布: 2016-07-29 09:10:00
原创
1599人浏览过
问题背景:单测中有个普遍性的问题,被侧类中的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>
登录后复制

关键代码分析

表单大师AI
表单大师AI

一款基于自然语言处理技术的智能在线表单创建工具,可以帮助用户快速、高效地生成各类专业表单。

表单大师AI 74
查看详情 表单大师AI

立即学习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在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号