You are here

class ReadonlyStorageTest in Config Filter 8.2

Same name and namespace in other branches
  1. 8 src/Tests/ReadonlyStorageTest.php \Drupal\config_filter\Tests\ReadonlyStorageTest

Tests ReadonlyStorage operations.

@group config_filter

Hierarchy

Expanded class hierarchy of ReadonlyStorageTest

File

src/Tests/ReadonlyStorageTest.php, line 17

Namespace

Drupal\config_filter\Tests
View source
class ReadonlyStorageTest extends UnitTestCase {

  /**
   * Wrap a given storage.
   *
   * This is useful when testing a subclass of ReadonlyStorage.
   *
   * @param \Drupal\Core\Config\StorageInterface $source
   *   The storage to decorate.
   *
   * @return \Drupal\Core\Config\StorageInterface
   *   The storage wrapping the source.
   */
  protected function getStorage(StorageInterface $source) {
    return new ReadOnlyStorage($source);
  }

  /**
   * Test methods that should be transparent.
   *
   * @dataProvider readMethodsProvider
   */
  public function testReadOperations($method, $arguments, $returnValue) {
    $source = $this
      ->prophesize(StorageInterface::class);
    $methodProhecy = new MethodProphecy($source, $method, $arguments);
    $methodProhecy
      ->shouldBeCalledTimes(1);
    $methodProhecy
      ->willReturn($returnValue);
    $source
      ->addMethodProphecy($methodProhecy);
    $storage = $this
      ->getStorage($source
      ->reveal());
    $actual = call_user_func_array([
      $storage,
      $method,
    ], $arguments);
    $this
      ->assertEquals($actual, $returnValue);
  }

  /**
   * Provide the methods that should continue to work.
   *
   * @return array
   *   The data.
   */
  public function readMethodsProvider() {
    return [
      [
        'exists',
        [
          $this
            ->randomMachineName(),
        ],
        $this
          ->randomMachineName(),
      ],
      [
        'read',
        [
          $this
            ->randomMachineName(),
        ],
        $this
          ->randomArray(),
      ],
      [
        'readMultiple',
        [
          $this
            ->randomArray(),
        ],
        $this
          ->randomArray(),
      ],
      [
        'encode',
        [
          $this
            ->randomArray(),
        ],
        $this
          ->randomMachineName(),
      ],
      [
        'decode',
        [
          $this
            ->randomMachineName(),
        ],
        $this
          ->randomArray(),
      ],
      [
        'listAll',
        [
          $this
            ->randomMachineName(),
        ],
        $this
          ->randomArray(),
      ],
      [
        'getAllCollectionNames',
        [],
        $this
          ->randomArray(),
      ],
      [
        'getCollectionName',
        [],
        $this
          ->randomMachineName(),
      ],
    ];
  }

  /**
   * Test creating a collection.
   *
   * Creating collections returns a new instance, make sure it decorates the
   * new instance of the source.
   */
  public function testCreateCollection() {
    $name = $this
      ->randomMachineName();
    $source = $this
      ->prophesize(StorageInterface::class);
    $collectionSource = $this
      ->prophesize(StorageInterface::class)
      ->reveal();
    $source
      ->createCollection($name)
      ->willReturn($collectionSource);
    $storage = $this
      ->getStorage($source
      ->reveal());
    $collectionStorage = $storage
      ->createCollection($name);
    $this
      ->assertInstanceOf(ReadOnlyStorage::class, $collectionStorage);
    $readonlyReflection = new \ReflectionClass(ReadOnlyStorage::class);
    $storageProperty = $readonlyReflection
      ->getProperty('storage');
    $storageProperty
      ->setAccessible(TRUE);
    $actualSource = $storageProperty
      ->getValue($collectionStorage);
    $this
      ->assertEquals($collectionSource, $actualSource);
  }

  /**
   * Test the operations that should throw an error.
   *
   * @dataProvider writeMethodsProvider
   */
  public function testWriteOperations($method, $arguments) {
    $source = $this
      ->prophesize(StorageInterface::class);
    $source
      ->{$method}(Argument::any())
      ->shouldNotBeCalled();
    $storage = $this
      ->getStorage($source
      ->reveal());
    try {
      call_user_func_array([
        $storage,
        $method,
      ], $arguments);
      $this
        ->fail();
    } catch (UnsupportedMethod $exception) {
      $this
        ->assertEquals(ReadOnlyStorage::class . '::' . $method . ' is not allowed on a ReadOnlyStorage', $exception
        ->getMessage());
    }
  }

  /**
   * Provide the methods that should throw an exception.
   *
   * @return array
   *   The data
   */
  public function writeMethodsProvider() {
    return [
      [
        'write',
        [
          $this
            ->randomMachineName(),
          $this
            ->randomArray(),
        ],
      ],
      [
        'delete',
        [
          $this
            ->randomMachineName(),
        ],
      ],
      [
        'rename',
        [
          $this
            ->randomMachineName(),
          $this
            ->randomMachineName(),
        ],
      ],
      [
        'deleteAll',
        [
          $this
            ->randomMachineName(),
        ],
      ],
    ];
  }

  /**
   * Get a random array.
   *
   * @return array
   *   A random array used for data testing.
   */
  protected function randomArray() {
    return (array) $this
      ->getRandomGenerator()
      ->object();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ReadonlyStorageTest::getStorage protected function Wrap a given storage. 1
ReadonlyStorageTest::randomArray protected function Get a random array.
ReadonlyStorageTest::readMethodsProvider public function Provide the methods that should continue to work.
ReadonlyStorageTest::testCreateCollection public function Test creating a collection.
ReadonlyStorageTest::testReadOperations public function Test methods that should be transparent.
ReadonlyStorageTest::testWriteOperations public function Test the operations that should throw an error. 1
ReadonlyStorageTest::writeMethodsProvider public function Provide the methods that should throw an exception.
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