]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Console/ConsoleTest.php
Automatic Install Tests & Doku (#5674)
[friendica.git] / tests / src / Core / Console / ConsoleTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Console;
4
5 use Friendica\App;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
8 use Friendica\Test\Util\Intercept;
9 use org\bovigo\vfs\vfsStream;
10 use org\bovigo\vfs\vfsStreamDirectory;
11 use PHPUnit\Framework\TestCase;
12
13 abstract class ConsoleTest extends TestCase
14 {
15         /**
16          * @var MultiUseConsole Extension of the basic Friendica Console for testing purpose
17          */
18         private $console;
19         /**
20          * @var App The Friendica App
21          */
22         protected $app;
23
24         /**
25          * @var vfsStreamDirectory The Stream Directory
26          */
27         protected $root;
28
29         protected $stdout;
30
31         protected function setUp()
32         {
33                 parent::setUp();
34
35                 Intercept::setUp();
36
37                 if (!getenv('MYSQL_DATABASE')) {
38                         $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
39                 }
40
41                 $this->setUpVfsDir();
42
43                 // Reusable App object
44                 $this->app = new App($this->root->url());
45                 BaseObject::setApp($this->app);
46                 $this->console = new MultiUseConsole();
47         }
48
49         public function execute($args) {
50                 DBA::disconnect();
51                 $this->app->reload();
52
53                 array_unshift($args, $this->getExecutablePath());
54                 Intercept::reset();
55                 $this->console->reset();
56                 $this->console->parseTestArgv($args);
57                 $this->console->execute();
58
59                 $returnStr = Intercept::$cache;
60                 Intercept::reset();
61                 return $returnStr;
62         }
63
64         /**
65          * @return string returns the path to the console executable during tests
66          */
67         protected function getExecutablePath() {
68                 return $this->root->getChild('bin' . DIRECTORY_SEPARATOR . 'console.php')->url();
69         }
70
71         private function setUpVfsDir() {
72                 // the used directories inside the App class
73                 $structure = [
74                         'config' => [],
75                         'bin' => []
76                 ];
77
78                 // create a virtual directory and copy all needed files and folders to it
79                 $this->root = vfsStream::setup('friendica', null, $structure);
80
81                 $this->setConfigFile('config.ini.php');
82                 $this->setConfigFile('settings.ini.php');
83                 $this->setConfigFile('local.ini.php');
84                 $this->setConfigFile('dbstructure.json');
85
86                 // fake console.php for setting an executable
87                 vfsStream::newFile('console.php')
88                         ->at($this->root->getChild('bin'))
89                         ->setContent('<? php');
90         }
91
92         private function setConfigFile($filename)
93         {
94                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
95                         '..' . DIRECTORY_SEPARATOR .
96                         '..' . DIRECTORY_SEPARATOR .
97                         '..' . DIRECTORY_SEPARATOR .
98                         'config' . DIRECTORY_SEPARATOR .
99                         $filename;
100
101                 if (file_exists($file)) {
102                         vfsStream::newFile($filename)
103                                 ->at($this->root->getChild('config'))
104                                 ->setContent(file_get_contents($file));
105                 }
106         }
107 }