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.

tests.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. require "api.php";
  3. class MySQL_CRUD_API_Config
  4. {
  5. public static $username='{{username}}';
  6. public static $password='{{password}}';
  7. public static $database='{{test_database}}'; // NB: Use an empty database, data will be LOST!
  8. }
  9. class API
  10. {
  11. protected $test;
  12. protected $api;
  13. public function __construct($test)
  14. {
  15. $this->test = $test;
  16. }
  17. private function action($method,$url,$data=false)
  18. {
  19. $url = parse_url($url);
  20. $query = isset($url['query'])?$url['query']:'';
  21. parse_str($query,$get);
  22. if ($data) {
  23. $data = 'data://text/plain;base64,'.base64_encode($data);
  24. }
  25. $this->api = new MySQL_CRUD_API(array(
  26. 'hostname'=>'localhost',
  27. 'username'=>MySQL_CRUD_API_Config::$username,
  28. 'password'=>MySQL_CRUD_API_Config::$password,
  29. 'database'=>MySQL_CRUD_API_Config::$database,
  30. 'charset'=>'utf8',
  31. // for tests
  32. 'method' =>$method,
  33. 'request' =>$url['path'],
  34. 'post'=>$data?:'php://input',
  35. 'get' =>$get,
  36. ));
  37. return $this;
  38. }
  39. public function get($url)
  40. {
  41. return $this->action('GET',$url);
  42. }
  43. public function post($url,$data)
  44. {
  45. return $this->action('POST',$url,$data);
  46. }
  47. public function put($url,$data)
  48. {
  49. return $this->action('PUT',$url,$data);
  50. }
  51. public function delete($url)
  52. {
  53. return $this->action('DELETE',$url);
  54. }
  55. public function expect($output,$error=false)
  56. {
  57. $exception = false;
  58. ob_start();
  59. try {
  60. $this->api->executeCommand();
  61. } catch (\Exception $e) {
  62. $exception = $e->getMessage();
  63. }
  64. $data = ob_get_contents();
  65. ob_end_clean();
  66. $this->test->assertEquals($output, $data);
  67. $this->test->assertEquals($error, $exception);
  68. return $this;
  69. }
  70. }
  71. class MySQL_CRUD_API_Test extends PHPUnit_Framework_TestCase
  72. {
  73. private static function checkConfig()
  74. {
  75. $file = __FILE__;
  76. $file = str_replace(rtrim(getcwd(),'/').'/', '', $file);
  77. if (MySQL_CRUD_API_Config::$database=='{{test_database}}') {
  78. die("Configure database in '$file' before running tests.\n");
  79. }
  80. }
  81. public static function setUpBeforeClass()
  82. {
  83. static::checkConfig();
  84. $fixture = 'blog.sql';
  85. $username = escapeshellarg(MySQL_CRUD_API_Config::$username);
  86. $password = escapeshellarg(MySQL_CRUD_API_Config::$password);
  87. $database = escapeshellarg(MySQL_CRUD_API_Config::$database);
  88. $command = "cat $fixture | mysql -u$username -p$password $database 2>&1";
  89. $output = shell_exec($command);
  90. if ($output) die("Could not load fixture '$fixture':\n$output");
  91. }
  92. public function testListPosts()
  93. {
  94. $test = new API($this);
  95. $test->get('/posts');
  96. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["1","1","1","blog started"],["2","1","2","It works!"]]}}');
  97. }
  98. public function testListPostsWithTransform()
  99. {
  100. $test = new API($this);
  101. $test->get('/posts?transform=1');
  102. $test->expect('{"posts":[{"id":"1","user_id":"1","category_id":"1","content":"blog started"},{"id":"2","user_id":"1","category_id":"2","content":"It works!"}]}');
  103. }
  104. public function testReadPost()
  105. {
  106. $test = new API($this);
  107. $test->get('/posts/2');
  108. $test->expect('{"id":"2","user_id":"1","category_id":"2","content":"It works!"}');
  109. }
  110. public function testAddPost()
  111. {
  112. $test = new API($this);
  113. $test->post('/posts','{"user_id":"1","category_id":"1","content":"test"}');
  114. $test->expect('3');
  115. }
  116. public function testEditPost()
  117. {
  118. $test = new API($this);
  119. $test->put('/posts/3','{"user_id":"1","category_id":"1","content":"test (edited)"}');
  120. $test->expect('1');
  121. $test->get('/posts/3');
  122. $test->expect('{"id":"3","user_id":"1","category_id":"1","content":"test (edited)"}');
  123. }
  124. public function testEditPostWithUtf8Content()
  125. {
  126. $utf8 = json_encode('Hello world, Καλημέρα κόσμε, コンニチハ');
  127. $test = new API($this);
  128. $test->put('/posts/2','{"content":'.$utf8.'}');
  129. $test->expect('1');
  130. $test->get('/posts/2');
  131. $test->expect('{"id":"2","user_id":"1","category_id":"2","content":'.$utf8.'}');
  132. }
  133. public function testDeletePost()
  134. {
  135. $test = new API($this);
  136. $test->delete('/posts/3');
  137. $test->expect('1');
  138. $test->get('/posts/3');
  139. $test->expect('','Not found (object)');
  140. }
  141. public function testListWithPaginate()
  142. {
  143. $test = new API($this);
  144. for ($i=1;$i<=10;$i++) {
  145. $test->post('/posts','{"user_id":"1","category_id":"1","content":"#'.$i.'"}');
  146. $test->expect(3+$i);
  147. }
  148. $test->get('/posts?page=2,2&order=id');
  149. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["4","1","1","#1"],["5","1","1","#2"]],"results":12}}');
  150. }
  151. public function testListWithPaginateLastPage()
  152. {
  153. $test = new API($this);
  154. $test->get('/posts?page=3,5&order=id');
  155. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["12","1","1","#9"],["13","1","1","#10"]],"results":12}}');
  156. }
  157. public function testListExampleFromReadme()
  158. {
  159. $test = new API($this);
  160. $test->get('/posts,categories,tags,comments?filter=id,eq,1');
  161. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["1","1","1","blog started"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[["1","1","1"],["2","1","2"]]},"categories":{"relations":{"id":"posts.category_id"},"columns":["id","name"],"records":[["1","anouncement"]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[["1","funny"],["2","important"]]},"comments":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","message"],"records":[["1","1","great"],["2","1","fantastic"]]}}');
  162. }
  163. public function testListExampleFromReadmeWithTransform()
  164. {
  165. $test = new API($this);
  166. $test->get('/posts,categories,tags,comments?filter=id,eq,1&transform=1');
  167. $test->expect('{"posts":[{"id":"1","post_tags":[{"id":"1","post_id":"1","tag_id":"1","tags":[{"id":"1","name":"funny"}]},{"id":"2","post_id":"1","tag_id":"2","tags":[{"id":"2","name":"important"}]}],"comments":[{"id":"1","post_id":"1","message":"great"},{"id":"2","post_id":"1","message":"fantastic"}],"user_id":"1","category_id":"1","categories":[{"id":"1","name":"anouncement"}],"content":"blog started"}]}');
  168. }
  169. }