diff --git a/src/BaseObject.php b/src/BaseObject.php index 6b64daccf..d006c249d 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -34,7 +34,7 @@ class BaseObject /** * Set the app * - * @param object $app App + * @param App $app App * * @return void */ diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index ec1eb290d..2cb76dcad 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,13 +5,9 @@ namespace Friendica\Test; -use Friendica\App; -use Friendica\BaseObject; -use Friendica\Core\Config; use Friendica\Database\DBA; use PHPUnit\DbUnit\DataSet\YamlDataSet; use PHPUnit\DbUnit\TestCaseTrait; -use PHPUnit\Framework\TestCase; use PHPUnit_Extensions_Database_DB_IDatabaseConnection; require_once __DIR__ . '/../boot.php'; @@ -19,7 +15,7 @@ require_once __DIR__ . '/../boot.php'; /** * Abstract class used by tests that need a database. */ -abstract class DatabaseTest extends TestCase +abstract class DatabaseTest extends MockedTest { use TestCaseTrait; diff --git a/tests/MockedTest.php b/tests/MockedTest.php new file mode 100644 index 000000000..87f775702 --- /dev/null +++ b/tests/MockedTest.php @@ -0,0 +1,18 @@ +shouldReceive('t') - ->andReturnUsing(function ($arg) { return $arg; }); - $this->mockConfigGet('system', 'theme', 'testtheme'); // Mocking App and most used functions diff --git a/tests/Util/DBAMockTrait.php b/tests/Util/DBAMockTrait.php index a076ac23d..1bb69c27b 100644 --- a/tests/Util/DBAMockTrait.php +++ b/tests/Util/DBAMockTrait.php @@ -49,4 +49,24 @@ trait DBAMockTrait ->times($times) ->andReturn($return); } + + /** + * Mocking DBA::fetchFirst() + * + * @param string $arg The argument of fetchFirst + * @param bool $return True, if the DB is connected, otherwise false + * @param null|int $times How often the method will get used + */ + public function mockFetchFirst($arg, $return = true, $times = null) + { + if (!isset($this->dbaMock)) { + $this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA'); + } + + $this->dbaMock + ->shouldReceive('fetchFirst') + ->with($arg) + ->times($times) + ->andReturn($return); + } } diff --git a/tests/Util/L10nMockTrait.php b/tests/Util/L10nMockTrait.php new file mode 100644 index 000000000..f1c771c6b --- /dev/null +++ b/tests/Util/L10nMockTrait.php @@ -0,0 +1,45 @@ +l10nMock)) { + $this->l10nMock = \Mockery::mock('alias:Friendica\Core\L10n'); + } + + $with = isset($input) ? $input : \Mockery::any(); + + $return = isset($return) ? $return : $with; + + if ($return instanceof \Mockery\Matcher\Any) { + $this->l10nMock + ->shouldReceive('t') + ->with($with) + ->times($times) + ->andReturnUsing(function($arg) { return $arg; }); + } else { + $this->l10nMock + ->shouldReceive('t') + ->with($with) + ->times($times) + ->andReturn($return); + } + } +} diff --git a/tests/Util/RendererMockTrait.php b/tests/Util/RendererMockTrait.php index 1fe3bd97d..b12327f49 100644 --- a/tests/Util/RendererMockTrait.php +++ b/tests/Util/RendererMockTrait.php @@ -18,6 +18,13 @@ trait RendererMockTrait */ private $rendererMock; + /** + * Mocking the method 'Renderer::getMarkupTemplate()' + * + * @param string $templateName The name of the template which should get + * @param string $return the return value of the mock (should be defined to have it later for followUp use) + * @param null|int $times How often the method will get used + */ public function mockGetMarkupTemplate($templateName, $return = '', $times = null) { if (!isset($this->rendererMock)) { @@ -31,6 +38,14 @@ trait RendererMockTrait ->andReturn($return); } + /** + * Mocking the method 'Renderer::replaceMacros()' + * + * @param string $template The template to use (normally, it is the mock result of 'mockGetMarkupTemplate()' + * @param array $args The arguments to pass to the macro + * @param string $return the return value of the mock + * @param null|int $times How often the method will get used + */ public function mockReplaceMacros($template, $args = [], $return = '', $times = null) { if (!isset($this->rendererMock)) { diff --git a/tests/ApiTest.php b/tests/include/ApiTest.php similarity index 99% rename from tests/ApiTest.php rename to tests/include/ApiTest.php index c509c7d2a..a539eb944 100644 --- a/tests/ApiTest.php +++ b/tests/include/ApiTest.php @@ -12,7 +12,7 @@ use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Network\HTTPException; -require_once __DIR__ . '/../include/api.php'; +require_once __DIR__ . '/../../include/api.php'; /** * Tests for the API functions. diff --git a/tests/TextTest.php b/tests/include/TextTest.php similarity index 100% rename from tests/TextTest.php rename to tests/include/TextTest.php diff --git a/tests/src/App/ModeTest.php b/tests/src/App/ModeTest.php index bac553eb8..cab953bda 100644 --- a/tests/src/App/ModeTest.php +++ b/tests/src/App/ModeTest.php @@ -3,16 +3,20 @@ namespace Friendica\Test\src\App; use Friendica\App\Mode; +use Friendica\Test\MockedTest; +use Friendica\Test\Util\ConfigMockTrait; +use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\VFSTrait; -use PHPUnit\Framework\TestCase; /** * @runTestsInSeparateProcesses * @preserveGlobalState disabled */ -class ModeTest extends TestCase +class ModeTest extends MockedTest { use VFSTrait; + use DBAMockTrait; + use ConfigMockTrait; public function setUp() { @@ -48,10 +52,7 @@ class ModeTest extends TestCase public function testWithoutDatabase() { - $dba = \Mockery::mock('alias:Friendica\Database\DBA'); - $dba - ->shouldReceive('connected') - ->andReturn(false); + $this->mockConnected(false, 1); $mode = new Mode($this->root->url()); $mode->determine(); @@ -65,14 +66,8 @@ class ModeTest extends TestCase 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); + $this->mockConnected(true, 1); + $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', false, 1); $mode = new Mode($this->root->url()); $mode->determine(); @@ -85,20 +80,9 @@ class ModeTest extends TestCase 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); + $this->mockConnected(true, 1); + $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1); + $this->mockConfigGet('system', 'maintenance', true, 1); $mode = new Mode($this->root->url()); $mode->determine(); @@ -112,20 +96,9 @@ class ModeTest extends TestCase 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); + $this->mockConnected(true, 1); + $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1); + $this->mockConfigGet('system', 'maintenance', false, 1); $mode = new Mode($this->root->url()); $mode->determine(); diff --git a/tests/BaseObjectTest.php b/tests/src/BaseObjectTest.php similarity index 94% rename from tests/BaseObjectTest.php rename to tests/src/BaseObjectTest.php index b3d018958..f8542f7b3 100644 --- a/tests/BaseObjectTest.php +++ b/tests/src/BaseObjectTest.php @@ -38,7 +38,7 @@ class BaseObjectTest extends TestCase */ public function testSetApp() { - $app = new App(__DIR__.'/../'); + $app = new App(__DIR__ . '/../../'); $this->assertNull($this->baseObject->setApp($app)); $this->assertEquals($app, $this->baseObject->getApp()); } diff --git a/tests/src/Core/Console/AutomaticInstallationConsoleTest.php b/tests/src/Core/Console/AutomaticInstallationConsoleTest.php index 9882bf37e..957517f03 100644 --- a/tests/src/Core/Console/AutomaticInstallationConsoleTest.php +++ b/tests/src/Core/Console/AutomaticInstallationConsoleTest.php @@ -5,6 +5,7 @@ namespace Friendica\Test\src\Core\Console; use Friendica\Core\Console\AutomaticInstallation; use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\DBStructureMockTrait; +use Friendica\Test\Util\L10nMockTrait; use Friendica\Test\Util\RendererMockTrait; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamFile; @@ -16,6 +17,7 @@ use org\bovigo\vfs\vfsStreamFile; */ class AutomaticInstallationConsoleTest extends ConsoleTest { + use L10nMockTrait; use DBAMockTrait; use DBStructureMockTrait; use RendererMockTrait; @@ -51,8 +53,17 @@ class AutomaticInstallationConsoleTest extends ConsoleTest $this->db_pass = getenv('MYSQL_PASSWORD'); $this->mockConfigGet('config', 'php_path', false); + + $this->mockL10nT(); } + /** + * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.ini.php + * + * @param bool $withDb if true, DB will get saved too + * + * @return array The arguments to pass to the mock for 'replaceMacros()' + */ private function createArgumentsForMacro($withDb) { $args = [ diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index a4ee20b8c..ebbf5037d 100644 --- a/tests/src/Core/InstallerTest.php +++ b/tests/src/Core/InstallerTest.php @@ -3,24 +3,48 @@ // this is in the same namespace as Install for mocking 'function_exists' namespace Friendica\Core; +use Friendica\Test\MockedTest; +use Friendica\Test\Util\L10nMockTrait; use Friendica\Test\Util\VFSTrait; -use PHPUnit\Framework\TestCase; /** * @runTestsInSeparateProcesses * @preserveGlobalState disabled */ -class InstallerTest extends TestCase +class InstallerTest extends MockedTest { use VFSTrait; + use L10nMockTrait; public function setUp() { - parent::setUp(); // TODO: Change the autogenerated stub + parent::setUp(); $this->setUpVfsDir(); } + /** + * Mocking the L10n::t() calls for the function checks + */ + private function mockFunctionL10TCalls() + { + $this->mockL10nT('Apache mod_rewrite module', 1); + $this->mockL10nT('PDO or MySQLi PHP module', 1); + $this->mockL10nT('libCurl PHP module', 1); + $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1); + $this->mockL10nT('XML PHP module', 1); + $this->mockL10nT('GD graphics PHP module', 1); + $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1); + $this->mockL10nT('OpenSSL PHP module', 1); + $this->mockL10nT('Error: openssl PHP module required but not installed.', 1); + $this->mockL10nT('mb_string PHP module', 1); + $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1); + $this->mockL10nT('iconv PHP module', 1); + $this->mockL10nT('Error: iconv PHP module required but not installed.', 1); + $this->mockL10nT('POSIX PHP module', 1); + $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1); + } + private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray) { $this->assertArraySubset([$position => [ @@ -87,66 +111,73 @@ class InstallerTest extends TestCase */ public function testCheckFunctions() { - $this->setFunctions(['curl_init' => false]); + $this->mockFunctionL10TCalls(); + $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(3, - L10n::t('libCurl PHP module'), - L10n::t('Error: libCURL PHP module required but not installed.'), + 'libCurl PHP module', + 'Error: libCURL PHP module required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions(['imagecreatefromjpeg' => false]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(4, - L10n::t('GD graphics PHP module'), - L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'), + 'GD graphics PHP module', + 'Error: GD graphics PHP module with JPEG support required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions(['openssl_public_encrypt' => false]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(5, - L10n::t('OpenSSL PHP module'), - L10n::t('Error: openssl PHP module required but not installed.'), + 'OpenSSL PHP module', + 'Error: openssl PHP module required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions(['mb_strlen' => false]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(6, - L10n::t('mb_string PHP module'), - L10n::t('Error: mb_string PHP module required but not installed.'), + 'mb_string PHP module', + 'Error: mb_string PHP module required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions(['iconv_strlen' => false]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(7, - L10n::t('iconv PHP module'), - L10n::t('Error: iconv PHP module required but not installed.'), + 'iconv PHP module', + 'Error: iconv PHP module required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions(['posix_kill' => false]); $install = new Installer(); $this->assertFalse($install->checkFunctions()); $this->assertCheckExist(8, - L10n::t('POSIX PHP module'), - L10n::t('Error: POSIX PHP module required but not installed.'), + 'POSIX PHP module', + 'Error: POSIX PHP module required but not installed.', false, true, $install->getChecks()); + $this->mockFunctionL10TCalls(); $this->setFunctions([ 'curl_init' => true, 'imagecreatefromjpeg' => true, @@ -308,13 +339,14 @@ class InstallerTest extends TestCase public function testImagickNotInstalled() { $this->setClasses(['Imagick' => false]); + $this->mockL10nT('ImageMagick PHP extension is not installed'); $install = new Installer(); // even there is no supported type, Imagick should return true (because it is not required) $this->assertTrue($install->checkImagick()); $this->assertCheckExist(0, - L10n::t('ImageMagick PHP extension is not installed'), + 'ImageMagick PHP extension is not installed', '', false, false,