2018-10-06 14:27:20 +00:00
|
|
|
<?php
|
2024-08-24 12:31:41 +00:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-10-06 14:27:20 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\Util;
|
|
|
|
|
|
|
|
|
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
use org\bovigo\vfs\vfsStreamDirectory;
|
|
|
|
|
|
|
|
trait VFSTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var vfsStreamDirectory The Stream Directory
|
|
|
|
*/
|
|
|
|
protected $root;
|
|
|
|
|
2018-10-31 09:24:07 +00:00
|
|
|
/**
|
|
|
|
* Sets up the Virtual File System for Friendica with common files (config, dbstructure)
|
|
|
|
*/
|
2018-10-06 14:27:20 +00:00
|
|
|
protected function setUpVfsDir() {
|
|
|
|
// the used directories inside the App class
|
|
|
|
$structure = [
|
|
|
|
'config' => [],
|
2018-10-31 10:03:15 +00:00
|
|
|
'bin' => [],
|
2019-06-23 17:56:21 +00:00
|
|
|
'static' => [],
|
|
|
|
'test' => [],
|
2019-09-23 13:23:09 +00:00
|
|
|
'logs' => [],
|
2021-09-11 12:33:26 +00:00
|
|
|
'config2' => [],
|
2018-10-06 14:27:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// create a virtual directory and copy all needed files and folders to it
|
2019-03-13 22:05:33 +00:00
|
|
|
$this->root = vfsStream::setup('friendica', 0777, $structure);
|
2018-10-06 14:27:20 +00:00
|
|
|
|
2023-02-12 12:09:05 +00:00
|
|
|
$this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'dbstructure.config.php', true);
|
|
|
|
$this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'dbview.config.php', true);
|
|
|
|
$this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'defaults.config.php', true);
|
|
|
|
$this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'settings.config.php', true);
|
|
|
|
$this->setConfigFile(
|
2023-02-12 13:38:43 +00:00
|
|
|
'mods' . DIRECTORY_SEPARATOR . 'local.config.ci.php',
|
2023-02-12 12:09:05 +00:00
|
|
|
false, 'local.config.php'
|
|
|
|
);
|
2018-10-06 14:27:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:24:07 +00:00
|
|
|
/**
|
|
|
|
* Copying a config file from the file system to the Virtual File System
|
|
|
|
*
|
2023-02-12 12:09:05 +00:00
|
|
|
* @param string $sourceFilePath The filename of the config file
|
|
|
|
* @param bool $static True, if the folder `static` instead of `config` should be used
|
2018-10-31 09:24:07 +00:00
|
|
|
*/
|
2023-03-26 20:27:24 +00:00
|
|
|
public function setConfigFile(string $sourceFilePath, bool $static = false, string $targetFileName = null)
|
2018-10-06 14:27:20 +00:00
|
|
|
{
|
|
|
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
|
|
|
'..' . DIRECTORY_SEPARATOR .
|
2023-02-12 12:09:05 +00:00
|
|
|
$sourceFilePath;
|
2018-10-06 14:27:20 +00:00
|
|
|
|
|
|
|
if (file_exists($file)) {
|
2023-02-12 12:09:05 +00:00
|
|
|
if (empty($targetFileName)) {
|
|
|
|
$tmpArray = preg_split('/\\' . DIRECTORY_SEPARATOR . '/', $sourceFilePath);
|
|
|
|
$targetFileName = array_pop($tmpArray);
|
|
|
|
}
|
|
|
|
vfsStream::newFile($targetFileName)
|
2019-06-23 17:56:21 +00:00
|
|
|
->at($this->root->getChild(($static ? 'static' : 'config')))
|
2018-10-06 14:27:20 +00:00
|
|
|
->setContent(file_get_contents($file));
|
2023-02-12 12:09:05 +00:00
|
|
|
} else {
|
|
|
|
throw new \Exception(sprintf('Unexpected missing config \'%s\'', $file));
|
2018-10-06 14:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 09:24:07 +00:00
|
|
|
/**
|
2023-03-26 22:09:03 +00:00
|
|
|
* Deletes a config file from the Virtual File System
|
2018-10-31 09:24:07 +00:00
|
|
|
*
|
|
|
|
* @param string $filename The filename of the config file
|
2020-10-18 18:31:57 +00:00
|
|
|
* @param bool $static True, if the folder `static` instead of `config` should be used
|
2018-10-31 09:24:07 +00:00
|
|
|
*/
|
2020-10-18 18:31:57 +00:00
|
|
|
protected function delConfigFile(string $filename, bool $static = false)
|
2018-10-06 14:27:20 +00:00
|
|
|
{
|
2019-06-23 17:56:21 +00:00
|
|
|
if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) {
|
|
|
|
$this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename);
|
2018-10-06 14:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|