Build/Test Tools: Add seeking support to stream test library.

Introduces seeking to the `WP_Test_Stream` stream wrapper. This allows the testing of `wp_getimagesize()` and `wp_read_image_metadata()` among others. Includes tests for the latter.

Props hellofromTonya.
Fixes #52922.
See #52826.


git-svn-id: https://develop.svn.wordpress.org/trunk@50771 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2021-04-20 03:01:55 +00:00
parent c072116d6a
commit 38652cf33e
2 changed files with 144 additions and 14 deletions

View File

@@ -11,8 +11,7 @@
* 'DIRECTORY' to the static variable WP_Test_Stream::$data['bucket']['/foo/']
* (note the trailing slash).
*
* This class can be used to test that code works with basic read/write streams,
* as such, operations such as seeking are not supported.
* This class can be used to test that code works with basic read/write streams.
*
* This class does not register itself as a stream handler: test fixtures
* should make the appropriate call to stream_wrapper_register().
@@ -24,12 +23,12 @@ class WP_Test_Stream {
/**
* In-memory storage for files and directories simulated by this wrapper.
*/
static $data = array();
public static $data = array();
var $position;
var $file;
var $bucket;
var $data_ref;
public $position;
public $file;
public $bucket;
public $data_ref;
/**
* Initializes internal state for reading the given URL.
@@ -66,7 +65,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::stream_open
*/
function stream_open( $path, $mode, $options, &$opened_path ) {
public function stream_open( $path, $mode, $options, &$opened_path ) {
$this->open( $path );
return true;
}
@@ -76,7 +75,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::stream_read
*/
function stream_read( $count ) {
public function stream_read( $count ) {
if ( ! isset( $this->data_ref ) ) {
return '';
}
@@ -92,7 +91,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::stream_write
*/
function stream_write( $data ) {
public function stream_write( $data ) {
if ( ! isset( $this->data_ref ) ) {
$this->data_ref = '';
}
@@ -106,12 +105,54 @@ class WP_Test_Stream {
return strlen( $data );
}
/**
* Seeks to specific location in a stream.
*
* @see streamWrapper::stream_seek
*
* @param int $offset The stream offset to seek to.
* @param int $whence Optional. Seek position.
* @return bool Returns true when position is updated, else false.
*/
public function stream_seek( $offset, $whence = SEEK_SET ) {
if ( empty( $this->data_ref ) ) {
return false;
}
$new_offset = $this->position;
switch ( $whence ) {
case SEEK_CUR:
$new_offset += $offset;
break;
case SEEK_END:
$new_offset = strlen( $this->data_ref ) + $offset;
break;
case SEEK_SET:
$new_offset = $offset;
break;
default:
return false;
}
if ( $new_offset < 0 ) {
return false;
}
// Save the new position.
$this->position = $new_offset;
return true;
}
/**
* Retrieves the current position of a stream.
*
* @see streamWrapper::stream_tell
*/
function stream_tell() {
public function stream_tell() {
return $this->position;
}
@@ -120,7 +161,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::stream_eof
*/
function stream_eof() {
public function stream_eof() {
if ( ! isset( $this->data_ref ) ) {
return true;
}
@@ -133,7 +174,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::stream_metadata
*/
function stream_metadata( $path, $option, $var ) {
public function stream_metadata( $path, $option, $var ) {
$this->open( $path );
if ( STREAM_META_TOUCH === $option ) {
if ( ! isset( $this->data_ref ) ) {
@@ -149,7 +190,7 @@ class WP_Test_Stream {
*
* @see streamWrapper::mkdir
*/
function mkdir( $path, $mode, $options ) {
public function mkdir( $path, $mode, $options ) {
$this->open( $path );
$plainfile = rtrim( $this->file, '/' );