You are here

class DrupalCacheAdapterTest in Flysystem 3.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Flysystem/Adapter/DrupalCacheAdapterTest.php \Drupal\Tests\flysystem\Unit\Flysystem\Adapter\DrupalCacheAdapterTest
  2. 2.0.x tests/src/Unit/Flysystem/Adapter/DrupalCacheAdapterTest.php \Drupal\Tests\flysystem\Unit\Flysystem\Adapter\DrupalCacheAdapterTest
  3. 3.0.x tests/src/Unit/Flysystem/Adapter/DrupalCacheAdapterTest.php \Drupal\Tests\flysystem\Unit\Flysystem\Adapter\DrupalCacheAdapterTest

Test the Drupal Cache Adapter.

@group flysystem

@coversDefaultClass \Drupal\flysystem\Flysystem\Adapter\DrupalCacheAdapter @covers \Drupal\flysystem\Flysystem\Adapter\DrupalCacheAdapter

Hierarchy

Expanded class hierarchy of DrupalCacheAdapterTest

File

tests/src/Unit/Flysystem/Adapter/DrupalCacheAdapterTest.php, line 20

Namespace

Drupal\Tests\flysystem\Unit\Flysystem\Adapter
View source
class DrupalCacheAdapterTest extends UnitTestCase {

  /**
   * URI scheme to use for testing.
   *
   * @var string
   */
  const SCHEME = 'test-scheme';

  /**
   * The main test file.
   *
   * @var string
   */
  const FILE = 'test.txt';

  /**
   * The wrapped Flysytem adaper.
   *
   * @var \League\Flysystem\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $adapter;

  /**
   * The cache adapter under test.
   *
   * @var \Drupal\flysystem\Flysystem\Adapter\DrupalCacheAdapter
   */
  protected $cacheAdapter;

  /**
   * The flysystem backend for testing.
   *
   * @var \Drupal\flysystem\Flysystem\Adapter\CacheItemBackend
   */
  protected $cacheItemBackend;

  /**
   * {@inheritdoc}
   */
  public function setup() {
    $this->cacheItemBackend = new CacheItemBackend(static::SCHEME, new MemoryBackend('foo'));
    $this->adapter = $this
      ->prophesize(AdapterInterface::class);
    $this->cacheAdapter = new DrupalCacheAdapter(static::SCHEME, $this->adapter
      ->reveal(), $this->cacheItemBackend);
  }
  public function testWriteSuccess() {
    $config = new Config();
    $this->adapter
      ->write(static::FILE, 'contents', $config)
      ->willReturn([
      'visibility' => 'public',
    ]);
    $metadata = $this->cacheAdapter
      ->write(static::FILE, 'contents', $config);
    $this
      ->assertSame('public', $metadata['visibility']);
    $this
      ->assertSame('public', $this->cacheAdapter
      ->getVisibility(static::FILE)['visibility']);
    $this
      ->assertTrue($this->cacheItemBackend
      ->has(static::FILE));
  }
  public function testWriteStreamSuccess() {
    $config = new Config();
    $stream = fopen('data:text/plain,contents', 'rb');
    $this->adapter
      ->writeStream(static::FILE, $stream, $config)
      ->willReturn([
      'timestamp' => 12345,
    ]);
    $metadata = $this->cacheAdapter
      ->writeStream(static::FILE, $stream, $config);
    $this
      ->assertSame(12345, $metadata['timestamp']);
    $this
      ->assertSame(12345, $this->cacheAdapter
      ->getTimestamp(static::FILE)['timestamp']);
    $this
      ->assertTrue($this->cacheItemBackend
      ->has(static::FILE));
  }
  public function testUpdateSuccess() {
    $config = new Config();
    $this->adapter
      ->update(static::FILE, 'contents', $config)
      ->willReturn([
      'visibility' => 'public',
    ]);
    $metadata = $this->cacheAdapter
      ->update(static::FILE, 'contents', $config);
    $this
      ->assertSame('public', $metadata['visibility']);
    $this
      ->assertSame('public', $this->cacheAdapter
      ->getVisibility(static::FILE)['visibility']);
  }
  public function testUpdateStreamSuccess() {
    $config = new Config();
    $stream = fopen('data:text/plain,contents', 'rb');
    $this->adapter
      ->updateStream(static::FILE, $stream, $config)
      ->willReturn([
      'mimetype' => 'test_mimetype',
    ]);
    $metadata = $this->cacheAdapter
      ->updateStream(static::FILE, $stream, $config);
    $this
      ->assertSame('test_mimetype', $metadata['mimetype']);
    $this
      ->assertSame('test_mimetype', $this->cacheAdapter
      ->getMimetype(static::FILE)['mimetype']);
    $this
      ->assertTrue($this->cacheItemBackend
      ->has(static::FILE));
  }
  public function testRenameSuccess() {
    $config = new Config();
    $this->adapter
      ->write(static::FILE, 'contents', $config)
      ->willReturn([
      'size' => 1234,
    ]);
    $this->cacheAdapter
      ->write(static::FILE, 'contents', $config);
    $this->adapter
      ->rename(static::FILE, 'new.txt')
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->rename(static::FILE, 'new.txt'));
    $this
      ->assertSame(1234, $this->cacheAdapter
      ->getSize('new.txt')['size']);
    $this
      ->assertFalse($this->cacheItemBackend
      ->has(static::FILE));
    $this
      ->assertTrue($this->cacheItemBackend
      ->has('new.txt'));
  }
  public function testCopySuccess() {
    $config = new Config();
    $this->adapter
      ->write(static::FILE, 'contents', $config)
      ->willReturn([
      'size' => 1234,
    ]);
    $this->cacheAdapter
      ->write(static::FILE, 'contents', $config);
    $this->adapter
      ->copy(static::FILE, 'new.txt')
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->copy(static::FILE, 'new.txt'));
    $this
      ->assertSame(1234, $this->cacheAdapter
      ->getSize(static::FILE)['size']);
    $this
      ->assertSame(1234, $this->cacheAdapter
      ->getSize('new.txt')['size']);
    $this
      ->assertTrue($this->cacheItemBackend
      ->has(static::FILE));
    $this
      ->assertTrue($this->cacheItemBackend
      ->has('new.txt'));
  }
  public function testDeleteSuccess() {
    $config = new Config();
    $this->adapter
      ->write(static::FILE, 'contents', $config)
      ->willReturn([
      'size' => 1234,
    ]);
    $this->cacheAdapter
      ->write(static::FILE, 'contents', $config);
    $this->adapter
      ->delete(static::FILE)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->delete(static::FILE));
    $this
      ->assertFalse($this->cacheItemBackend
      ->has(static::FILE));
  }
  public function testDeleteDirSuccess() {
    $config = new Config();

    // Create a directory with one sub file.
    $this->adapter
      ->createDir('testdir', $config)
      ->willReturn([
      'type' => 'dir',
    ]);
    $this->adapter
      ->write('testdir/test.txt', 'contents', $config)
      ->willReturn([
      'size' => 1234,
    ]);
    $this->adapter
      ->deleteDir('testdir')
      ->willReturn(TRUE);
    $this->adapter
      ->listContents('testdir', TRUE)
      ->willReturn([
      [
        'path' => 'testdir',
      ],
      [
        'path' => 'testdir/test.txt',
      ],
    ]);
    $this->cacheAdapter
      ->createDir('testdir', $config);
    $this->cacheAdapter
      ->write('testdir/test.txt', 'contents', $config);
    $this
      ->assertTrue($this->cacheAdapter
      ->deleteDir('testdir'));
    $this
      ->assertFalse($this->cacheItemBackend
      ->has('testdir/test.txt'));
    $this
      ->assertFalse($this->cacheItemBackend
      ->has('testdir'));
  }
  public function testSetVisibilitySuccess() {
    $config = new Config();
    $this->adapter
      ->setVisibility(static::FILE, 'private')
      ->willReturn([
      'visibility' => 'private',
    ]);
    $metadata = $this->cacheAdapter
      ->setVisibility(static::FILE, 'private');
    $this
      ->assertSame('private', $metadata['visibility']);
    $this
      ->assertSame('private', $this->cacheAdapter
      ->getVisibility(static::FILE)['visibility']);
    $this
      ->assertTrue($this->cacheItemBackend
      ->has(static::FILE));
  }
  public function testHasSuccess() {
    $cache_item = $this->cacheItemBackend
      ->load(static::FILE);
    $this->cacheItemBackend
      ->set(static::FILE, $cache_item);
    $this
      ->assertTrue($this->cacheAdapter
      ->has(static::FILE));
  }
  public function testHasFail() {
    $this->adapter
      ->has(static::FILE)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->has(static::FILE));
  }
  public function testRead() {
    $this->adapter
      ->read(static::FILE)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->read(static::FILE));
  }
  public function testReadStream() {
    $this->adapter
      ->readStream(static::FILE)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->readStream(static::FILE));
  }
  public function testListContentsSuccess() {
    $this->adapter
      ->listContents('testdir', TRUE)
      ->willReturn(TRUE);
    $this
      ->assertTrue($this->cacheAdapter
      ->listContents('testdir', TRUE));
  }
  public function testGetMetadataSuccess() {
    $cache_item = $this->cacheItemBackend
      ->load(static::FILE);
    $cache_item
      ->updateMetadata([
      'type' => 'dir',
    ]);
    $this->cacheItemBackend
      ->set(static::FILE, $cache_item);
    $this
      ->assertSame('dir', $this->cacheAdapter
      ->getMetadata(static::FILE)['type']);
  }
  public function testGetMetadataFail() {
    $this->adapter
      ->getMetadata(static::FILE)
      ->willReturn([
      'type' => 'dir',
    ]);
    $this
      ->assertSame('dir', $this->cacheAdapter
      ->getMetadata(static::FILE)['type']);
  }
  public function testGetSizeFail() {
    $this->adapter
      ->getSize(static::FILE)
      ->willReturn([
      'size' => 123,
    ]);
    $this
      ->assertSame(123, $this->cacheAdapter
      ->getSize(static::FILE)['size']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalCacheAdapterTest::$adapter protected property The wrapped Flysytem adaper.
DrupalCacheAdapterTest::$cacheAdapter protected property The cache adapter under test.
DrupalCacheAdapterTest::$cacheItemBackend protected property The flysystem backend for testing.
DrupalCacheAdapterTest::FILE constant The main test file.
DrupalCacheAdapterTest::SCHEME constant URI scheme to use for testing.
DrupalCacheAdapterTest::setup public function
DrupalCacheAdapterTest::testCopySuccess public function
DrupalCacheAdapterTest::testDeleteDirSuccess public function
DrupalCacheAdapterTest::testDeleteSuccess public function
DrupalCacheAdapterTest::testGetMetadataFail public function
DrupalCacheAdapterTest::testGetMetadataSuccess public function
DrupalCacheAdapterTest::testGetSizeFail public function
DrupalCacheAdapterTest::testHasFail public function
DrupalCacheAdapterTest::testHasSuccess public function
DrupalCacheAdapterTest::testListContentsSuccess public function
DrupalCacheAdapterTest::testRead public function
DrupalCacheAdapterTest::testReadStream public function
DrupalCacheAdapterTest::testRenameSuccess public function
DrupalCacheAdapterTest::testSetVisibilitySuccess public function
DrupalCacheAdapterTest::testUpdateStreamSuccess public function
DrupalCacheAdapterTest::testUpdateSuccess public function
DrupalCacheAdapterTest::testWriteStreamSuccess public function
DrupalCacheAdapterTest::testWriteSuccess public function
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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 308
UnitTestCase::setUpBeforeClass public static function