]> git.mxchange.org Git - friendica.git/blob - tests/Util/VFSTrait.php
Add the possibility to use a different configuration directory
[friendica.git] / tests / Util / VFSTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\Util;
23
24
25 use org\bovigo\vfs\vfsStream;
26 use org\bovigo\vfs\vfsStreamDirectory;
27
28 trait VFSTrait
29 {
30         /**
31          * @var vfsStreamDirectory The Stream Directory
32          */
33         protected $root;
34
35         /**
36          * Sets up the Virtual File System for Friendica with common files (config, dbstructure)
37          */
38         protected function setUpVfsDir() {
39                 // the used directories inside the App class
40                 $structure = [
41                         'config' => [],
42                         'bin' => [],
43                         'static' => [],
44                         'test' => [],
45                         'logs' => [],
46                         'config2' => [],
47                 ];
48
49                 // create a virtual directory and copy all needed files and folders to it
50                 $this->root = vfsStream::setup('friendica', 0777, $structure);
51
52                 $this->setConfigFile('dbstructure.config.php', true);
53                 $this->setConfigFile('defaults.config.php', true);
54                 $this->setConfigFile('settings.config.php', true);
55                 $this->setConfigFile('local.config.php');
56         }
57
58         /**
59          * Copying a config file from the file system to the Virtual File System
60          *
61          * @param string $filename The filename of the config file
62          * @param bool   $static   True, if the folder `static` instead of `config` should be used
63          */
64         protected function setConfigFile(string $filename, bool $static = false)
65         {
66                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
67                         '..' . DIRECTORY_SEPARATOR .
68                         ($static ? 'static' : 'config') . DIRECTORY_SEPARATOR .
69                         $filename;
70
71                 if (file_exists($file)) {
72                         vfsStream::newFile($filename)
73                                 ->at($this->root->getChild(($static ? 'static' : 'config')))
74                                 ->setContent(file_get_contents($file));
75                 }
76         }
77
78         /**
79          * Delets a config file from the Virtual File System
80          *
81          * @param string $filename The filename of the config file
82          * @param bool   $static   True, if the folder `static` instead of `config` should be used
83          */
84         protected function delConfigFile(string $filename, bool $static = false)
85         {
86                 if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) {
87                         $this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename);
88                 }
89         }
90 }