WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/20/2021 03:01:55 AM (3 months ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/class-wp-test-stream.php

    r49545 r50771  
    1212 * (note the trailing slash).
    1313 *
    14  * This class can be used to test that code works with basic read/write streams,
    15  * as such, operations such as seeking are not supported.
     14 * This class can be used to test that code works with basic read/write streams.
    1615 *
    1716 * This class does not register itself as a stream handler: test fixtures
     
    2524     * In-memory storage for files and directories simulated by this wrapper.
    2625     */
    27     static $data = array();
    28 
    29     var $position;
    30     var $file;
    31     var $bucket;
    32     var $data_ref;
     26    public static $data = array();
     27
     28    public $position;
     29    public $file;
     30    public $bucket;
     31    public $data_ref;
    3332
    3433    /**
     
    6766     * @see streamWrapper::stream_open
    6867     */
    69     function stream_open( $path, $mode, $options, &$opened_path ) {
     68    public function stream_open( $path, $mode, $options, &$opened_path ) {
    7069        $this->open( $path );
    7170        return true;
     
    7776     * @see streamWrapper::stream_read
    7877     */
    79     function stream_read( $count ) {
     78    public function stream_read( $count ) {
    8079        if ( ! isset( $this->data_ref ) ) {
    8180            return '';
     
    9392     * @see streamWrapper::stream_write
    9493     */
    95     function stream_write( $data ) {
     94    public function stream_write( $data ) {
    9695        if ( ! isset( $this->data_ref ) ) {
    9796            $this->data_ref = '';
     
    108107
    109108    /**
     109     * Seeks to specific location in a stream.
     110     *
     111     * @see streamWrapper::stream_seek
     112     *
     113     * @param int $offset The stream offset to seek to.
     114     * @param int $whence Optional. Seek position.
     115     * @return bool Returns true when position is updated, else false.
     116     */
     117    public function stream_seek( $offset, $whence = SEEK_SET ) {
     118        if ( empty( $this->data_ref ) ) {
     119            return false;
     120        }
     121
     122        $new_offset = $this->position;
     123        switch ( $whence ) {
     124            case SEEK_CUR:
     125                $new_offset += $offset;
     126                break;
     127
     128            case SEEK_END:
     129                $new_offset = strlen( $this->data_ref ) + $offset;
     130                break;
     131
     132            case SEEK_SET:
     133                $new_offset = $offset;
     134                break;
     135
     136            default:
     137                return false;
     138        }
     139
     140        if ( $new_offset < 0 ) {
     141            return false;
     142        }
     143
     144        // Save the new position.
     145        $this->position = $new_offset;
     146
     147        return true;
     148    }
     149
     150    /**
    110151     * Retrieves the current position of a stream.
    111152     *
    112153     * @see streamWrapper::stream_tell
    113154     */
    114     function stream_tell() {
     155    public function stream_tell() {
    115156        return $this->position;
    116157    }
     
    121162     * @see streamWrapper::stream_eof
    122163     */
    123     function stream_eof() {
     164    public function stream_eof() {
    124165        if ( ! isset( $this->data_ref ) ) {
    125166            return true;
     
    134175     * @see streamWrapper::stream_metadata
    135176     */
    136     function stream_metadata( $path, $option, $var ) {
     177    public function stream_metadata( $path, $option, $var ) {
    137178        $this->open( $path );
    138179        if ( STREAM_META_TOUCH === $option ) {
     
    150191     * @see streamWrapper::mkdir
    151192     */
    152     function mkdir( $path, $mode, $options ) {
     193    public function mkdir( $path, $mode, $options ) {
    153194        $this->open( $path );
    154195        $plainfile = rtrim( $this->file, '/' );
Note: See TracChangeset for help on using the changeset viewer.