You are here

class HttpStreamWrapperTest in Remote Stream Wrapper 8

Same name in this branch
  1. 8 tests/src/Unit/HttpStreamWrapperTest.php \Drupal\Tests\remote_stream_wrapper\Unit\HttpStreamWrapperTest
  2. 8 tests/src/Kernel/HttpStreamWrapperTest.php \Drupal\Tests\remote_stream_wrapper\Kernel\HttpStreamWrapperTest

@group remote_stream_wrapper @coversDefaultClass \Drupal\remote_stream_wrapper\StreamWrapper\HttpStreamWrapper

Hierarchy

Expanded class hierarchy of HttpStreamWrapperTest

File

tests/src/Unit/HttpStreamWrapperTest.php, line 14

Namespace

Drupal\Tests\remote_stream_wrapper\Unit
View source
class HttpStreamWrapperTest extends UnitTestCase {

  /**
   * Test that the wrapper constants.
   *
   * @covers ::getType
   * @covers ::getName
   * @covers ::getDescription
   */
  public function testStreamConfiguration() {
    $type = HttpStreamWrapper::getType();
    $this
      ->assertEquals(StreamWrapperInterface::READ & StreamWrapperInterface::HIDDEN, $type);
    $this
      ->assertEquals($type & StreamWrapperInterface::LOCAL, 0);
    $this
      ->assertNotEquals($type & StreamWrapperInterface::READ, 0);
    $this
      ->assertEquals($type & StreamWrapperInterface::WRITE, 0);
    $this
      ->assertEquals($type & StreamWrapperInterface::VISIBLE, 0);
    $this
      ->assertNotEquals($type & StreamWrapperInterface::HIDDEN, 0);

    //$this->assertEquals($type & StreamWrapperInterface::LOCAL_HIDDEN, 0);

    //$this->assertEquals($type & StreamWrapperInterface::WRITE_VISIBLE, 0);
    $this
      ->assertNotEquals($type & StreamWrapperInterface::READ_VISIBLE, 0);

    //$this->assertEquals($type & StreamWrapperInterface::NORMAL, 0);

    //$this->assertEquals($type & StreamWrapperInterface::LOCAL_NORMAL, 0);
    $wrapper = new HttpStreamWrapper(new Client());
    $this
      ->assertInternalType('string', $wrapper
      ->getName());
    $this
      ->assertInternalType('string', $wrapper
      ->getDescription());
  }

  /**
   * Test URI methods.
   *
   * @covers ::setUri
   * @covers ::getUri
   * @covers ::getExternalUrl
   * @covers ::realpath
   */
  public function testUri() {
    $wrapper = new HttpStreamWrapper();
    $uri = 'http://example.com/file.txt';
    $wrapper
      ->setUri($uri);
    $this
      ->assertEquals($uri, $wrapper
      ->getUri());
    $this
      ->assertEquals($uri, $wrapper
      ->getExternalUrl());
    $this
      ->assertEquals(FALSE, $wrapper
      ->realpath());
  }

  /**
   * Test dirname().
   *
   * @covers ::dirname
   */
  public function testDirname() {
    $wrapper = new HttpStreamWrapper();

    // Test dirname() with no parameters.
    $wrapper
      ->setUri('http://example.com/test.txt');
    $this
      ->assertEquals('http://example.com', $wrapper
      ->dirname());

    // Test dirname() with one directory.
    $wrapper
      ->setUri('http://example.com/directory/test.txt');
    $this
      ->assertEquals('http://example.com/directory', $wrapper
      ->dirname());

    // Test dirname() with two directories and a $uri parameter.
    $this
      ->assertEquals('http://example.com/directory/directory2', $wrapper
      ->dirname('http://example.com/directory/directory2/test.txt'));

    // Test referencing self with a dot.
    $this
      ->assertEquals('http://', $wrapper
      ->dirname('http://.'));
  }

  /**
   * Test that we always return TRUE for locks.
   *
   * @covers ::stream_lock
   */
  public function testStreamLock() {
    $wrapper = new HttpStreamWrapper();
    $wrapper
      ->setUri('http://example.com/test.txt');
    foreach ([
      LOCK_SH,
      LOCK_EX,
      LOCK_UN,
      LOCK_NB,
    ] as $type) {
      $this
        ->assertTrue($wrapper
        ->stream_lock($type));
    }
  }

  /**
   * Test that the timeout is set properly in configuration.
   *
   * @covers ::stream_set_option
   */
  public function testStreamSetOption() {
    $wrapper = new HttpStreamWrapper();
    $result = $wrapper
      ->stream_set_option(STREAM_OPTION_READ_TIMEOUT, 30, 50);
    $this
      ->assertTrue($result);
    $config = $wrapper
      ->getHttpConfig();
    $this
      ->assertEquals($config['timeout'], 30.00005);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpStreamWrapperTest::testDirname public function Test dirname().
HttpStreamWrapperTest::testStreamConfiguration public function Test that the wrapper constants.
HttpStreamWrapperTest::testStreamLock public function Test that we always return TRUE for locks.
HttpStreamWrapperTest::testStreamSetOption public function Test that the timeout is set properly in configuration.
HttpStreamWrapperTest::testUri public function Test URI methods.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340