FileStorageTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/Config/Storage/FileStorageTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Config\Storage;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\UnsupportedDataTypeConfigException;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StreamWrapper\PublicStream;
class FileStorageTest extends ConfigStorageTestBase {
protected $directory;
protected function setUp() {
parent::setUp();
$this->directory = PublicStream::basePath() . '/config';
$this->storage = new FileStorage($this->directory);
$this->invalidStorage = new FileStorage($this->directory . '/nonexisting');
$this->storage
->write('system.performance', $this
->config('system.performance')
->get());
$this->storage
->write('core.extension', [
'module' => [],
]);
}
protected function read($name) {
$data = file_get_contents($this->storage
->getFilePath($name));
return Yaml::decode($data);
}
protected function insert($name, $data) {
file_put_contents($this->storage
->getFilePath($name), $data);
}
protected function update($name, $data) {
file_put_contents($this->storage
->getFilePath($name), $data);
}
protected function delete($name) {
unlink($this->storage
->getFilePath($name));
}
public function testlistAll() {
$expected_files = [
'core.extension',
'system.performance',
];
$config_files = $this->storage
->listAll();
$this
->assertIdentical($config_files, $expected_files, 'Relative path, two config files found.');
$this
->assertSame([
'system.performance',
], $this->storage
->listAll('system'), 'The FileStorage::listAll() with prefix works.');
$this
->assertSame([], $this->storage
->listAll('System'), 'The FileStorage::listAll() is case sensitive.');
}
public function testReadUnsupportedDataTypeConfigException() {
file_put_contents($this->storage
->getFilePath('core.extension'), PHP_EOL . 'foo : [bar}', FILE_APPEND);
try {
$config_parsed = $this->storage
->read('core.extension');
} catch (UnsupportedDataTypeConfigException $e) {
$this
->assertStringContainsString($this->storage
->getFilePath('core.extension'), $e
->getMessage(), 'Erroneous file path is displayed.');
}
}
}