]> git.mxchange.org Git - friendica.git/blob - tests/Util/VFSTrait.php
Fix transmission of events to Diaspora
[friendica.git] / tests / Util / VFSTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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('static' . DIRECTORY_SEPARATOR . 'dbstructure.config.php', true);
53                 $this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'dbview.config.php', true);
54                 $this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'defaults.config.php', true);
55                 $this->setConfigFile('static' . DIRECTORY_SEPARATOR . 'settings.config.php', true);
56                 $this->setConfigFile(
57                         'mods' . DIRECTORY_SEPARATOR . 'local.config.ci.php',
58                         false, 'local.config.php'
59                 );
60         }
61
62         /**
63          * Copying a config file from the file system to the Virtual File System
64          *
65          * @param string $sourceFilePath The filename of the config file
66          * @param bool   $static         True, if the folder `static` instead of `config` should be used
67          */
68         public function setConfigFile(string $sourceFilePath, bool $static = false, string $targetFileName = null)
69         {
70                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
71                         '..' . DIRECTORY_SEPARATOR .
72                                 $sourceFilePath;
73
74                 if (file_exists($file)) {
75                         if (empty($targetFileName)) {
76                                 $tmpArray = preg_split('/\\' . DIRECTORY_SEPARATOR . '/', $sourceFilePath);
77                                 $targetFileName = array_pop($tmpArray);
78                         }
79                         vfsStream::newFile($targetFileName)
80                                 ->at($this->root->getChild(($static ? 'static' : 'config')))
81                                 ->setContent(file_get_contents($file));
82                 } else {
83                         throw new \Exception(sprintf('Unexpected missing config \'%s\'', $file));
84                 }
85         }
86
87         /**
88          * Deletes a config file from the Virtual File System
89          *
90          * @param string $filename The filename of the config file
91          * @param bool   $static   True, if the folder `static` instead of `config` should be used
92          */
93         protected function delConfigFile(string $filename, bool $static = false)
94         {
95                 if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) {
96                         $this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename);
97                 }
98         }
99 }