From: Philipp Holzer Date: Fri, 26 Jul 2019 13:54:14 +0000 (+0200) Subject: Fix tests X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=07aaf292ec62c90585c614f5dbbc39f7805fe1d6;p=friendica.git Fix tests --- diff --git a/src/BaseObject.php b/src/BaseObject.php index 7aa012cef3..fcec89bb42 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -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 { diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 5f9d3bf72c..98f79e351b 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -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; - - /** @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. @@ -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() { diff --git a/tests/Util/AppMockTrait.php b/tests/Util/AppMockTrait.php index caf8a7c084..f9b026979c 100644 --- a/tests/Util/AppMockTrait.php +++ b/tests/Util/AppMockTrait.php @@ -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; diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index a7193c8872..351973d479 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -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 diff --git a/tests/src/BaseObjectTest.php b/tests/src/BaseObjectTest.php index cb980b47e9..6935214527 100644 --- a/tests/src/BaseObjectTest.php +++ b/tests/src/BaseObjectTest.php @@ -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 diff --git a/tests/src/Content/Text/BBCodeTest.php b/tests/src/Content/Text/BBCodeTest.php index df89bda312..3affe4e778 100644 --- a/tests/src/Content/Text/BBCodeTest.php +++ b/tests/src/Content/Text/BBCodeTest.php @@ -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() diff --git a/tests/src/Core/Cache/MemoryCacheTest.php b/tests/src/Core/Cache/MemoryCacheTest.php index 7ddcc45727..6688153b05 100644 --- a/tests/src/Core/Cache/MemoryCacheTest.php +++ b/tests/src/Core/Cache/MemoryCacheTest.php @@ -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'); } diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index 942cdec1a5..a898dd2957 100644 --- a/tests/src/Core/InstallerTest.php +++ b/tests/src/Core/InstallerTest.php @@ -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) diff --git a/tests/src/Core/Lock/LockTest.php b/tests/src/Core/Lock/LockTest.php index 7202058e4a..59d6ee2d5d 100644 --- a/tests/src/Core/Lock/LockTest.php +++ b/tests/src/Core/Lock/LockTest.php @@ -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(); diff --git a/tests/src/Database/DBATest.php b/tests/src/Database/DBATest.php index 69ea267bd2..d6f59542f6 100644 --- a/tests/src/Database/DBATest.php +++ b/tests/src/Database/DBATest.php @@ -1,35 +1,22 @@ 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); diff --git a/tests/src/Database/DBStructureTest.php b/tests/src/Database/DBStructureTest.php index a951d1c091..2747c3df21 100644 --- a/tests/src/Database/DBStructureTest.php +++ b/tests/src/Database/DBStructureTest.php @@ -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); } /** diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php index b542371b39..72991c6d0f 100644 --- a/tests/src/Network/CurlResultTest.php +++ b/tests/src/Network/CurlResultTest.php @@ -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); } /**