api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. require "api.php";
  3. class MySQL_CRUD_API_Test extends PHPUnit_Framework_TestCase
  4. {
  5. private function expectQueries($queries)
  6. {
  7. $mysqli = $this->getMockBuilder('mysqli')
  8. ->setMethods(array('query','real_escape_string'))
  9. ->getMock();
  10. $mysqli->expects($this->any())
  11. ->method('real_escape_string')
  12. ->will($this->returnArgument(0));
  13. $mysqli->expects($this->any())
  14. ->method('query')
  15. ->will($this->returnCallback(function() use ($queries) {
  16. static $i = 0;
  17. $args = func_get_args();
  18. list($query,$fields,$results) = $queries[$i++];
  19. $fields = array_map(function($v){ return (object)array('name'=>$v); },$fields);
  20. $this->assertEquals($query,$args[0]);
  21. $mysqli_result = $this->getMockBuilder('mysqli_result')
  22. ->setMethods(array('fetch_fields','fetch_row','close'))
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $mysqli_result->expects($this->any())
  26. ->method('fetch_fields')
  27. ->willReturn($fields);
  28. $mysqli_result->expects($this->any())
  29. ->method('fetch_row')
  30. ->will($this->returnCallback(function() use ($results) {
  31. static $r = 0;
  32. return isset($results[$r])?$results[$r++]:false;
  33. }));
  34. return $mysqli_result;
  35. }));
  36. return $mysqli;
  37. }
  38. private function expect($method,$url,$queries,$output)
  39. {
  40. $url = explode('?',$url,2);
  41. $request = $url[0];
  42. $get = array();
  43. if (isset($url[1])) {
  44. parse_str($url[1],$get);
  45. }
  46. $mysqli = $this->expectQueries($queries);
  47. $config = array(
  48. 'method' =>$method,
  49. 'request' =>$request,
  50. 'get' =>$get,
  51. 'database'=>'database',
  52. 'mysqli'=>$mysqli,
  53. );
  54. $api = new MySQL_CRUD_API($config);
  55. $this->expectOutputString($output);
  56. $api->executeCommand();
  57. }
  58. public function testList()
  59. {
  60. $this->expect(
  61. "GET",
  62. "/table",
  63. array(
  64. array(
  65. "SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'table' AND `TABLE_SCHEMA` = 'database'",
  66. array('table_name'),
  67. array(array('table')),
  68. ),array(
  69. "SELECT * FROM `table`",
  70. array('id','name'),
  71. array(array('1','value1'),array('2','value2'),array('3','value3'),array('4','value4'),array('5','value5')),
  72. )
  73. ),
  74. '{"table":{"columns":["id","name"],"records":[["1","value1"],["2","value2"],["3","value3"],["4","value4"],["5","value5"]]}}'
  75. );
  76. }
  77. public function testListPageFilterMatchOrder()
  78. {
  79. $this->expect(
  80. "GET",
  81. "/table?page=2,2&filter=id:3&match=from&order=id,desc",
  82. array(
  83. array(
  84. "SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'table' AND `TABLE_SCHEMA` = 'database'",
  85. array('table_name'),
  86. array(array('table')),
  87. ),array(
  88. "SELECT COUNT(*) FROM `table` WHERE `id` >= '3'",
  89. array('count'),
  90. array(array('3')),
  91. ),array(
  92. "SELECT * FROM `table` WHERE `id` >= '3' ORDER BY `id` DESC LIMIT 2 OFFSET 2",
  93. array('id','name'),
  94. array(array('3','value3')),
  95. )
  96. ),
  97. '{"table":{"columns":["id","name"],"records":[["3","value3"]],"results":3}}'
  98. );
  99. }
  100. }