You are here

class FlysystemFactoryTest in Flysystem 8

Same name and namespace in other branches
  1. 3.x 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 25

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 $plguinManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->cache = new NullBackend('bin');
    $this->eventDispatcher = $this
      ->getMock(EventDispatcherInterface::class);
    $this->plugin_manager = $this
      ->prophesize(PluginManagerInterface::class);
    $this->plugin = $this
      ->prophesize(FlysystemPluginInterface::class);
    $this->plugin
      ->getAdapter()
      ->willReturn(new NullAdapter());
    $this->plugin_manager
      ->createInstance('testdriver', [])
      ->willReturn($this->plugin
      ->reveal());
    $this->plugin_manager
      ->createInstance('', [])
      ->willReturn(new Missing());
    $this->filesystem = $this
      ->prophesize(CoreFileSystemInterface::class);
    $this->filesystem
      ->validScheme(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->plugin_manager
      ->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
      ->validScheme('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->plugin_manager
      ->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::$plguinManager protected property
FlysystemFactoryTest::$plugin 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
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.