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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Api.php 4.6KB

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