ValueParserTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace EasySwoole\Annotation\Tests;
  3. use EasySwoole\Annotation\ValueParser;
  4. use PHPUnit\Framework\TestCase;
  5. class ValueParserTest extends TestCase
  6. {
  7. function testNormal()
  8. {
  9. $str = "int=1";
  10. $this->assertEquals([
  11. 'int'=>1
  12. ],ValueParser::parser($str));
  13. $str = "int=1,int2=2";
  14. $this->assertEquals([
  15. 'int'=>"1",
  16. 'int2'=>"2"
  17. ],ValueParser::parser($str));
  18. $str = "int=1,str='2'";
  19. $this->assertEquals([
  20. 'int'=>"1",
  21. 'str'=>"'2'"
  22. ],ValueParser::parser($str));
  23. }
  24. function testArray()
  25. {
  26. $str = "array={1,2,3}";
  27. $this->assertEquals([
  28. 'array'=>[1,2,3]
  29. ],ValueParser::parser($str));
  30. $str = "array={'1','2','3'}";
  31. $this->assertEquals([
  32. 'array'=>["'1'","'2'","'3'"]
  33. ],ValueParser::parser($str));
  34. $str = 'array={"1","2 , 3"}';
  35. $this->assertEquals([
  36. 'array'=>["1","2 , 3"]
  37. ],ValueParser::parser($str));
  38. $str = "array={1,2,3} ,array2={4,5,6}";
  39. $this->assertEquals([
  40. 'array'=>[1,2,3],
  41. 'array2'=>[4,5,6]
  42. ],ValueParser::parser($str));
  43. }
  44. function testEval()
  45. {
  46. $str = 'time="eval(time() + 30)"';
  47. $this->assertEquals([
  48. 'time'=>time() + 30,
  49. ],ValueParser::parser($str));
  50. $str = 'time="eval(time() + 30)" , time2="eval(time() + 31)';
  51. $this->assertEquals([
  52. 'time'=>time() + 30,
  53. 'time2'=>time() + 31
  54. ],ValueParser::parser($str));
  55. $str = 'list="eval([1,2,3,4])"';
  56. $this->assertEquals([
  57. 'list'=>[1,2,3,4]
  58. ],ValueParser::parser($str));
  59. }
  60. function testArrayAndEval()
  61. {
  62. $str = 'array={1,2,eval(time() + 30)}';
  63. $this->assertEquals([
  64. 'array'=>[1,2,time() + 30]
  65. ],ValueParser::parser($str));
  66. $str = 'array={1,2,eval(time() + 30)},str="222"';
  67. $this->assertEquals([
  68. 'array'=>[1,2,time() + 30],
  69. "str"=>'222'
  70. ],ValueParser::parser($str));
  71. $str = "array={1,2,3},time=eval(time())";
  72. $this->assertEquals([
  73. 'array'=>[1,2,3],
  74. 'time'=>time()
  75. ],ValueParser::parser($str));
  76. }
  77. function testStrMulti()
  78. {
  79. $str = 'mix={first,{1,2,3},eval(time() + 3)},int=1,strInt="2",arr={1,2,3},b="asdasda",d=abcdefh';
  80. $this->assertEquals([
  81. 'mix'=>['first',['1','2','3'],time() + 3],
  82. 'int'=>1,
  83. 'strInt'=>'2',
  84. 'arr'=>[1,2,3],
  85. 'b'=>'asdasda',
  86. 'd'=>'abcdefh'
  87. ],ValueParser::parser($str));
  88. }
  89. }