class ReadonlyStorageTest in Config Filter 8.2
Same name and namespace in other branches
- 8 src/Tests/ReadonlyStorageTest.php \Drupal\config_filter\Tests\ReadonlyStorageTest
Tests ReadonlyStorage operations.
@group config_filter
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\config_filter\Tests\ReadonlyStorageTest
Expanded class hierarchy of ReadonlyStorageTest
File
- src/
Tests/ ReadonlyStorageTest.php, line 17
Namespace
Drupal\config_filter\TestsView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ReadonlyStorageTest:: |
protected | function | Wrap a given storage. | 1 |
ReadonlyStorageTest:: |
protected | function | Get a random array. | |
ReadonlyStorageTest:: |
public | function | Provide the methods that should continue to work. | |
ReadonlyStorageTest:: |
public | function | Test creating a collection. | |
ReadonlyStorageTest:: |
public | function | Test methods that should be transparent. | |
ReadonlyStorageTest:: |
public | function | Test the operations that should throw an error. | 1 |
ReadonlyStorageTest:: |
public | function | Provide the methods that should throw an exception. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |