Fix tests

This commit is contained in:
Philipp Holzer 2019-07-26 15:54:14 +02:00
parent 2a87464c97
commit 07aaf292ec
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
12 changed files with 134 additions and 144 deletions

View file

@ -42,7 +42,7 @@ class BaseObject
*/
public static function getApp()
{
return self::$dice->create(App::class);
return self::getClass(App::class);
}
/**
@ -54,8 +54,12 @@ class BaseObject
*
* @throws InternalServerErrorException
*/
public static function getClass(string $name)
protected static function getClass(string $name)
{
if (empty(self::$dice)) {
throw new InternalServerErrorException('DICE isn\'t initialized.');
}
if (class_exists($name) || interface_exists($name )) {
return self::$dice->create($name);
} else {

View file

@ -5,19 +5,10 @@
namespace Friendica\Test;
use Friendica\App\Mode;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Database\Database;
use Friendica\Factory\ConfigFactory;
use Friendica\Util\BasePath;
use Friendica\Util\ConfigFileLoader;
use Friendica\Util\Profiler;
use PDO;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
use Psr\Log\NullLogger;
require_once __DIR__ . '/../boot.php';
/**
* Abstract class used by tests that need a database.
@ -26,33 +17,11 @@ abstract class DatabaseTest extends MockedTest
{
use TestCaseTrait;
/** @var Database */
protected static $dba;
// only instantiate pdo once for test clean-up/fixture load
static private $pdo = null;
/** @var BasePath */
protected static $basePath;
/** @var Mode */
protected static $mode;
/** @var ConfigCache */
protected static $configCache;
/** @var Profiler */
protected static $profiler;
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
self::$basePath = new BasePath(dirname(__DIR__));
$configLoader = new ConfigFileLoader(self::$basePath->getPath());
$configFactory = new ConfigFactory();
self::$configCache = $configFactory->createCache($configLoader);
self::$profiler = new Profiler(self::$configCache);
self::$dba = new Database(self::$configCache, self::$profiler, new NullLogger(), $_SERVER);
self::$mode = new Mode(self::$basePath, self::$dba, self::$configCache);
}
// only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
private $conn = null;
/**
* Get database connection.
@ -67,23 +36,45 @@ abstract class DatabaseTest extends MockedTest
*/
protected function getConnection()
{
if (!getenv('MYSQL_DATABASE')) {
$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
}
$server = $_SERVER;
if (!self::$dba->isConnected()) {
if (!self::$dba->connect()) {
$this->markTestSkipped('Could not connect to the database.');
if ($this->conn === null) {
if (self::$pdo == null) {
if (!empty($server['MYSQL_HOST'])
&& !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
&& $server['MYSQL_PASSWORD'] !== false
&& !empty($server['MYSQL_DATABASE'])) {
$connect = "mysql:host=" . $server['MYSQL_HOST'] . ";dbname=" . $server['MYSQL_DATABASE'];
if (!empty($server['MYSQL_PORT'])) {
$connect .= ";port=" . $server['MYSQL_PORT'];
}
if (!empty($server['MYSQL_USERNAME'])) {
$db_user = $server['MYSQL_USERNAME'];
} else {
$db_user = $server['MYSQL_USER'];
}
$db_pass = (string)$server['MYSQL_PASSWORD'];
self::$pdo = @new PDO($connect, $db_user, $db_pass);
self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, getenv('MYSQL_DATABASE'));
}
return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE'));
return $this->conn;
}
/**
* Get dataset to populate the database with.
*
* @return YamlDataSet
* @see https://phpunit.de/manual/5.7/en/database.html
* @see https://phtablepunit.de/manual/5.7/en/database.html
*/
protected function getDataSet()
{

View file

@ -2,6 +2,7 @@
namespace Friendica\Test\Util;
use Dice\Dice;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config;
@ -35,6 +36,11 @@ trait AppMockTrait
*/
protected $mode;
/**
* @var MockInterface|Dice The dependency injection library
*/
protected $dice;
/**
* Mock the App
*
@ -43,18 +49,31 @@ trait AppMockTrait
*/
public function mockApp(vfsStreamDirectory $root, $raw = false)
{
$this->dice = \Mockery::mock(Dice::class)->makePartial();
$this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
$this->configMock = \Mockery::mock(Config\Cache\ConfigCache::class);
$this->dice->shouldReceive('create')
->with(Config\Cache\ConfigCache::class)
->andReturn($this->configMock);
$this->mode = \Mockery::mock(App\Mode::class);
$this->dice->shouldReceive('create')
->with(App\Mode::class)
->andReturn($this->mode);
$configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
// Disable the adapter
$configModel->shouldReceive('isConnected')->andReturn(false);
$config = new Config\JitConfiguration($this->configMock, $configModel);
// Initialize empty Config
Config::init($config);
$this->dice->shouldReceive('create')
->with(Config\Configuration::class)
->andReturn($config);
// Mocking App and most used functions
$this->app = \Mockery::mock(App::class);
$this->dice->shouldReceive('create')
->with(App::class)
->andReturn($this->app);
$this->app
->shouldReceive('getBasePath')
->andReturn($root->url());
@ -65,6 +84,9 @@ trait AppMockTrait
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
$this->dice->shouldReceive('create')
->with(Profiler::class)
->andReturn($this->profilerMock);
$this->app
->shouldReceive('getConfigCache')
@ -87,7 +109,7 @@ trait AppMockTrait
return $this->configMock->get('system', 'url');
});
BaseObject::setApp($this->app);
BaseObject::setDependencyInjection($this->dice);
if ($raw) {
return;

View file

@ -5,16 +5,14 @@
namespace Friendica\Test;
use Dice\Dice;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache\PConfigCache;
use Friendica\Core\L10n\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Factory;
use Friendica\Network\HTTPException;
use Friendica\Util\BaseURL;
use Monolog\Handler\TestHandler;
require_once __DIR__ . '/../../include/api.php';
@ -32,9 +30,6 @@ class ApiTest extends DatabaseTest
*/
protected $logOutput;
/** @var App */
protected $app;
/** @var array */
protected $selfUser;
/** @var array */
@ -44,27 +39,24 @@ class ApiTest extends DatabaseTest
protected $wrongUserId;
/** @var App */
protected $app;
/**
* Create variables used by tests.
*/
public function setUp()
{
$configModel = new \Friendica\Model\Config\Config(self::$dba);
$configFactory = new Factory\ConfigFactory();
$config = $configFactory->createConfig(self::$configCache, $configModel);
$pconfigModel = new \Friendica\Model\Config\PConfig(self::$dba);
$configFactory->createPConfig(self::$configCache, new PConfigCache(), $pconfigModel);
$loggerFactory = new Factory\LoggerFactory();
$logger = $loggerFactory->create('test', self::$dba, $config, self::$profiler);
$baseUrl = new BaseURL($config, $_SERVER);
$router = new App\Router();
$l10n = new L10n($config,
self::$dba,
$logger);
$this->app = new App(self::$dba, $config, self::$mode, $router, $baseUrl, $logger, self::$profiler, $l10n, false);
parent::setUp();
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
BaseObject::setDependencyInjection($dice);
$this->app = BaseObject::getApp();
$this->app->argc = 1;
$this->app->argv = ['home'];
// User data that the test database is populated with
$this->selfUser = [
'id' => 42,
@ -107,17 +99,6 @@ class ApiTest extends DatabaseTest
Config::set('system', 'theme', 'system_theme');
}
/**
* Cleanup variables used by tests.
*/
protected function tearDown()
{
parent::tearDown();
$this->app->argc = 1;
$this->app->argv = ['home'];
}
/**
* Assert that an user array contains expected keys.
* @param array $user User array

View file

@ -23,20 +23,6 @@ class BaseObjectTest extends TestCase
*/
private $baseObject;
/**
* Test the setApp() and getApp() function.
* @return void
*/
public function testGetSetApp()
{
$baseObject = new BaseObject();
$this->setUpVfsDir();
$this->mockApp($this->root);
$baseObject->setApp($this->app);
$this->assertEquals($this->app, $baseObject->getApp());
}
/**
* Test the getApp() function without App
* @expectedException Friendica\Network\HTTPException\InternalServerErrorException

View file

@ -41,8 +41,9 @@ class BBCodeTest extends MockedTest
$l10nMock = \Mockery::mock(L10n::class);
$l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; });
\Friendica\Core\L10n::init($l10nMock);
$this->dice->shouldReceive('create')
->with(L10n::class)
->andReturn($l10nMock);
}
public function dataLinks()

View file

@ -3,7 +3,7 @@
namespace Friendica\Test\src\Core\Cache;
use Friendica\Core\Cache\IMemoryCacheDriver;
use Friendica\Core\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
abstract class MemoryCacheTest extends CacheTest
@ -15,9 +15,13 @@ abstract class MemoryCacheTest extends CacheTest
protected function setUp()
{
Logger::init(new NullLogger());
parent::setUp();
$logger = new NullLogger();
$this->dice->shouldReceive('create')
->with(LoggerInterface::class)
->andReturn($logger);
if (!($this->instance instanceof IMemoryCacheDriver)) {
throw new \Exception('MemoryCacheTest unsupported');
}

View file

@ -3,6 +3,8 @@
// this is in the same namespace as Install for mocking 'function_exists'
namespace Friendica\Core;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Network\CurlResult;
use Friendica\Object\Image;
@ -27,7 +29,16 @@ class InstallerTest extends MockedTest
$this->setUpVfsDir();
$this->l10nMock = \Mockery::mock(\Friendica\Core\L10n\L10n::class);
L10n::init($this->l10nMock);
/** @var Dice|MockInterface $dice */
$dice = \Mockery::mock(Dice::class)->makePartial();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$dice->shouldReceive('create')
->with(\Friendica\Core\L10n\L10n::class)
->andReturn($this->l10nMock);
BaseObject::setDependencyInjection($dice);
}
private function mockL10nT(string $text, $times = null)

View file

@ -2,10 +2,10 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Logger;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\VFSTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
abstract class LockTest extends MockedTest
@ -34,7 +34,10 @@ abstract class LockTest extends MockedTest
->shouldReceive('getHostname')
->andReturn('friendica.local');
Logger::init(new NullLogger());
$logger = new NullLogger();
$this->dice->shouldReceive('create')
->with(LoggerInterface::class)
->andReturn($logger);
parent::setUp();
$this->instance = $this->getInstance();

View file

@ -1,35 +1,22 @@
<?php
namespace Friendica\Test\src\Database;
use Friendica\App;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache\PConfigCache;
use Friendica\Core\L10n\L10n;
use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Test\DatabaseTest;
use Friendica\Util\BaseURL;
class DBATest extends DatabaseTest
{
public function setUp()
{
$configModel = new \Friendica\Model\Config\Config(self::$dba);
$configFactory = new Factory\ConfigFactory();
$config = $configFactory->createConfig(self::$configCache, $configModel);
$pconfigModel = new \Friendica\Model\Config\PConfig(self::$dba);
$configFactory->createPConfig(self::$configCache, new PConfigCache(), $pconfigModel);
$loggerFactory = new Factory\LoggerFactory();
$logger = $loggerFactory->create('test', self::$dba, $config, self::$profiler);
$baseUrl = new BaseURL($config, $_SERVER);
$router = new App\Router();
$l10n = new L10n($config,
self::$dba,
$logger);
$this->app = new App(self::$dba, $config, self::$mode, $router, $baseUrl, $logger, self::$profiler, $l10n, false);
parent::setUp();
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
BaseObject::setDependencyInjection($dice);
// Default config
Config::set('config', 'hostname', 'localhost');
Config::set('system', 'throttle_limit_day', 100);

View file

@ -2,14 +2,10 @@
namespace Friendica\Test\src\Database;
use Friendica\App;
use Friendica\Core\Config\Cache\PConfigCache;
use Friendica\Core\L10n\L10n;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Database\DBStructure;
use Friendica\Factory;
use Friendica\Model\Config\Config;
use Friendica\Test\DatabaseTest;
use Friendica\Util\BaseURL;
class DBStructureTest extends DatabaseTest
{
@ -18,20 +14,11 @@ class DBStructureTest extends DatabaseTest
*/
public function setUp()
{
$configModel = new Config(self::$dba);
$configFactory = new Factory\ConfigFactory();
$config = $configFactory->createConfig(self::$configCache, $configModel);
$pconfigModel = new \Friendica\Model\Config\PConfig(self::$dba);
$configFactory->createPConfig(self::$configCache, new PConfigCache(), $pconfigModel);
$loggerFactory = new Factory\LoggerFactory();
$logger = $loggerFactory->create('test', self::$dba, $config, self::$profiler);
$baseUrl = new BaseURL($config, $_SERVER);
$router = new App\Router();
$l10n = new L10n($config,
self::$dba,
$logger);
$this->app = new App(self::$dba, $config, self::$mode, $router, $baseUrl, $logger, self::$profiler, $l10n, false);
parent::setUp();
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
BaseObject::setDependencyInjection($dice);
}
/**

View file

@ -2,10 +2,13 @@
namespace Friendica\Test\src\Network;
use Friendica\Core\Logger;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Network\CurlResult;
use Friendica\Util\Logger\VoidLogger;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class CurlResultTest extends TestCase
{
@ -13,7 +16,17 @@ class CurlResultTest extends TestCase
{
parent::setUp();
Logger::init(new VoidLogger());
/** @var Dice|MockInterface $dice */
$dice = \Mockery::mock(Dice::class)->makePartial();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$logger = new NullLogger();
$dice->shouldReceive('create')
->with(LoggerInterface::class)
->andReturn($logger);
BaseObject::setDependencyInjection($dice);
}
/**