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.

Api.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require_once(__DIR__ . '/../api.php');
  3. class Api
  4. {
  5. /**
  6. * Database configuration array
  7. *
  8. * @var array
  9. */
  10. protected $config;
  11. /**
  12. * @var PHP_CRUD_API_Test
  13. */
  14. protected $test;
  15. /**
  16. * @var PHP_CRUD_API
  17. */
  18. protected $api;
  19. public function __construct($test)
  20. {
  21. $this->test = $test;
  22. $this->config = $test::$config;
  23. $this->config['dbengine'] = $test->getEngineName();
  24. }
  25. private function action($method,$url,$data='')
  26. {
  27. $url = parse_url($url);
  28. $query = isset($url['query'])?$url['query']:'';
  29. parse_str($query,$get);
  30. $this->api = new PHP_CRUD_API(array(
  31. 'dbengine'=>$this->config['dbengine'],
  32. 'hostname'=>$this->config['hostname'],
  33. 'username'=>$this->config['username'],
  34. 'password'=>$this->config['password'],
  35. 'database'=>$this->config['database'],
  36. // callbacks
  37. 'table_authorizer'=>function($action,$database,$table) { return true; },
  38. 'column_authorizer'=>function($action,$database,$table,$column) { return !($column=='password'&&$action=='list'); },
  39. 'record_filter'=>function($action,$database,$table) { return ($table=='posts')?array('id,neq,13'):false; },
  40. 'tenancy_function'=>function($action,$database,$table,$column) { return ($table=='users'&&$column=='id')?1:null; },
  41. 'input_sanitizer'=>function($action,$database,$table,$column,$type,$value) { return is_string($value)?strip_tags($value):$value; },
  42. 'input_validator'=>function($action,$database,$table,$column,$type,$value,$context) { return ($column=='category_id' && !is_numeric($value))?'must be numeric':true; },
  43. 'before'=>function(&$action,&$database,&$table,&$id,&$input) { if ($table=='products') if ($action=='create') $input->created_at = '2013-12-11 10:09:08'; else if ($action=='delete') { $action='update'; $input = (object)array('deleted_at' => '2013-12-11 11:10:09'); } },
  44. 'after'=>function($action,$database,$table,$id,$input,$output) { file_put_contents('log.txt',var_export(array($action,$database,$table,$id,$input,$output),true),FILE_APPEND); },
  45. // for tests
  46. 'method'=>$method,
  47. 'request'=>$url['path'],
  48. 'post'=>$data,
  49. 'get'=>$get,
  50. ));
  51. return $this;
  52. }
  53. public function get($url)
  54. {
  55. return $this->action('GET',$url);
  56. }
  57. public function post($url,$data)
  58. {
  59. return $this->action('POST',$url,$data);
  60. }
  61. public function put($url,$data)
  62. {
  63. return $this->action('PUT',$url,$data);
  64. }
  65. public function delete($url)
  66. {
  67. return $this->action('DELETE',$url);
  68. }
  69. public function options($url)
  70. {
  71. return $this->action('OPTIONS',$url);
  72. }
  73. public function patch($url,$data)
  74. {
  75. return $this->action('PATCH',$url,$data);
  76. }
  77. public function expectAny()
  78. {
  79. ob_start();
  80. $this->api->executeCommand();
  81. ob_end_clean();
  82. return $this;
  83. }
  84. public function expect($output,$error=false)
  85. {
  86. $exception = false;
  87. ob_start();
  88. try {
  89. $this->api->executeCommand();
  90. } catch (\Exception $e) {
  91. $exception = $e->getMessage();
  92. }
  93. $data = ob_get_contents();
  94. ob_end_clean();
  95. if ($exception) $this->test->assertEquals($error, $exception);
  96. else $this->test->assertEquals($output, $data);
  97. return $this;
  98. }
  99. }