]> git.mxchange.org Git - friendica.git/commitdiff
Fix tests
authorPhilipp Holzer <admin+github@philipp.info>
Fri, 26 Jul 2019 13:54:14 +0000 (15:54 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Sat, 27 Jul 2019 21:54:13 +0000 (23:54 +0200)
12 files changed:
src/BaseObject.php
tests/DatabaseTest.php
tests/Util/AppMockTrait.php
tests/include/ApiTest.php
tests/src/BaseObjectTest.php
tests/src/Content/Text/BBCodeTest.php
tests/src/Core/Cache/MemoryCacheTest.php
tests/src/Core/InstallerTest.php
tests/src/Core/Lock/LockTest.php
tests/src/Database/DBATest.php
tests/src/Database/DBStructureTest.php
tests/src/Network/CurlResultTest.php

index 7aa012cef34f17cc7ead25d93326b6e637a1edcc..fcec89bb4228b5f3faae23beb535b7b8aee91c79 100644 (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 {
index 5f9d3bf72cdc05b835e18443463f01c5bc49a296..98f79e351b5b9f008a24c86d2b357a4a7f8d7960 100644 (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;
-
-       /** @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()
        {
index caf8a7c08475d24d04f6f95ba1531445fecc95cd..f9b026979cbdef989567538d4585374e31ae8526 100644 (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;
index a7193c8872dc6ad0280171a15636d1fc557358e7..351973d47968f036eba579a43a07c1f8543ad4f0 100644 (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
index cb980b47e90698639f71a1381ba7c19b046e2db5..6935214527ef2a3b40a0a17656a17c1e12cba77b 100644 (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
index df89bda31242b1bf99221211e2b29b893e2bb484..3affe4e778a9cf78c8ee07b78744c581a6b00d25 100644 (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()
index 7ddcc45727cfcb9ec110b6bb86a881ce9166e0ab..6688153b05ad8082c03cde44da82d1972f88fc76 100644 (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');
                }
index 942cdec1a582bbfa2911fa90819078a76459f6ee..a898dd2957bb85cc100c9c9281194811716e8251 100644 (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)
index 7202058e4af4552569d6012c3fc5016c8fc822a0..59d6ee2d5d824f7b45cba698efde6db55a4b7654 100644 (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();
index 69ea267bd2890999ab0501cb3d54350f5533f51d..d6f59542f6978fb422e0f6a765ba2d8bce8a99b7 100644 (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);
index a951d1c0914f894b1551164a263ac5bd10501633..2747c3df21f84a4e0f43319424e44eb2c466897e 100644 (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);
        }
 
        /**
index b542371b3922432eda53ae84e2cb82e318de256c..72991c6d0f71695e6566b8c544ec5782b3afd993 100644 (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);
        }
 
        /**