HttpStreamWrapperTest.php in Remote Stream Wrapper 8        
                          
                  
                        
  
  
  
  
File
  tests/src/Unit/HttpStreamWrapperTest.php
  
    View source  
  <?php
namespace Drupal\Tests\remote_stream_wrapper\Unit;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\remote_stream_wrapper\StreamWrapper\HttpStreamWrapper;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\Client;
class HttpStreamWrapperTest extends UnitTestCase {
  
  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
      ->assertNotEquals($type & StreamWrapperInterface::READ_VISIBLE, 0);
    
    
    $wrapper = new HttpStreamWrapper(new Client());
    $this
      ->assertInternalType('string', $wrapper
      ->getName());
    $this
      ->assertInternalType('string', $wrapper
      ->getDescription());
  }
  
  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());
  }
  
  public function testDirname() {
    $wrapper = new HttpStreamWrapper();
    
    $wrapper
      ->setUri('http://example.com/test.txt');
    $this
      ->assertEquals('http://example.com', $wrapper
      ->dirname());
    
    $wrapper
      ->setUri('http://example.com/directory/test.txt');
    $this
      ->assertEquals('http://example.com/directory', $wrapper
      ->dirname());
    
    $this
      ->assertEquals('http://example.com/directory/directory2', $wrapper
      ->dirname('http://example.com/directory/directory2/test.txt'));
    
    $this
      ->assertEquals('http://', $wrapper
      ->dirname('http://.'));
  }
  
  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));
    }
  }
  
  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);
  }
}