PhpInput.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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-2015 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. * <code>
  26. * class ...
  27. * {
  28. * public function setUp()
  29. * {
  30. * Zend_AllTests_StreamWrapper_PhpInput::mockInput('expected string');
  31. * }
  32. *
  33. * public function testReadingFromPhpInput()
  34. * {
  35. * $this->assertSame('expected string', file_get_contents('php://input'));
  36. * $this->assertSame('php://input', Zend_AllTests_StreamWrapper_PhpInput::getCurrentPath());
  37. * }
  38. *
  39. * public function tearDown()
  40. * {
  41. * Zend_AllTests_StreamWrapper_PhpInput::restoreDefault();
  42. * }
  43. * }
  44. *
  45. * @category Zend
  46. * @package Zend
  47. * @subpackage UnitTests
  48. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. class Zend_AllTests_StreamWrapper_PhpInput
  52. {
  53. protected static $_data;
  54. protected static $_returnValues = array();
  55. protected static $_arguments = array();
  56. protected $_position = 0;
  57. public static function mockInput($data)
  58. {
  59. stream_wrapper_unregister('php');
  60. stream_wrapper_register('php', 'Zend_AllTests_StreamWrapper_PhpInput');
  61. self::$_data = $data;
  62. }
  63. public static function restoreDefault()
  64. {
  65. // Reset static values
  66. self::$_returnValues = array();
  67. self::$_arguments = array();
  68. // Restore original stream wrapper
  69. stream_wrapper_restore('php');
  70. }
  71. public static function methodWillReturn($methodName, $returnValue)
  72. {
  73. $methodName = strtolower($methodName);
  74. self::$_returnValues[$methodName] = $returnValue;
  75. }
  76. public static function argumentsPassedTo($methodName)
  77. {
  78. $methodName = strtolower($methodName);
  79. if (isset(self::$_arguments[$methodName])) {
  80. return self::$_arguments[$methodName];
  81. }
  82. return null;
  83. }
  84. public function stream_open()
  85. {
  86. self::$_arguments[__FUNCTION__] = func_get_args();
  87. if (array_key_exists(__FUNCTION__, self::$_returnValues)) {
  88. return self::$_returnValues[__FUNCTION__];
  89. }
  90. return true;
  91. }
  92. public function stream_eof()
  93. {
  94. self::$_arguments[__FUNCTION__] = func_get_args();
  95. if (array_key_exists(__FUNCTION__, self::$_returnValues)) {
  96. return self::$_returnValues[__FUNCTION__];
  97. }
  98. return (0 == strlen(self::$_data));
  99. }
  100. public function stream_read($count)
  101. {
  102. self::$_arguments[__FUNCTION__] = func_get_args();
  103. if (array_key_exists(__FUNCTION__, self::$_returnValues)) {
  104. return self::$_returnValues[__FUNCTION__];
  105. }
  106. // To match the behavior of php://input, we need to clear out the data
  107. // as it is read
  108. if ($count > strlen(self::$_data)) {
  109. $data = self::$_data;
  110. self::$_data = '';
  111. } else {
  112. $data = substr(self::$_data, 0, $count);
  113. self::$_data = substr(self::$_data, $count);
  114. }
  115. return $data;
  116. }
  117. public function stream_stat()
  118. {
  119. self::$_arguments[__FUNCTION__] = func_get_args();
  120. if (array_key_exists(__FUNCTION__, self::$_returnValues)) {
  121. return self::$_returnValues[__FUNCTION__];
  122. }
  123. return array();
  124. }
  125. }