class ReadOnlyStorageTest

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Config/ReadOnlyStorageTest.php \Drupal\Tests\Core\Config\ReadOnlyStorageTest
  2. 10 core/tests/Drupal/Tests/Core/Config/ReadOnlyStorageTest.php \Drupal\Tests\Core\Config\ReadOnlyStorageTest
  3. 11.x core/tests/Drupal/Tests/Core/Config/ReadOnlyStorageTest.php \Drupal\Tests\Core\Config\ReadOnlyStorageTest

@coversDefaultClass \Drupal\Core\Config\ReadOnlyStorage @group Config

Hierarchy

Expanded class hierarchy of ReadOnlyStorageTest

File

core/tests/Drupal/Tests/Core/Config/ReadOnlyStorageTest.php, line 15

Namespace

Drupal\Tests\Core\Config
View source
class ReadOnlyStorageTest extends UnitTestCase {
    use StorageCopyTrait;
    
    /**
     * The memory storage containing the data.
     *
     * @var \Drupal\Core\Config\MemoryStorage
     */
    protected $memory;
    
    /**
     * The read-only storage under test.
     *
     * @var \Drupal\Core\Config\ReadOnlyStorage
     */
    protected $storage;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        // Set up a memory storage we can manipulate to set fixtures.
        $this->memory = new MemoryStorage();
        // Wrap the memory storage in the read-only storage to test it.
        $this->storage = new ReadOnlyStorage($this->memory);
    }
    
    /**
     * @covers ::exists
     * @covers ::read
     * @covers ::readMultiple
     * @covers ::listAll
     *
     * @dataProvider readMethodsProvider
     */
    public function testReadOperations($method, $arguments, $fixture) {
        $this->setRandomFixtureConfig($fixture);
        $expected = call_user_func_array([
            $this->memory,
            $method,
        ], $arguments);
        $actual = call_user_func_array([
            $this->storage,
            $method,
        ], $arguments);
        $this->assertEquals($expected, $actual);
    }
    
    /**
     * Provide the methods that work transparently.
     *
     * @return array
     *   The data.
     */
    public function readMethodsProvider() {
        $fixture = [
            StorageInterface::DEFAULT_COLLECTION => [
                'config.a',
                'config.b',
                'other.a',
            ],
        ];
        $data = [];
        $data[] = [
            'exists',
            [
                'config.a',
            ],
            $fixture,
        ];
        $data[] = [
            'exists',
            [
                'not.existing',
            ],
            $fixture,
        ];
        $data[] = [
            'read',
            [
                'config.a',
            ],
            $fixture,
        ];
        $data[] = [
            'read',
            [
                'not.existing',
            ],
            $fixture,
        ];
        $data[] = [
            'readMultiple',
            [
                [
                    'config.a',
                    'config.b',
                    'not',
                ],
            ],
            $fixture,
        ];
        $data[] = [
            'listAll',
            [
                '',
            ],
            $fixture,
        ];
        $data[] = [
            'listAll',
            [
                'config',
            ],
            $fixture,
        ];
        $data[] = [
            'listAll',
            [
                'none',
            ],
            $fixture,
        ];
        return $data;
    }
    
    /**
     * @covers ::write
     * @covers ::delete
     * @covers ::rename
     * @covers ::deleteAll
     *
     * @dataProvider writeMethodsProvider
     */
    public function testWriteOperations($method, $arguments, $fixture) {
        $this->setRandomFixtureConfig($fixture);
        // Create an independent memory storage as a backup.
        $backup = new MemoryStorage();
        static::replaceStorageContents($this->memory, $backup);
        try {
            call_user_func_array([
                $this->storage,
                $method,
            ], $arguments);
            $this->fail("exception not thrown");
        } catch (\BadMethodCallException $exception) {
            $this->assertEquals(ReadOnlyStorage::class . '::' . $method . ' is not allowed on a ReadOnlyStorage', $exception->getMessage());
        }
        // Assert that the memory storage has not been altered.
        $this->assertEquals($backup, $this->memory);
    }
    
    /**
     * Provide the methods that throw an exception.
     *
     * @return array
     *   The data
     */
    public function writeMethodsProvider() {
        $fixture = [
            StorageInterface::DEFAULT_COLLECTION => [
                'config.a',
                'config.b',
            ],
        ];
        $data = [];
        $data[] = [
            'write',
            [
                'config.a',
                (array) $this->getRandomGenerator()
                    ->object(),
            ],
            $fixture,
        ];
        $data[] = [
            'write',
            [
                $this->randomMachineName(),
                (array) $this->getRandomGenerator()
                    ->object(),
            ],
            $fixture,
        ];
        $data[] = [
            'delete',
            [
                'config.a',
            ],
            $fixture,
        ];
        $data[] = [
            'delete',
            [
                $this->randomMachineName(),
            ],
            $fixture,
        ];
        $data[] = [
            'rename',
            [
                'config.a',
                'config.b',
            ],
            $fixture,
        ];
        $data[] = [
            'rename',
            [
                'config.a',
                $this->randomMachineName(),
            ],
            $fixture,
        ];
        $data[] = [
            'rename',
            [
                $this->randomMachineName(),
                $this->randomMachineName(),
            ],
            $fixture,
        ];
        $data[] = [
            'deleteAll',
            [
                '',
            ],
            $fixture,
        ];
        $data[] = [
            'deleteAll',
            [
                'config',
            ],
            $fixture,
        ];
        $data[] = [
            'deleteAll',
            [
                'other',
            ],
            $fixture,
        ];
        return $data;
    }
    
    /**
     * @covers ::getAllCollectionNames
     * @covers ::getCollectionName
     * @covers ::createCollection
     */
    public function testCollections() {
        $fixture = [
            StorageInterface::DEFAULT_COLLECTION => [
                $this->randomMachineName(),
            ],
            'A' => [
                $this->randomMachineName(),
            ],
            'B' => [
                $this->randomMachineName(),
            ],
            'C' => [
                $this->randomMachineName(),
            ],
        ];
        $this->setRandomFixtureConfig($fixture);
        $this->assertEquals([
            'A',
            'B',
            'C',
        ], $this->storage
            ->getAllCollectionNames());
        foreach (array_keys($fixture) as $collection) {
            $storage = $this->storage
                ->createCollection($collection);
            // Assert that the collection storage is still a read-only storage.
            $this->assertInstanceOf(ReadOnlyStorage::class, $storage);
            $this->assertEquals($collection, $storage->getCollectionName());
        }
    }
    
    /**
     * @covers ::encode
     * @covers ::decode
     */
    public function testEncodeDecode() {
        $array = (array) $this->getRandomGenerator()
            ->object();
        $string = $this->getRandomGenerator()
            ->string();
        // Assert reversibility of encoding and decoding.
        $this->assertEquals($array, $this->storage
            ->decode($this->storage
            ->encode($array)));
        $this->assertEquals($string, $this->storage
            ->encode($this->storage
            ->decode($string)));
        // Assert same results as the decorated storage.
        $this->assertEquals($this->memory
            ->encode($array), $this->storage
            ->encode($array));
        $this->assertEquals($this->memory
            ->decode($string), $this->storage
            ->decode($string));
    }
    
    /**
     * Generate random config in the memory storage.
     *
     * @param array $config
     *   The config keys, keyed by the collection.
     */
    protected function setRandomFixtureConfig($config) {
        // Erase previous fixture.
        foreach (array_merge([
            StorageInterface::DEFAULT_COLLECTION,
        ], $this->memory
            ->getAllCollectionNames()) as $collection) {
            $this->memory
                ->createCollection($collection)
                ->deleteAll();
        }
        foreach ($config as $collection => $keys) {
            $storage = $this->memory
                ->createCollection($collection);
            foreach ($keys as $key) {
                // Create some random config.
                $storage->write($key, (array) $this->getRandomGenerator()
                    ->object());
            }
        }
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
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.
ReadOnlyStorageTest::$memory protected property The memory storage containing the data.
ReadOnlyStorageTest::$storage protected property The read-only storage under test.
ReadOnlyStorageTest::readMethodsProvider public function Provide the methods that work transparently.
ReadOnlyStorageTest::setRandomFixtureConfig protected function Generate random config in the memory storage.
ReadOnlyStorageTest::setUp protected function Overrides UnitTestCase::setUp
ReadOnlyStorageTest::testCollections public function @covers ::getAllCollectionNames
@covers ::getCollectionName
@covers ::createCollection
ReadOnlyStorageTest::testEncodeDecode public function @covers ::encode
@covers ::decode
ReadOnlyStorageTest::testReadOperations public function @covers ::exists
@covers ::read
@covers ::readMultiple
@covers ::listAll
ReadOnlyStorageTest::testWriteOperations public function @covers ::write
@covers ::delete
@covers ::rename
@covers ::deleteAll
ReadOnlyStorageTest::writeMethodsProvider public function Provide the methods that throw an exception.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.