PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/flax_search_service/clients/php/unittests/_testrestclient.php

http://flaxcode.googlecode.com/
PHP | 226 lines | 178 code | 25 blank | 23 comment | 50 complexity | debc312ce19d920b390b7b3a9faa6fad MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, GPL-2.0, BSD-3-Clause, AGPL-1.0
  1. <?php
  2. /* Copyright (C) 2009 Lemur Consulting Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. /**
  19. * Class faking a RestClient for the purposes of testing. This doesn't actually
  20. * access any remote resource, but acts like it does.
  21. *
  22. */
  23. require_once('../flaxerrors.php');
  24. class _MockDatabase {
  25. function __construct() {
  26. $this->created_date = new DateTime();
  27. $this->last_modified_date = new DateTime();
  28. $this->fields = array();
  29. $this->docs = array();
  30. }
  31. function as_array() {
  32. return array(
  33. 'doccount' => count($this->docs),
  34. 'created_date' => $this->created_date,
  35. 'last_modified_date' => $this->last_modified_date
  36. );
  37. }
  38. }
  39. class FlaxTestRestClient {
  40. function __construct($baseurl=null) {
  41. $this->baseurl = $baseurl;
  42. $this->dbs = array();
  43. $this->patterns = array(
  44. # more complex ones first!
  45. array('/^dbs\/(.+)\/schema\/fields\/(.+)$/', 'f_field'),
  46. array('/^dbs\/(.+)\/schema\/fields$/', 'f_fields'),
  47. array('/^dbs\/(.+)\/docs\/(.+)$/', 'f_doc'),
  48. array('/^dbs\/(.+)\/docs$/', 'f_docs'),
  49. array('/^dbs\/(.+)$/', 'f_db'),
  50. array('/^dbs$/', 'f_dbs'),
  51. array('/^$/', 'f_root')
  52. );
  53. }
  54. function match_path($path) {
  55. foreach ($this->patterns as $p) {
  56. $groups = array();
  57. if (preg_match($p[0], $path, $groups)) {
  58. return array($p[1], $groups);
  59. }
  60. }
  61. return null;
  62. }
  63. function do_get($path, $params=null) {
  64. $groups = null;
  65. $m = $this->match_path($path);
  66. if ($m) {
  67. return $this->$m[0]('GET', $m[1], $params, null);
  68. }
  69. return array(404, 'Path not found');
  70. }
  71. function do_post($path, $data='') {
  72. $groups = null;
  73. $m = $this->match_path($path);
  74. if ($m) {
  75. return $this->$m[0]('POST', $m[1], null, $data);
  76. }
  77. return array(404, 'Path not found');
  78. }
  79. function do_put($path, $data='') {
  80. $groups = null;
  81. $m = $this->match_path($path);
  82. if ($m) {
  83. return $this->$m[0]('PUT', $m[1], null, $data);
  84. }
  85. return array(404, 'Path not found');
  86. }
  87. function do_delete($path) {
  88. $groups = null;
  89. $m = $this->match_path($path);
  90. if ($m) {
  91. return $this->$m[0]('DELETE', $m[1], null, null);
  92. }
  93. return array(404, 'Path not found');
  94. }
  95. //////////////////////////////////////////////////////////////////
  96. function f_root($method, $pathparams, $queryparams, $bodydata) {
  97. if ($method == 'GET') {
  98. return array(200, 'Flax Search Service (Test RestClient)');
  99. }
  100. return array(404, 'Path not found');
  101. }
  102. function f_dbs($method, $pathparams, $queryparams, $bodydata) {
  103. if ($method == 'GET') {
  104. return array_keys($this->dbs);
  105. }
  106. return array(404, 'Path not found');
  107. }
  108. function f_db($method, $pathparams, $queryparams, $bodydata) {
  109. $dbname = $pathparams[1];
  110. if ($method == 'GET') {
  111. if (array_key_exists($dbname, $this->dbs)) {
  112. return array(200, $this->dbs[$dbname]->as_array());
  113. }
  114. }
  115. else if ($method == 'POST') {
  116. $this->dbs[$dbname] = new _MockDatabase();
  117. return array(201, 'Database created');
  118. }
  119. else if ($method == 'DELETE') {
  120. unset($this->dbs[$dbname]);
  121. return array(200, 'Database deleted');
  122. }
  123. return array(404, 'Path not found');
  124. }
  125. function f_fields($method, $pathparams, $queryparams, $bodydata) {
  126. $dbname = $pathparams[1];
  127. if ($method == 'GET') {
  128. if (array_key_exists($dbname, $this->dbs)) {
  129. return array(200, array_keys($this->dbs[$dbname]->fields));
  130. }
  131. }
  132. return array(404, 'Path not found');
  133. }
  134. function f_field($method, $pathparams, $queryparams, $bodydata) {
  135. $dbname = $pathparams[1];
  136. $fieldname = $pathparams[2];
  137. if (array_key_exists($dbname, $this->dbs)) {
  138. $db = $this->dbs[$dbname];
  139. if ($method == 'GET') {
  140. if (array_key_exists($fieldname, $db->fields)) {
  141. return array(200, $db->fields[$fieldname]);
  142. }
  143. }
  144. else if ($method == 'POST') {
  145. if (array_key_exists($fieldname, $db->fields)) {
  146. return array(409, 'Field exists');
  147. }
  148. $db->fields[$fieldname] = $bodydata;
  149. return array(201, 'Field created');
  150. }
  151. else if ($method == 'PUT') {
  152. $db->fields[$fieldname] = $bodydata;
  153. return array(201, 'Field created');
  154. }
  155. else if ($method == 'DELETE') {
  156. if (array_key_exists($fieldname, $db->fields)) {
  157. unset($db->fields[$fieldname]);
  158. return array(200, 'Field deleted');
  159. }
  160. }
  161. }
  162. return array(404, 'Path not found');
  163. }
  164. function f_doc($method, $pathparams, $queryparams, $bodydata) {
  165. $dbname = $pathparams[1];
  166. $docid = $pathparams[2];
  167. if (array_key_exists($dbname, $this->dbs)) {
  168. $db = $this->dbs[$dbname];
  169. if ($method == 'GET') {
  170. if (array_key_exists($docid, $db->docs)) {
  171. return array(200, $db->docs[$docid]);
  172. }
  173. }
  174. else if ($method == 'PUT') {
  175. $db->docs[$docid] = $bodydata;
  176. return array(201, $docid);
  177. }
  178. else if ($method == 'POST') {
  179. if (array_key_exists($docid, $db->docs)) {
  180. return array(409, 'Doc ID exists');
  181. }
  182. $db->docs[$docid] = $bodydata;
  183. return array(201, $docid);
  184. }
  185. }
  186. return array(404, 'Path not found');
  187. }
  188. function f_docs($method, $pathparams, $queryparams, $bodydata) {
  189. $dbname = $pathparams[1];
  190. if (array_key_exists($dbname, $this->dbs)) {
  191. $db = $this->dbs[$dbname];
  192. if ($method == 'POST') {
  193. $docid = uniqid();
  194. $db->docs[$docid] = $bodydata;
  195. return array(201, $docid);
  196. }
  197. }
  198. return array(404, 'Path not found');
  199. }
  200. }
  201. ?>