You are here

class FlysystemFactoryTest in Flysystem 3.x

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

@coversDefaultClass \Drupal\flysystem\FlysystemFactory @group flysystem

Hierarchy

Expanded class hierarchy of FlysystemFactoryTest

File

tests/src/Unit/FlysystemFactoryTest.php, line 26

Namespace

Drupal\Tests\flysystem\Unit
View source
class FlysystemFactoryTest extends UnitTestCase {

  /**
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $filesystem;

  /**
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $plugin;

  /**
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $pluginManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->cache = new NullBackend('bin');
    $this->eventDispatcher = $this
      ->createMock(EventDispatcherInterface::class);
    $this->pluginManager = $this
      ->prophesize(PluginManagerInterface::class);
    $this->plugin = $this
      ->prophesize(FlysystemPluginInterface::class);
    $this->plugin
      ->getAdapter()
      ->willReturn(new NullAdapter());
    $this->pluginManager
      ->createInstance('testdriver', [])
      ->willReturn($this->plugin
      ->reveal());
    $this->pluginManager
      ->createInstance('', [])
      ->willReturn(new Missing());
    $this->filesystem = $this
      ->prophesize(StreamWrapperManager::class);
    $this->filesystem
      ->isValidScheme(Argument::type('string'))
      ->willReturn(TRUE);
  }

  /**
   * @covers ::getFilesystem
   * @covers ::__construct
   * @covers ::getAdapter
   * @covers ::getSettings
   * @covers ::getPlugin
   */
  public function testGetFilesystemReturnsValidFilesystem() {
    new Settings([
      'flysystem' => [
        'testscheme' => [
          'driver' => 'testdriver',
        ],
      ],
    ]);
    $factory = $this
      ->getFactory();
    $this
      ->assertInstanceOf(FilesystemInterface::class, $factory
      ->getFilesystem('testscheme'));
    $this
      ->assertInstanceOf(NullAdapter::class, $factory
      ->getFilesystem('testscheme')
      ->getAdapter());
  }

  /**
   * @covers ::getFilesystem
   */
  public function testGetFilesystemReturnsMissingFilesystem() {
    new Settings([]);
    $factory = $this
      ->getFactory();
    $this
      ->assertInstanceOf(MissingAdapter::class, $factory
      ->getFilesystem('testscheme')
      ->getAdapter());
  }

  /**
   * @covers ::getFilesystem
   * @covers ::getAdapter
   */
  public function testGetFilesystemReturnsCachedAdapter() {
    new Settings([
      'flysystem' => [
        'testscheme' => [
          'driver' => 'testdriver',
          'cache' => TRUE,
        ],
      ],
    ]);
    $factory = $this
      ->getFactory();
    $this
      ->assertInstanceOf(DrupalCacheAdapter::class, $factory
      ->getFilesystem('testscheme')
      ->getAdapter());
  }

  /**
   * @covers ::getFilesystem
   * @covers ::getAdapter
   */
  public function testGetFilesystemReturnsReplicateAdapter() {

    // Test replicate.
    $this->pluginManager
      ->createInstance('wrapped', [])
      ->willReturn($this->plugin
      ->reveal());
    new Settings([
      'flysystem' => [
        'testscheme' => [
          'driver' => 'testdriver',
          'replicate' => 'wrapped',
        ],
        'wrapped' => [
          'driver' => 'testdriver',
        ],
      ],
    ]);
    $factory = $this
      ->getFactory();
    $this
      ->assertInstanceOf(ReplicateAdapter::class, $factory
      ->getFilesystem('testscheme')
      ->getAdapter());
  }

  /**
   * @covers ::getSchemes
   * @covers ::__construct
   */
  public function testGetSchemesFiltersInvalidSchemes() {
    new Settings([
      'flysystem' => [
        'testscheme' => [
          'driver' => 'testdriver',
        ],
        'invalidscheme' => [
          'driver' => 'testdriver',
        ],
      ],
    ]);
    $this->filesystem
      ->isValidScheme('invalidscheme')
      ->willReturn(FALSE);
    $this
      ->assertSame([
      'testscheme',
    ], $this
      ->getFactory()
      ->getSchemes());
  }

  /**
   * @covers ::getSchemes
   */
  public function testGetSchemesHandlesNoSchemes() {
    new Settings([]);
    $this
      ->assertSame([], $this
      ->getFactory()
      ->getSchemes());
  }

  /**
   * @covers ::ensure
   */
  public function testEnsureReturnsErrors() {
    new Settings([
      'flysystem' => [
        'testscheme' => [
          'driver' => 'testdriver',
        ],
      ],
    ]);
    $this->plugin
      ->ensure(FALSE)
      ->willReturn([
      [
        'severity' => 'bad',
        'message' => 'Something bad',
        'context' => [],
      ],
    ]);
    $errors = $this
      ->getFactory()
      ->ensure();
    $this
      ->assertSame('Something bad', $errors['testscheme'][0]['message']);
  }

  /**
   * @return \Drupal\flysystem\FlysystemFactory
   */
  protected function getFactory() {
    return new FlysystemFactory($this->pluginManager
      ->reveal(), $this->filesystem
      ->reveal(), $this->cache, $this->eventDispatcher);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlysystemFactoryTest::$cache protected property
FlysystemFactoryTest::$eventDispatcher protected property
FlysystemFactoryTest::$filesystem protected property
FlysystemFactoryTest::$plugin protected property
FlysystemFactoryTest::$pluginManager protected property
FlysystemFactoryTest::getFactory protected function
FlysystemFactoryTest::setUp public function Overrides UnitTestCase::setUp
FlysystemFactoryTest::testEnsureReturnsErrors public function @covers ::ensure
FlysystemFactoryTest::testGetFilesystemReturnsCachedAdapter public function @covers ::getFilesystem @covers ::getAdapter
FlysystemFactoryTest::testGetFilesystemReturnsMissingFilesystem public function @covers ::getFilesystem
FlysystemFactoryTest::testGetFilesystemReturnsReplicateAdapter public function @covers ::getFilesystem @covers ::getAdapter
FlysystemFactoryTest::testGetFilesystemReturnsValidFilesystem public function @covers ::getFilesystem @covers ::__construct @covers ::getAdapter @covers ::getSettings @covers ::getPlugin
FlysystemFactoryTest::testGetSchemesFiltersInvalidSchemes public function @covers ::getSchemes @covers ::__construct
FlysystemFactoryTest::testGetSchemesHandlesNoSchemes public function @covers ::getSchemes
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::setUpBeforeClass public static function