View source
<?php
namespace Drupal\config_filter\Tests;
use Drupal\config_filter\Config\ReadOnlyStorage;
use Drupal\config_filter\Exception\UnsupportedMethod;
use Drupal\Core\Config\StorageInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
class ReadonlyStorageTest extends UnitTestCase {
protected function getStorage(StorageInterface $source) {
return new ReadOnlyStorage($source);
}
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);
}
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(),
],
];
}
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);
}
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());
}
}
public function writeMethodsProvider() {
return [
[
'write',
[
$this
->randomMachineName(),
$this
->randomArray(),
],
],
[
'delete',
[
$this
->randomMachineName(),
],
],
[
'rename',
[
$this
->randomMachineName(),
$this
->randomMachineName(),
],
],
[
'deleteAll',
[
$this
->randomMachineName(),
],
],
];
}
protected function randomArray() {
return (array) $this
->getRandomGenerator()
->object();
}
}