Merge pull request #6062 from nupplaphil/test_enhancements

Test enhancements
This commit is contained in:
Hypolite Petovan 2018-11-01 10:30:06 -04:00 committed by GitHub
commit 4f76d21e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 177 additions and 72 deletions

View File

@ -34,7 +34,7 @@ class BaseObject
/**
* Set the app
*
* @param object $app App
* @param App $app App
*
* @return void
*/

View File

@ -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;

18
tests/MockedTest.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Friendica\Test;
use PHPUnit\Framework\TestCase;
/**
* This class verifies each mock after each call
*/
abstract class MockedTest extends TestCase
{
protected function tearDown()
{
\Mockery::close();
parent::tearDown();
}
}

View File

@ -27,11 +27,6 @@ trait AppMockTrait
*/
public function mockApp($root)
{
// simply returning the input when using L10n::t()
$l10nMock = \Mockery::mock('alias:Friendica\Core\L10n');
$l10nMock->shouldReceive('t')
->andReturnUsing(function ($arg) { return $arg; });
$this->mockConfigGet('system', 'theme', 'testtheme');
// Mocking App and most used functions

View File

@ -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);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Friendica\Test\Util;
use Mockery\MockInterface;
trait L10nMockTrait
{
/**
* @var MockInterface The interface for L10n mocks
*/
private $l10nMock;
/**
* Mocking the 'L10n::t()' method
*
* @param null|string $input Either an input (string) or null for EVERY input is possible
* @param null|int $times How often will it get called
* @param null|string $return Either an return (string) or null for return the input
*/
public function mockL10nT($input = null, $times = null, $return = null)
{
if (!isset($this->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);
}
}
}

View File

@ -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)) {

View File

@ -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.

View File

@ -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();

View File

@ -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());
}

View File

@ -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 = [

View File

@ -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,