利用php反射构造自动测试工具

<?php
namespace app\console;

use yii\console\Controller;

/**
 * Lib测试工具
 * Class TestController
 * @package app\console
 *
$this->runCase('app\\library\\Activity',[
    'constructor' => [],//构造方法参数
    'common' => [ //公共参数
        'activities_id' => 2,
        'users_openid' => 'test-openid',
    ],
    'method1' => []//方法参数

]);
 */
class TestController extends Controller
{
    /**
     * 测试方法
     * @param $class_name string 类的全名(带ns)
     * @param $params [] 需要用到的参数
     */
    private function runCase($class_name,$params){
        try{
            $class = new \ReflectionClass($class_name);
            echo "Begin test [{$class_name}]\n";
            $instance = null;
            $constructor = $class->getConstructor();
            if($constructor){
                $args = [];
                foreach($constructor->getParameters() as $parameter){
                    $args[] = isset($params['constructor'][$parameter->name])
                                ? $params['constructor'][$parameter->name]
                                : null;
                }
                $instance = $class->newInstanceArgs($args);
                echo "Got new instance [\$instance] with constructor args: ".print_r($args,1)."\n";
            }
            $methods = $class->getMethods();

            foreach($methods as $method){
                if($method->isPrivate())
                    continue;
                $args = [];
                $args_name = [];
                foreach($method->getParameters() as $parameter){
                    if($parameter->isPassedByReference()){
                        echo "{$method->name}'s \${$parameter->name}: reference pass is not support\n";
                        echo "pass.... \n";
                        break 2;
                    }elseif(isset($params[$method->name][$parameter->name])){
                        $value = $params[$method->name][$parameter->name];
                    }elseif($parameter->isDefaultValueAvailable()){
                        $value = $parameter->getDefaultValue();
                    }elseif(isset($params['common'][$parameter->name])){
                        $value = $params['common'][$parameter->name];
                    }else{
                        throw new \Exception("Need set {$parameter->name}");
                    }
                    $args[] = $value;
                    $args_name[] = "\${$parameter->name} = {$value}";
                }
                $method_invoke = $method->isStatic() ? "{$class_name}::{$method->name}" : "\$instance->{$method->name}";
                echo "Call method [{$method_invoke}(".implode(', ',$args_name).")]\n";
                echo "Method return\n";
                echo print_r($method->invokeArgs($method->isStatic() ? null : $instance,$args),1),"\n";
                echo "Method done.\n\n";
            }
            echo "End [{$class_name}]\n";
        }catch (\Exception $e){
            echo $e->getMessage();
        }

    }


    public function actionAct(){
        $this->runCase('app\\library\\Activity',[
            'constructor' => [],
            'common' => [
                'activities_id' => 2,
                'users_openid' => 'test-openid',
            ],

        ]);
    }

    public function actionOrder(){
        $this->runCase('app\\library\\Order',[
            'constructor' => [],
            'common' => [
                'activities_id' => 2,
                'users_openid' => 'test-openid'
            ],

        ]);
    }

    public function actionBrand(){

    }

}

输出示例:

Begin test [app\library\Activity]

Call method [app\library\Activity::setCollect($activities_id = 2, $users_openid = test-openid)]
Method return
1
Method done.

Call method [app\library\Activity::isJoin($activities_id = 2, $users_openid = test-openid)]
Method return
1
Method done.

Call method [app\library\Activity::isCollect($activities_id = 2, $users_openid = test-openid)]
Method return

Method done.

End [app\library\Activity]
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值