You are here

public function ExtensionStreamTest::testStreamWrapperMethods in System stream wrapper 8

Test the extension stream wrapper methods.

@dataProvider providerStreamWrapperMethods

Parameters

string $uri: The uri to be tested.

string $dirname: The expectation for dirname() method.

string $realpath: The expectation for realpath() method.

string|\RuntimeException|\InvalidArgumentException $getExternalUrl: The expectation for getExternalUrl() method.

File

tests/src/Kernel/File/ExtensionStreamTest.php, line 113

Class

ExtensionStreamTest
Tests system stream wrapper functions.

Namespace

Drupal\Tests\system_stream_wrapper\Kernel\File

Code

public function testStreamWrapperMethods($uri, $dirname, $realpath, $getExternalUrl) {

  // Set 'minimal' as installed profile for the purposes of this test.
  $this
    ->setInstallProfile('minimal');
  $this
    ->enableModules([
    'minimal',
  ]);

  // Prefix realpath() expected value with Drupal root directory.
  $realpath = strpos($realpath, 'or is not') === FALSE ? DRUPAL_ROOT . $realpath : $realpath;

  // Prefix getExternalUrl() expected value with base url.
  $getExternalUrl = strpos($getExternalUrl, 'or is not') === FALSE ? "{$this->baseUrl}{$getExternalUrl}" : $getExternalUrl;
  $case = compact('dirname', 'realpath', 'getExternalUrl');
  foreach ($case as $method => $expected) {
    list($scheme, ) = explode('://', $uri);
    $this->streamWrappers[$scheme]
      ->setUri($uri);
    if (strpos($expected, 'or is not') !== FALSE) {

      /** @var \Exception $expected */
      $message = sprintf('Exception thrown: \\InvalidArgumentException("%s").', $expected);
      try {
        $this->streamWrappers[$scheme]
          ->{$method}();
        $this
          ->fail($message);
      } catch (\InvalidArgumentException $e) {
        $this
          ->assertSame($expected, $e
          ->getMessage(), $message);
      } catch (\RuntimeException $e) {
        $this
          ->assertSame($expected, $e
          ->getMessage(), $message);
      }
    }
    elseif (is_string($expected)) {
      $this
        ->assertSame($expected, $this->streamWrappers[$scheme]
        ->{$method}(), sprintf("%s failed", $method));
    }
  }
}