*/
public static function getApp()
{
- return self::$dice->create(App::class);
+ return self::getClass(App::class);
}
/**
*
* @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 {
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.
{
use TestCaseTrait;
- /** @var Database */
- protected static $dba;
-
- /** @var BasePath */
- protected static $basePath;
-
- /** @var Mode */
- protected static $mode;
+ // only instantiate pdo once for test clean-up/fixture load
+ static private $pdo = null;
- /** @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.
*/
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()
{
namespace Friendica\Test\Util;
+use Dice\Dice;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config;
*/
protected $mode;
+ /**
+ * @var MockInterface|Dice The dependency injection library
+ */
+ protected $dice;
+
/**
* Mock the App
*
*/
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());
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
+ $this->dice->shouldReceive('create')
+ ->with(Profiler::class)
+ ->andReturn($this->profilerMock);
$this->app
->shouldReceive('getConfigCache')
return $this->configMock->get('system', 'url');
});
- BaseObject::setApp($this->app);
+ BaseObject::setDependencyInjection($this->dice);
if ($raw) {
return;
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';
*/
protected $logOutput;
- /** @var App */
- protected $app;
-
/** @var array */
protected $selfUser;
/** @var array */
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,
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
*/
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
$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()
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
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');
}
// 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;
$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)
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
->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();
<?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);
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
{
*/
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);
}
/**
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
{
{
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);
}
/**