2018-08-17 15:41:46 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core\Console;
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\BaseObject;
|
2018-08-27 00:15:55 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-08-17 15:41:46 -04:00
|
|
|
use Friendica\Test\Util\Intercept;
|
2018-08-27 00:15:55 -04:00
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
use org\bovigo\vfs\vfsStreamDirectory;
|
2018-08-17 15:41:46 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
abstract class ConsoleTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var MultiUseConsole Extension of the basic Friendica Console for testing purpose
|
|
|
|
*/
|
|
|
|
private $console;
|
|
|
|
/**
|
|
|
|
* @var App The Friendica App
|
|
|
|
*/
|
|
|
|
protected $app;
|
|
|
|
|
2018-08-27 00:15:55 -04:00
|
|
|
/**
|
|
|
|
* @var vfsStreamDirectory The Stream Directory
|
|
|
|
*/
|
|
|
|
protected $root;
|
|
|
|
|
2018-08-17 15:41:46 -04:00
|
|
|
protected $stdout;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Intercept::setUp();
|
|
|
|
|
|
|
|
if (!getenv('MYSQL_DATABASE')) {
|
|
|
|
$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:15:55 -04:00
|
|
|
$this->setUpVfsDir();
|
|
|
|
|
2018-08-17 15:41:46 -04:00
|
|
|
// Reusable App object
|
2018-08-27 00:15:55 -04:00
|
|
|
$this->app = new App($this->root->url());
|
|
|
|
BaseObject::setApp($this->app);
|
2018-08-17 15:41:46 -04:00
|
|
|
$this->console = new MultiUseConsole();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($args) {
|
2018-08-27 00:15:55 -04:00
|
|
|
DBA::disconnect();
|
|
|
|
$this->app->reload();
|
|
|
|
|
|
|
|
array_unshift($args, $this->getExecutablePath());
|
2018-08-17 15:41:46 -04:00
|
|
|
Intercept::reset();
|
|
|
|
$this->console->reset();
|
|
|
|
$this->console->parseTestArgv($args);
|
|
|
|
$this->console->execute();
|
|
|
|
|
|
|
|
$returnStr = Intercept::$cache;
|
|
|
|
Intercept::reset();
|
|
|
|
return $returnStr;
|
|
|
|
}
|
2018-08-27 00:15:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string returns the path to the console executable during tests
|
|
|
|
*/
|
|
|
|
protected function getExecutablePath() {
|
|
|
|
return $this->root->getChild('bin' . DIRECTORY_SEPARATOR . 'console.php')->url();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setUpVfsDir() {
|
|
|
|
// the used directories inside the App class
|
|
|
|
$structure = [
|
|
|
|
'config' => [],
|
|
|
|
'bin' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
// create a virtual directory and copy all needed files and folders to it
|
|
|
|
$this->root = vfsStream::setup('friendica', null, $structure);
|
|
|
|
|
|
|
|
$this->setConfigFile('config.ini.php');
|
|
|
|
$this->setConfigFile('settings.ini.php');
|
|
|
|
$this->setConfigFile('local.ini.php');
|
|
|
|
$this->setConfigFile('dbstructure.json');
|
|
|
|
|
|
|
|
// fake console.php for setting an executable
|
|
|
|
vfsStream::newFile('console.php')
|
|
|
|
->at($this->root->getChild('bin'))
|
|
|
|
->setContent('<? php');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setConfigFile($filename)
|
|
|
|
{
|
|
|
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
|
|
|
'..' . DIRECTORY_SEPARATOR .
|
|
|
|
'..' . DIRECTORY_SEPARATOR .
|
|
|
|
'..' . DIRECTORY_SEPARATOR .
|
|
|
|
'config' . DIRECTORY_SEPARATOR .
|
|
|
|
$filename;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
vfsStream::newFile($filename)
|
|
|
|
->at($this->root->getChild('config'))
|
|
|
|
->setContent(file_get_contents($file));
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 15:41:46 -04:00
|
|
|
}
|