]> git.mxchange.org Git - friendica.git/blob - tests/Util/VFSTrait.php
110f24a61ae38e4a54b8c03dcfcb0ac1b3b0ce2f
[friendica.git] / tests / Util / VFSTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5
6 use org\bovigo\vfs\vfsStream;
7 use org\bovigo\vfs\vfsStreamDirectory;
8
9 trait VFSTrait
10 {
11         /**
12          * @var vfsStreamDirectory The Stream Directory
13          */
14         protected $root;
15
16         /**
17          * Sets up the Virtual File System for Friendica with common files (config, dbstructure)
18          */
19         protected function setUpVfsDir() {
20                 // the used directories inside the App class
21                 $structure = [
22                         'config' => [],
23                         'bin' => [],
24                         'static' => [],
25                         'test' => [],
26                 ];
27
28                 // create a virtual directory and copy all needed files and folders to it
29                 $this->root = vfsStream::setup('friendica', 0777, $structure);
30
31                 $this->setConfigFile('defaults.config.php', true);
32                 $this->setConfigFile('settings.config.php', true);
33                 $this->setConfigFile('local.config.php');
34         }
35
36         /**
37          * Copying a config file from the file system to the Virtual File System
38          *
39          * @param string $filename The filename of the config file
40          * @param bool $static True, if the folder `static` instead of `config` should be used
41          */
42         protected function setConfigFile($filename, bool $static = false)
43         {
44                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
45                         '..' . DIRECTORY_SEPARATOR .
46                         ($static ? 'static' : 'config') . DIRECTORY_SEPARATOR .
47                         $filename;
48
49                 if (file_exists($file)) {
50                         vfsStream::newFile($filename)
51                                 ->at($this->root->getChild(($static ? 'static' : 'config')))
52                                 ->setContent(file_get_contents($file));
53                 }
54         }
55
56         /**
57          * Delets a config file from the Virtual File System
58          *
59          * @param string $filename The filename of the config file
60          * @param bool $static True, if the folder `static` instead of `config` should be used
61          */
62         protected function delConfigFile($filename, bool $static = false)
63         {
64                 if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) {
65                         $this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename);
66                 }
67         }
68 }