• 测试Behavior
    • 建议

    测试Behavior

    Behavior 遵循TP的自动加载方式,所以直接创建对应的Behavior 并填写模拟参数调用run()方法即可

    样例:

    行为PaidChongzhiOrderBehavior.class.php

    1. /**
    2. * 支付充值订单后
    3. */
    4. class PaidChongzhiOrderBehavior extends BaseBehavior {
    5. /**
    6. * @param PaidOrderBehaviorParam $param
    7. */
    8. public function run(&$param) {
    9. parent::run($param);
    10. //你的代码
    11. }
    12. }

    Behavior参数类PaidOrderBehaviorParam.class.php

    1. class PaidOrderBehaviorParam extends BaseBehaviorParam {
    2. /**
    3. * @var 订单号
    4. */
    5. public $orderid;
    6. }

    调用样例:

    1. <?php
    2. namespace Payment\Controller;
    3. use Common\Controller\Base;
    4. use Payment\Behavior\PaidChongzhiOrderBehavior;
    5. use Payment\BehaviorParam\PaidOrderBehaviorParam;
    6. class TestController extends Base {
    7. function testPaidChongzhiOrderBehavior(){
    8. $b = new PaidChongzhiOrderBehavior();
    9. $bp = PaidOrderBehaviorParam::create(['orderid' => '2016111011144970299']);
    10. $b->run($bp);
    11. }
    12. }

    建议

    为每个行为都配合写一个 PaidOrderBehaviorParam ,这样明确了,执行该行为的回调函数时
    到底含有哪一些参数

    BaseBehavior.class参考如下(未来将会内置到 CMS 中):

    1. <?php
    2. namespace Common\BehaviorParam;
    3. /**
    4. * 行为回调参数
    5. *
    6. */
    7. class BaseBehaviorParam {
    8. function __get($name) {
    9. return $this->$name;
    10. }
    11. function __set($name, $value) {
    12. $this->$name = $value;
    13. }
    14. /**
    15. * 创建参数实例
    16. * @param array $data
    17. * @return mixed
    18. */
    19. static function create(array $data) {
    20. $class = __CLASS__;
    21. $param = new $class;
    22. foreach ($data as $key => $val){
    23. if(empty($param->$key)){
    24. $param->$key = $val;
    25. }
    26. }
    27. return $param;
    28. }
    29. }