From 83ead5ec483041c2751c5de0d45cd63a598fb02c Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 1 Nov 2018 13:44:47 +0100 Subject: [PATCH] Test enhancements --- src/BaseObject.php | 2 +- tests/DatabaseTest.php | 6 +- tests/MockedTest.php | 18 +++++ tests/Util/AppMockTrait.php | 5 -- tests/Util/DBAMockTrait.php | 20 ++++++ tests/Util/L10nMockTrait.php | 45 +++++++++++++ tests/{ => include}/ApiTest.php | 0 tests/{ => include}/TextTest.php | 0 tests/src/App/ModeTest.php | 57 +++++----------- tests/{ => src}/BaseObjectTest.php | 2 +- .../AutomaticInstallationConsoleTest.php | 4 ++ tests/src/Core/InstallerTest.php | 66 ++++++++++++++----- 12 files changed, 154 insertions(+), 71 deletions(-) create mode 100644 tests/MockedTest.php create mode 100644 tests/Util/L10nMockTrait.php rename tests/{ => include}/ApiTest.php (100%) rename tests/{ => include}/TextTest.php (100%) rename tests/{ => src}/BaseObjectTest.php (94%) diff --git a/src/BaseObject.php b/src/BaseObject.php index 6b64daccf0..d006c249de 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 ec1eb290d1..2cb76dcad9 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 0000000000..87f7757025 --- /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 a076ac23d0..1bb69c27bf 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 0000000000..f1c771c6b3 --- /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/ApiTest.php b/tests/include/ApiTest.php similarity index 100% rename from tests/ApiTest.php rename to tests/include/ApiTest.php 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 bac553eb8c..cab953bdaf 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 b3d0189583..f8542f7b38 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 d8f78b483f..957517f035 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,6 +53,8 @@ class AutomaticInstallationConsoleTest extends ConsoleTest $this->db_pass = getenv('MYSQL_PASSWORD'); $this->mockConfigGet('config', 'php_path', false); + + $this->mockL10nT(); } /** diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index a4ee20b8ce..ebbf5037d4 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, -- 2.39.5