PhpInput.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Class for mocking php://input
  24. *
  25. * To use:
  26. * <code>
  27. * Zend_AllTests_StreamWrapper_PhpInput::mockInput($string);
  28. * $value = file_get_contents('php://input');
  29. * </code>
  30. *
  31. * Once done, call stream_wrapper_restore('php') to restore the original behavior.
  32. *
  33. * @category Zend
  34. * @package Zend
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_AllTests_StreamWrapper_PhpInput
  40. {
  41. protected static $_data;
  42. protected $_position = 0;
  43. public static function mockInput($data)
  44. {
  45. stream_wrapper_unregister('php');
  46. stream_wrapper_register('php', 'Zend_AllTests_StreamWrapper_PhpInput');
  47. self::$_data = $data;
  48. }
  49. public function stream_open()
  50. {
  51. return true;
  52. }
  53. public function stream_eof()
  54. {
  55. return (0 == strlen(self::$_data));
  56. }
  57. public function stream_read($count)
  58. {
  59. // To match the behavior of php://input, we need to clear out the data
  60. // as it is read
  61. if ($count > strlen(self::$_data)) {
  62. $data = self::$_data;
  63. self::$_data = '';
  64. } else {
  65. $data = substr(self::$_data, 0, $count);
  66. self::$_data = substr(self::$_data, $count);
  67. }
  68. return $data;
  69. }
  70. public function stream_stat()
  71. {
  72. return array();
  73. }
  74. }