You are here

public function StreamWrapperTest::testFileFunctions in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php \Drupal\KernelTests\Core\File\StreamWrapperTest::testFileFunctions()
  2. 9 core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php \Drupal\KernelTests\Core\File\StreamWrapperTest::testFileFunctions()

Tests some file handle functions.

File

core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php, line 111

Class

StreamWrapperTest
Tests stream wrapper functions.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileFunctions() {
  $filename = 'public://' . $this
    ->randomMachineName();
  file_put_contents($filename, str_repeat('d', 1000));

  // Open for rw and place pointer at beginning of file so select will return.
  $handle = fopen($filename, 'c+');
  $this
    ->assertNotFalse($handle, 'Able to open a file for appending, reading and writing.');

  // Attempt to change options on the file stream: should all fail.
  $this
    ->assertFalse(@stream_set_blocking($handle, 0), 'Unable to set to non blocking using a local stream wrapper.');
  $this
    ->assertFalse(@stream_set_blocking($handle, 1), 'Unable to set to blocking using a local stream wrapper.');
  $this
    ->assertFalse(@stream_set_timeout($handle, 1), 'Unable to set read time out using a local stream wrapper.');
  $this
    ->assertEquals(-1, @stream_set_write_buffer($handle, 512), 'Unable to set write buffer using a local stream wrapper.');

  // This will test stream_cast().
  $read = [
    $handle,
  ];
  $write = NULL;
  $except = NULL;
  $this
    ->assertEquals(1, stream_select($read, $write, $except, 0), 'Able to cast a stream via stream_select.');

  // This will test stream_truncate().
  $this
    ->assertEquals(1, ftruncate($handle, 0), 'Able to truncate a stream via ftruncate().');
  fclose($handle);
  $this
    ->assertEquals(0, filesize($filename), 'Able to truncate a stream.');

  // Cleanup.
  unlink($filename);
}