2018-10-06 16:27:20 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-06 16:27:20 +02: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 10:24:07 +01:00
|
|
|
/**
|
|
|
|
* Sets up the Virtual File System for Friendica with common files (config, dbstructure)
|
|
|
|
*/
|
2018-10-06 16:27:20 +02:00
|
|
|
protected function setUpVfsDir() {
|
|
|
|
// the used directories inside the App class
|
|
|
|
$structure = [
|
|
|
|
'config' => [],
|
2018-10-31 11:03:15 +01:00
|
|
|
'bin' => [],
|
2019-06-23 19:56:21 +02:00
|
|
|
'static' => [],
|
|
|
|
'test' => [],
|
2019-09-23 15:23:09 +02:00
|
|
|
'logs' => [],
|
2021-09-11 14:33:26 +02:00
|
|
|
'config2' => [],
|
2018-10-06 16:27:20 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// create a virtual directory and copy all needed files and folders to it
|
2019-03-13 23:05:33 +01:00
|
|
|
$this->root = vfsStream::setup('friendica', 0777, $structure);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2023-02-12 13:09:05 +01: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 14:38:43 +01:00
|
|
|
'mods' . DIRECTORY_SEPARATOR . 'local.config.ci.php',
|
2023-02-12 13:09:05 +01:00
|
|
|
false, 'local.config.php'
|
|
|
|
);
|
2018-10-06 16:27:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:24:07 +01:00
|
|
|
/**
|
|
|
|
* Copying a config file from the file system to the Virtual File System
|
|
|
|
*
|
2023-02-12 13:09:05 +01: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 10:24:07 +01:00
|
|
|
*/
|
2023-02-12 13:09:05 +01:00
|
|
|
protected function setConfigFile(string $sourceFilePath, bool $static = false, string $targetFileName = null)
|
2018-10-06 16:27:20 +02:00
|
|
|
{
|
|
|
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
|
|
|
'..' . DIRECTORY_SEPARATOR .
|
2023-02-12 13:09:05 +01:00
|
|
|
$sourceFilePath;
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
if (file_exists($file)) {
|
2023-02-12 13:09:05 +01:00
|
|
|
if (empty($targetFileName)) {
|
|
|
|
$tmpArray = preg_split('/\\' . DIRECTORY_SEPARATOR . '/', $sourceFilePath);
|
|
|
|
$targetFileName = array_pop($tmpArray);
|
|
|
|
}
|
|
|
|
vfsStream::newFile($targetFileName)
|
2019-06-23 19:56:21 +02:00
|
|
|
->at($this->root->getChild(($static ? 'static' : 'config')))
|
2018-10-06 16:27:20 +02:00
|
|
|
->setContent(file_get_contents($file));
|
2023-02-12 13:09:05 +01:00
|
|
|
} else {
|
|
|
|
throw new \Exception(sprintf('Unexpected missing config \'%s\'', $file));
|
2018-10-06 16:27:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:24:07 +01:00
|
|
|
/**
|
2023-03-27 00:09:03 +02:00
|
|
|
* Deletes a config file from the Virtual File System
|
2018-10-31 10:24:07 +01:00
|
|
|
*
|
|
|
|
* @param string $filename The filename of the config file
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param bool $static True, if the folder `static` instead of `config` should be used
|
2018-10-31 10:24:07 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
protected function delConfigFile(string $filename, bool $static = false)
|
2018-10-06 16:27:20 +02:00
|
|
|
{
|
2019-06-23 19:56:21 +02:00
|
|
|
if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) {
|
|
|
|
$this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename);
|
2018-10-06 16:27:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|