Mock.js 生成测试数据 MockValue.php

* Mock.js

function Mock() {}

Mock.mobile_prefix = ["134", "135", "136", "137", "138", "139", "150", "151", 
"152", "157", "158", "159", "130","131", "132", "155", "156", "133", "153"];

Mock.numeric = "0123456789";

Mock.lowerCase = "abcdefghijklmnopqrstuvwxyz"
Mock.upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Mock.email_suffix = ["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", 
    "@aol.com", "@ask.com","@live.com", "@qq.com", "@0355.net", "@163.com", 
	"@163.net", "@263.net", "@3721.net", "@yeah.net", "@126.com", "@sina.com",
	"@sohu.com", "@yahoo.com.cn"];

Mock.grade = ['高三','高二','高一','初三','初二','初一','小六','六年级','七年级','八年级','九年级','高中','初中','小学'];

Mock.gradeValue = ["03-2016", "03-2017", "03-2018", "02-2016", "02-2017", "02-2018", "02-2015", "02-2016", "02-2017", "02-2018", "01-2013"];

Mock.random = function(len, list) {
   if (len <= 1) { len = 1; }
   var s = "";
   var n = list.length;
   if (typeof list === "string") {
      while (len-- > 0 ){      
	    s += list.charAt(Math.random() * n)
	  }
   } else if (list instanceof Array) {
	  while (len-- > 0 ){      
	    s += list[ Math.floor(Math.random() * n) ]
	  }
   }
   return s;
}

Mock.randomInt = function(min, max) {
	return min + Math.floor(Math.random() * (max-min+1))
}

Mock.pick = function(list) {
	if (! list.length ) {
		return undefined;
	}
	return list[ Math.floor(Math.random() * list.length) ]
}

Mock.getUsername = function() {
	return Mock.random(8, Mock.lowerCase)
}

Mock.getMobile = function() {
 return Mock.random(1, Mock.mobile_prefix) 
       + Mock.random(8, Mock.numeric);
}
Mock.getGrade = function() {
	return Mock.random(1, Mock.grade);
}
Mock.getEmail = function() {
	var opt = Mock.numeric + Mock.lowerCase + Mock.upperCase
	return Mock.random( Mock.randomInt(4, 10), opt) +
		Mock.random(1, Mock.email_suffix);
}

 

* MockValue.php

<?php

class MockValue {
    public static $letters = "abcdefghijklmnopqrstuvwxyz";

    public static $numeric = "0123456789";

    public static $email_suffix =
                                ["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", "@aol.com", "@ask.com",
                                 "@live.com", "@qq.com", "@0355.net", "@163.com", "@163.net",
                                 "@263.net", "@3721.net", "@yeah.net", "@126.com", "@sina.com", "@sohu.com", "@yahoo.com.cn"];
    public static $mobile_prefix = ["134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "130",
                                    "131", "132", "155", "156", "133", "153"];
    public static $grade = ['高三','高二','高一','初三','初二','初一','小六','六年级','七年级','八年级','九年级','高中','初中','小学'];

    public static $gradeValue = ["03-2016", "03-2017", "03-2018", "02-2016", "02-2017", "02-2018", "02-2015", "02-2016", "02-2017", "02-2018", "01-2013"];

    public static function getNumber(/* int */$width) /* int */ {
        $min = 1;
        if ($width <= 1) {
            $width = 1;
            return rand(0, 9);
        }
        $width -= 1;
        for ($i = 0; $i <$width; $i++) {
            $min *= 10;
        }
        $max = $min * 10 - 1;
        return rand($min, $max);
    }

    public static function getMobile() {
        return self::random(1, self::$mobile_prefix) . self::random(8, self::$numeric);
    }

    public static function getGrade() {
        return self::random(1, self::$grade);
    }

    public static function getGradeValue() {
        return self::pick(self::$gradeValue);
    }


    public static function getElement($list) {
       if (is_string($list)) {
            $n = strlen($list);
        } else if (is_array($list)) {
            $n = count($list);
        } else {
            throw new InvalidArgumentException("list must string or array");
        }
        return $list[rand(0, $n-1)];
    }

    public static function getName() {
        return self::random(8, self::$letters);
    }

    private static function random(/*int */$length, /* ArrayAccess */ $list) {
        if ($length <= 1) {
            $length = 1;
        }
        $s = "";
        if (is_string($list)) {
            $n = strlen($list);
        } else if (is_array($list)) {
            $n = count($list);
        } else {
            throw new InvalidArgumentException("list must string or array");
        }

        while ($length--) {
            $s .= $list[ rand(0, $n-1) ];  // inclusive $n-1
        }
        return $s;
    }

    public static function pick($list) {
        $n = count($list);
        if ($n < 1) {
            throw new RunTimeException("Empty list");
        }
        return $list[ rand(0, $n-1) ];
    }

    public static function main() {
        echo self::getNumber(5).PHP_EOL;
        echo self::getMobile().PHP_EOL;
        echo self::getGrade().PHP_EOL;
    }

}

// MockValue::main();

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值