MockLibraryService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\mocks\console\command;
  9. use lithium\core\Libraries;
  10. use lithium\net\http\Response;
  11. class MockLibraryService extends \lithium\net\http\Service {
  12. public function send($method, $path = null, $data = array(), array $options = array()) {
  13. if ($this->_config['host'] === 'localhost') {
  14. return null;
  15. }
  16. if ($method === 'post') {
  17. $this->request = $this->_request($method, $path, $data, $options);
  18. if (!empty($this->request->username)) {
  19. $user = array(
  20. 'method' => 'Basic', 'username' => 'gwoo', 'password' => 'password'
  21. );
  22. if ($this->request->username !== $user['username']) {
  23. $this->last = (object) array('response' => new Response());
  24. $this->last->response->status(401);
  25. return json_encode(array(
  26. 'error' => 'Invalid username/password.'
  27. ));
  28. }
  29. }
  30. $this->last = (object) array('response' => new Response());
  31. $this->last->response->status(201);
  32. return json_encode($this->_data('plugins', 1));
  33. }
  34. if ($path === 'lab/plugins') {
  35. return json_encode($this->_data('plugins'));
  36. }
  37. if ($path === 'lab/extensions') {
  38. return json_encode($this->_data('extensions'));
  39. }
  40. if (preg_match("/lab\/plugins/", $path, $match)) {
  41. return json_encode($this->_data('plugins'));
  42. }
  43. if (preg_match("/lab\/extensions/", $path, $match)) {
  44. return json_encode($this->_data('extensions'));
  45. }
  46. if (preg_match("/lab\/li3_lab.json/", $path, $match)) {
  47. return json_encode($this->_data('plugins', 0));
  48. }
  49. if (preg_match("/lab\/library_test_plugin.json/", $path, $match)) {
  50. return json_encode($this->_data('plugins', 1));
  51. }
  52. if (preg_match("/lab\/li3_docs.json/", $path, $match)) {
  53. return json_encode($this->_data('plugins', 2));
  54. }
  55. }
  56. protected function _data($type, $key = null) {
  57. $resources = Libraries::get(true, 'resources');
  58. $plugins = array(
  59. array(
  60. 'name' => 'li3_lab', 'version' => '1.0',
  61. 'summary' => 'the li3 plugin client/server',
  62. 'maintainers' => array(
  63. array(
  64. 'name' => 'gwoo', 'email' => '[email protected]',
  65. 'website' => 'li3.rad-dev.org'
  66. )
  67. ),
  68. 'created' => '2009-11-30', 'updated' => '2009-11-30',
  69. 'rating' => '9.9', 'downloads' => '1000',
  70. 'sources' => array(
  71. 'git' => 'git://rad-dev.org/li3_lab.git',
  72. 'phar' => 'http://downloads.rad-dev.org/li3_lab.phar.gz'
  73. ),
  74. 'requires' => array()
  75. ),
  76. array(
  77. 'id' => 'b22a2f0dfc873fd0e1a7655f4895872ae4b94ef4',
  78. 'name' => 'library_test_plugin', 'version' => '1.0',
  79. 'summary' => 'an li3 plugin example',
  80. 'maintainers' => array(
  81. array(
  82. 'name' => 'gwoo', 'email' => '[email protected]',
  83. 'website' => 'li3.rad-dev.org'
  84. )
  85. ),
  86. 'created' => '2009-11-30', 'updated' => '2009-11-30',
  87. 'rating' => '9.9', 'downloads' => '1000',
  88. 'sources' => array(
  89. 'phar' => "{$resources}/tmp/tests/library_test_plugin.phar.gz"
  90. ),
  91. 'requires' => array(
  92. 'li3_lab' => array('version' => '<=1.0')
  93. )
  94. ),
  95. array(
  96. 'name' => 'li3_docs', 'version' => '1.0',
  97. 'summary' => 'the li3 plugin client/server',
  98. 'maintainers' => array(
  99. array(
  100. 'name' => 'gwoo', 'email' => '[email protected]',
  101. 'website' => 'li3.rad-dev.org'
  102. )
  103. ),
  104. 'created' => '2009-11-30', 'updated' => '2009-11-30',
  105. 'rating' => '9.9', 'downloads' => '1000',
  106. 'sources' => array(
  107. 'git' => 'git://rad-dev.org/li3_docs.git',
  108. 'phar' => 'http://downloads.rad-dev.org/li3_docs.phar.gz'
  109. ),
  110. 'requires' => array()
  111. )
  112. );
  113. $extensions = array(
  114. array(
  115. 'class' => 'Example', 'namespace' => 'app\extensions\adapter\cache',
  116. 'summary' => 'the example adapter',
  117. 'maintainers' => array(
  118. array(
  119. 'name' => 'gwoo', 'email' => '[email protected]',
  120. 'website' => 'li3.rad-dev.org'
  121. )
  122. ),
  123. 'created' => '2009-11-30', 'updated' => '2009-11-30',
  124. 'rating' => '9.9', 'downloads' => '1000'
  125. ),
  126. array(
  127. 'class' => 'Paginator', 'namespace' => 'app\extensions\helpes',
  128. 'summary' => 'a paginator helper',
  129. 'maintainers' => array(
  130. array(
  131. 'name' => 'gwoo', 'email' => '[email protected]',
  132. 'website' => 'li3.rad-dev.org'
  133. )
  134. ),
  135. 'created' => '2009-11-30', 'updated' => '2009-11-30',
  136. 'rating' => '9.9', 'downloads' => '1000'
  137. )
  138. );
  139. $data = compact('plugins', 'extensions');
  140. if (isset($data[$type][$key])) {
  141. return $data[$type][$key];
  142. }
  143. if (isset($data[$type])) {
  144. return $data[$type];
  145. }
  146. if ($key !== null) {
  147. return null;
  148. }
  149. return $data;
  150. }
  151. }
  152. ?>