You are here

function ResourceStreamTestCase::testFread in D7 Media 6

File

resource/tests/ResourceStream.test, line 64
This provides SimpleTests for the core stream wrapper functionality.

Class

ResourceStreamTestCase
Base class for file tests that adds some additional file specific assertions and helper functions.

Code

function testFread() {
  $data = fread($this->handle, 4);
  $this
    ->assertTrue($data == '1234', 'fread 4 bytes, start pos 0');
  $pos = ftell($this->handle);
  $this
    ->assertTrue($pos == 4, "ftell at pos 4({$pos})");
  $data = fread($this->handle, 4);
  $this
    ->assertTrue($data == '5678', 'fread 4 bytes, start pos 4');

  // There is apparently a bug with either ftell or fseek working here.
  // ftell should return the currently position. Seek is obviously
  // moving the file pointer or the reads wouldn't match. Either fseek
  // isn't updating the pos read by ftell, or ftell is actually returning
  // bytes read.
  $this
    ->assertTrue(fseek($this->handle, 8), 'fseek to pos 0');
  $pos = ftell($this->handle);
  $this
    ->assertTrue($pos == 8, "ftell at pos 8({$pos})");
  $data = fread($this->handle, 4);
  $this
    ->assertTrue($data == '90ab', "fread 4 bytes, start pos 4({$data})");
  $this
    ->assertFalse(feof($this->handle), 'feof at pos 4');
  $data = fgets($this->handle);
  $this
    ->assertTrue($data == 'cdef', 'fgets pos 12 - 15');
  $this
    ->assertTrue(feof($this->handle), 'feof at end of file');
  $this
    ->assertTrue(fseek($this->handle, 4), 'fseek to pos 4');
  $this
    ->assertTrue(fclose($this->handle), "Close file.");
}