Преглед на файлове

ZF-7756: mocking of php://input (thanks to L. Strojny)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17958 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew преди 16 години
родител
ревизия
0233f51262
променени са 2 файла, в които са добавени 90 реда и са изтрити 1 реда
  1. 81 0
      tests/Zend/AllTests/StreamWrapper/PhpInput.php
  2. 9 1
      tests/Zend/Controller/Request/HttpTest.php

+ 81 - 0
tests/Zend/AllTests/StreamWrapper/PhpInput.php

@@ -0,0 +1,81 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * Class for mocking php://input
+ *
+ * To use: 
+ * <code>
+ * Zend_AllTests_StreamWrapper_PhpInput::mockInput($string);
+ * $value = file_get_contents('php://input');
+ * </code>
+ *
+ * Once done, call stream_wrapper_restore('php') to restore the original behavior.
+ *
+ * @category   Zend
+ * @package    Zend
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_AllTests_StreamWrapper_PhpInput
+{
+    protected static $_data;
+
+    protected $_position = 0;
+
+    public static function mockInput($data)
+    {
+        stream_wrapper_unregister('php');
+        stream_wrapper_register('php', 'Zend_AllTests_StreamWrapper_PhpInput');
+        self::$_data = $data;
+    }
+
+    public function stream_open()
+    {
+        return true;
+    }
+
+    public function stream_eof()
+    {
+        return (0 == strlen(self::$_data));
+    }
+
+    public function stream_read($count)
+    {
+        // To match the behavior of php://input, we need to clear out the data 
+        // as it is read
+        if ($count > strlen(self::$_data)) {
+            $data = self::$_data;
+            self::$_data = '';
+        } else {
+            $data = substr(self::$_data, 0, $count);
+            self::$_data = substr(self::$_data, $count);
+        }
+        return $data;
+    }
+
+    public function stream_stat()
+    {
+        return array();
+    }
+}

+ 9 - 1
tests/Zend/Controller/Request/HttpTest.php

@@ -319,7 +319,10 @@ class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
 
     public function testGetRawBodyReturnsFalseWithNoPost()
     {
+        require_once 'Zend/AllTests/StreamWrapper/PhpInput.php';
+        Zend_AllTests_StreamWrapper_PhpInput::mockInput('');
         $this->assertFalse($this->_request->getRawBody());
+        stream_wrapper_restore('php');
     }
 
     public function testGetQuery()
@@ -762,7 +765,12 @@ class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
      */
     public function testCallingGetRawBodyMultipleTimesShouldReturnSameValue()
     {
-        $this->markTestSkipped('Impossible to populate php://input to test this');
+        require_once 'Zend/AllTests/StreamWrapper/PhpInput.php';
+        Zend_AllTests_StreamWrapper_PhpInput::mockInput('foobar');
+        $request = new Zend_Controller_Request_Http();
+        $first = $request->getRawBody();
+        $this->assertSame($first, $request->getRawBody());
+        stream_wrapper_restore('php');
     }
 }