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() public static function getApp()
{ {
return self::$dice->create(App::class); return self::getClass(App::class);
} }
/** /**
@ -54,8 +54,12 @@ class BaseObject
* *
* @throws InternalServerErrorException * @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 )) { if (class_exists($name) || interface_exists($name )) {
return self::$dice->create($name); return self::$dice->create($name);
} else { } else {

View File

@ -5,19 +5,10 @@
namespace Friendica\Test; namespace Friendica\Test;
use Friendica\App\Mode; use PDO;
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 PHPUnit\DbUnit\DataSet\YamlDataSet; use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait; use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit_Extensions_Database_DB_IDatabaseConnection; use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
use Psr\Log\NullLogger;
require_once __DIR__ . '/../boot.php';
/** /**
* Abstract class used by tests that need a database. * Abstract class used by tests that need a database.
@ -26,33 +17,11 @@ abstract class DatabaseTest extends MockedTest
{ {
use TestCaseTrait; use TestCaseTrait;
/** @var Database */ // only instantiate pdo once for test clean-up/fixture load
protected static $dba; static private $pdo = null;
/** @var BasePath */ // only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
protected static $basePath; private $conn = null;
/** @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);
}
/** /**
* Get database connection. * Get database connection.
@ -67,23 +36,45 @@ abstract class DatabaseTest extends MockedTest
*/ */
protected function getConnection() protected function getConnection()
{ {
if (!getenv('MYSQL_DATABASE')) { $server = $_SERVER;
$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
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 (!self::$dba->isConnected()) { if (!empty($server['MYSQL_USERNAME'])) {
if (!self::$dba->connect()) { $db_user = $server['MYSQL_USERNAME'];
$this->markTestSkipped('Could not connect to the database.'); } else {
} $db_user = $server['MYSQL_USER'];
} }
return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE')); $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->conn;
} }
/** /**
* Get dataset to populate the database with. * Get dataset to populate the database with.
*
* @return YamlDataSet * @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() protected function getDataSet()
{ {

View File

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

View File

@ -5,16 +5,14 @@
namespace Friendica\Test; namespace Friendica\Test;
use Dice\Dice;
use Friendica\App; use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Config\Cache\PConfigCache;
use Friendica\Core\L10n\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Factory;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\BaseURL;
use Monolog\Handler\TestHandler; use Monolog\Handler\TestHandler;
require_once __DIR__ . '/../../include/api.php'; require_once __DIR__ . '/../../include/api.php';
@ -32,9 +30,6 @@ class ApiTest extends DatabaseTest
*/ */
protected $logOutput; protected $logOutput;
/** @var App */
protected $app;
/** @var array */ /** @var array */
protected $selfUser; protected $selfUser;
/** @var array */ /** @var array */
@ -44,27 +39,24 @@ class ApiTest extends DatabaseTest
protected $wrongUserId; protected $wrongUserId;
/** @var App */
protected $app;
/** /**
* Create variables used by tests. * Create variables used by tests.
*/ */
public function setUp() 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(); 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 // User data that the test database is populated with
$this->selfUser = [ $this->selfUser = [
'id' => 42, 'id' => 42,
@ -107,17 +99,6 @@ class ApiTest extends DatabaseTest
Config::set('system', 'theme', 'system_theme'); 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. * Assert that an user array contains expected keys.
* @param array $user User array * @param array $user User array

View File

@ -23,20 +23,6 @@ class BaseObjectTest extends TestCase
*/ */
private $baseObject; 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 * Test the getApp() function without App
* @expectedException Friendica\Network\HTTPException\InternalServerErrorException * @expectedException Friendica\Network\HTTPException\InternalServerErrorException

View File

@ -41,8 +41,9 @@ class BBCodeTest extends MockedTest
$l10nMock = \Mockery::mock(L10n::class); $l10nMock = \Mockery::mock(L10n::class);
$l10nMock->shouldReceive('t')->withAnyArgs()->andReturnUsing(function ($args) { return $args; }); $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() public function dataLinks()

View File

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

View File

@ -3,6 +3,8 @@
// this is in the same namespace as Install for mocking 'function_exists' // this is in the same namespace as Install for mocking 'function_exists'
namespace Friendica\Core; namespace Friendica\Core;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Network\CurlResult; use Friendica\Network\CurlResult;
use Friendica\Object\Image; use Friendica\Object\Image;
@ -27,7 +29,16 @@ class InstallerTest extends MockedTest
$this->setUpVfsDir(); $this->setUpVfsDir();
$this->l10nMock = \Mockery::mock(\Friendica\Core\L10n\L10n::class); $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) private function mockL10nT(string $text, $times = null)

View File

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

View File

@ -1,35 +1,22 @@
<?php <?php
namespace Friendica\Test\src\Database; namespace Friendica\Test\src\Database;
use Friendica\App; use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Config\Cache\PConfigCache;
use Friendica\Core\L10n\L10n;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Test\DatabaseTest; use Friendica\Test\DatabaseTest;
use Friendica\Util\BaseURL;
class DBATest extends DatabaseTest class DBATest extends DatabaseTest
{ {
public function setUp() 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(); parent::setUp();
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
BaseObject::setDependencyInjection($dice);
// Default config // Default config
Config::set('config', 'hostname', 'localhost'); Config::set('config', 'hostname', 'localhost');
Config::set('system', 'throttle_limit_day', 100); Config::set('system', 'throttle_limit_day', 100);

View File

@ -2,14 +2,10 @@
namespace Friendica\Test\src\Database; namespace Friendica\Test\src\Database;
use Friendica\App; use Dice\Dice;
use Friendica\Core\Config\Cache\PConfigCache; use Friendica\BaseObject;
use Friendica\Core\L10n\L10n;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
use Friendica\Factory;
use Friendica\Model\Config\Config;
use Friendica\Test\DatabaseTest; use Friendica\Test\DatabaseTest;
use Friendica\Util\BaseURL;
class DBStructureTest extends DatabaseTest class DBStructureTest extends DatabaseTest
{ {
@ -18,20 +14,11 @@ class DBStructureTest extends DatabaseTest
*/ */
public function setUp() 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(); 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; namespace Friendica\Test\src\Network;
use Friendica\Core\Logger; use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Network\CurlResult; use Friendica\Network\CurlResult;
use Friendica\Util\Logger\VoidLogger; use Mockery\MockInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class CurlResultTest extends TestCase class CurlResultTest extends TestCase
{ {
@ -13,7 +16,17 @@ class CurlResultTest extends TestCase
{ {
parent::setUp(); 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);
} }
/** /**