Changing Friendica\App\Mode from static methods to public methods

- Changing from static methods to public methods
- Adding dev-composer-dependency Mockery for static method mocking (f.e. Config, DBA)
- Adding ModeTest with Mocking
- removing bootstrap from phpunit.xml because of double loading tests\bootstrap.php
This commit is contained in:
Philipp Holzer 2018-10-06 16:27:20 +02:00
parent 5014779052
commit 31148e25cf
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
21 changed files with 498 additions and 106 deletions

52
tests/Util/VFSTrait.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace Friendica\Test\Util;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
trait VFSTrait
{
/**
* @var vfsStreamDirectory The Stream Directory
*/
protected $root;
protected 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');
}
protected function setConfigFile($filename)
{
$file = dirname(__DIR__) . 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));
}
}
protected function delConfigFile($filename)
{
if ($this->root->hasChild('config/' . $filename)) {
$this->root->removeChild('config/' . $filename);
}
}
}

139
tests/src/App/ModeTest.php Normal file
View file

@ -0,0 +1,139 @@
<?php
namespace Friendica\Test\src\App;
use Friendica\App\Mode;
use Friendica\Test\Util\VFSTrait;
use PHPUnit\Framework\TestCase;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class ModeTest extends TestCase
{
use VFSTrait;
public function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->setUpVfsDir();
}
public function testItEmpty()
{
$mode = new Mode($this->root->url());
$this->assertTrue($mode->isInstall());
$this->assertFalse($mode->isNormal());
}
public function testWithoutConfig()
{
$mode = new Mode($this->root->url());
$this->assertTrue($this->root->hasChild('config/local.ini.php'));
$this->delConfigFile('local.ini.php');
$this->assertFalse($this->root->hasChild('config/local.ini.php'));
$mode->determine();
$this->assertTrue($mode->isInstall());
$this->assertFalse($mode->isNormal());
$this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
}
public function testWithoutDatabase()
{
$dba = \Mockery::mock('alias:Friendica\Database\DBA');
$dba
->shouldReceive('connected')
->andReturn(false);
$mode = new Mode($this->root->url());
$mode->determine();
$this->assertFalse($mode->isNormal());
$this->assertTrue($mode->isInstall());
$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
$this->assertFalse($mode->has(Mode::DBAVAILABLE));
}
public function testWithoutDatabaseSetup()
{
$dba = \Mockery::mock('alias:Friendica\Database\DBA');
$dba
->shouldReceive('connected')
->andReturn(true);
$dba
->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')
->andReturn(false);
$mode = new Mode($this->root->url());
$mode->determine();
$this->assertFalse($mode->isNormal());
$this->assertTrue($mode->isInstall());
$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
}
public function testWithMaintenanceMode()
{
$dba = \Mockery::mock('alias:Friendica\Database\DBA');
$dba
->shouldReceive('connected')
->andReturn(true);
$dba
->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')
->andReturn(true);
$conf = \Mockery::mock('alias:Friendica\Core\Config');
$conf
->shouldReceive('get')
->with('system', 'maintenance')
->andReturn(true);
$mode = new Mode($this->root->url());
$mode->determine();
$this->assertFalse($mode->isNormal());
$this->assertFalse($mode->isInstall());
$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
$this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
}
public function testNormalMode()
{
$dba = \Mockery::mock('alias:Friendica\Database\DBA');
$dba
->shouldReceive('connected')
->andReturn(true);
$dba
->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')
->andReturn(true);
$conf = \Mockery::mock('alias:Friendica\Core\Config');
$conf
->shouldReceive('get')
->with('system', 'maintenance')
->andReturn(false);
$mode = new Mode($this->root->url());
$mode->determine();
$this->assertTrue($mode->isNormal());
$this->assertFalse($mode->isInstall());
$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
$this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
}
}

View file

@ -6,12 +6,15 @@ use Friendica\App;
use Friendica\BaseObject;
use Friendica\Database\DBA;
use Friendica\Test\Util\Intercept;
use Friendica\Test\Util\VFSTrait;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
abstract class ConsoleTest extends TestCase
{
use VFSTrait;
/**
* @var MultiUseConsole Extension of the basic Friendica Console for testing purpose
*/
@ -21,11 +24,6 @@ abstract class ConsoleTest extends TestCase
*/
protected $app;
/**
* @var vfsStreamDirectory The Stream Directory
*/
protected $root;
protected $stdout;
protected function setUp()
@ -40,6 +38,11 @@ abstract class ConsoleTest extends TestCase
$this->setUpVfsDir();
// fake console.php for setting an executable
vfsStream::newFile('console.php')
->at($this->root->getChild('bin'))
->setContent('<? php');
// Reusable App object
$this->app = new App($this->root->url());
BaseObject::setApp($this->app);
@ -67,41 +70,4 @@ abstract class ConsoleTest extends TestCase
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));
}
}
}