Fixing tests

This commit is contained in:
Philipp Holzer 2019-02-17 21:41:45 +01:00
parent 5e5c39b0e1
commit f5adbd268b
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
18 changed files with 101 additions and 93 deletions

View file

@ -6,6 +6,7 @@ use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Render\FriendicaSmartyEngine;
use Friendica\Util\Profiler;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStreamDirectory;
@ -22,70 +23,81 @@ trait AppMockTrait
/**
* @var MockInterface|Config\Configuration The mocked Config Cache
*/
protected $configCache;
protected $configMock;
/**
* @var MockInterface|Profiler The mocked profiler
*/
protected $profilerMock;
/**
* Mock the App
*
* @param vfsStreamDirectory $root The root directory
* @param MockInterface|Config\Configuration $config The config cache
*/
public function mockApp($root, Config\Configuration $config)
public function mockApp($root)
{
$this->configCache = $config;
$this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
$configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
// Disable the adapter
$configAdapterMock->shouldReceive('isConnected')->andReturn(false);
$config = new Config\Configuration($this->configMock, $configAdapterMock);
// Initialize empty Config
Config::init($config);
// Mocking App and most used functions
$this->app = \Mockery::mock(App::class);
$this->app
->shouldReceive('getBasePath')
->andReturn($root->url());
$config
$this->configMock
->shouldReceive('has')
->andReturn(true);
$this->configMock
->shouldReceive('get')
->with('database', 'hostname')
->andReturn(getenv('MYSQL_HOST'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'username')
->andReturn(getenv('MYSQL_USERNAME'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'password')
->andReturn(getenv('MYSQL_PASSWORD'));
$config
$this->configMock
->shouldReceive('get')
->with('database', 'database')
->andReturn(getenv('MYSQL_DATABASE'));
$config
$this->configMock
->shouldReceive('get')
->with('config', 'hostname')
->andReturn('localhost');
$config
$this->configMock
->shouldReceive('get')
->with('system', 'theme', NULL, false)
->with('system', 'theme')
->andReturn('system_theme');
$config
->shouldReceive('getConfig')
->andReturn($config);
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
$this->app
->shouldReceive('getConfigCache')
->andReturn($config);
->andReturn($this->configMock);
$this->app
->shouldReceive('getTemplateEngine')
->andReturn(new FriendicaSmartyEngine());
$this->app
->shouldReceive('getCurrentTheme')
->andReturn('Smarty3');
$this->app
->shouldReceive('saveTimestamp')
->andReturn(true);
$this->app
->shouldReceive('getBaseUrl')
->andReturn('http://friendica.local');
// Initialize empty Config
Config::init($config);
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
BaseObject::setApp($this->app);
}

View file

@ -2,6 +2,7 @@
namespace Friendica\Test\Util;
use Friendica\Database\DBStructure;
use Mockery\MockInterface;
/**
@ -16,6 +17,7 @@ trait DBStructureMockTrait
/**
* Mocking DBStructure::update()
* @see DBStructure::update();
*
* @param array $args The arguments for the update call
* @param bool $return True, if the connect was successful, otherwise false

View file

@ -90,7 +90,7 @@ class ModeTest extends MockedTest
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
$config = \Mockery::mock('Friendica\Core\Config\Configuration');
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null, false)
@ -118,7 +118,7 @@ class ModeTest extends MockedTest
$this->mockConnected(true, 1);
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
$config = \Mockery::mock('Friendica\Core\Config\Configuration');
$config = \Mockery::mock(Config\Configuration::class);
$config
->shouldReceive('get')
->with('system', 'maintenance', null, false)

View file

@ -31,8 +31,7 @@ class BaseObjectTest extends TestCase
{
$baseObject = new BaseObject();
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->assertNull($baseObject->setApp($this->app));
$this->assertEquals($this->app, $baseObject->getApp());

View file

@ -67,8 +67,7 @@ abstract class CacheTest extends MockedTest
protected function setUp()
{
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');

View file

@ -12,14 +12,14 @@ class MemcacheCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_host', NULL, false)
->with('system', 'memcache_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_port', NULL, false)
->with('system', 'memcache_port')
->andReturn(11211);
$this->cache = CacheDriverFactory::create('memcache');

View file

@ -12,9 +12,9 @@ class MemcachedCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcached_hosts', NULL, false)
->with('system', 'memcached_hosts')
->andReturn([0 => 'localhost, 11211']);
$this->cache = CacheDriverFactory::create('memcached');

View file

@ -12,14 +12,14 @@ class RedisCacheDriverTest extends MemoryCacheTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_host', NULL, false)
->with('system', 'redis_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_port', NULL, false)
->with('system', 'redis_port')
->andReturn(null);
$this->cache = CacheDriverFactory::create('redis');

View file

@ -2,6 +2,7 @@
namespace Friendica\Test\Core\Config;
use Friendica\Core\Config\Adapter\IConfigAdapter;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Config\Configuration;
@ -29,7 +30,7 @@ class ConfigurationTest extends MockedTest
public function testSetUp()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
$configuration = new Configuration($configCache, $configAdapter);
@ -43,7 +44,7 @@ class ConfigurationTest extends MockedTest
public function testCacheLoad()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
@ -64,7 +65,7 @@ class ConfigurationTest extends MockedTest
public function testCacheLoadDouble()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
@ -93,7 +94,7 @@ class ConfigurationTest extends MockedTest
public function testSetGetWithoutDB($data)
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
$configuration = new Configuration($configCache, $configAdapter);
@ -111,7 +112,7 @@ class ConfigurationTest extends MockedTest
public function testSetGetWithDB($data)
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
@ -132,7 +133,7 @@ class ConfigurationTest extends MockedTest
public function testGetWrongWithoutDB()
{
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new Configuration($configCache, $configAdapter);
@ -157,7 +158,7 @@ class ConfigurationTest extends MockedTest
public function testGetWithRefresh($data)
{
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
@ -188,7 +189,7 @@ class ConfigurationTest extends MockedTest
public function testGetWithoutLoaded($data)
{
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();
@ -223,7 +224,7 @@ class ConfigurationTest extends MockedTest
public function testDeleteWithoutDB($data)
{
$configCache = new ConfigCache(['test' => ['it' => $data]]);
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new Configuration($configCache, $configAdapter);
@ -244,7 +245,7 @@ class ConfigurationTest extends MockedTest
public function testDeleteWithDB()
{
$configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
$configAdapter = \Mockery::mock(IConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
// constructor loading
$configAdapter->shouldReceive('load')->andReturn([])->once();

View file

@ -2,6 +2,7 @@
namespace Friendica\Test\Core\Config;
use Friendica\Core\Config\Adapter\IPConfigAdapter;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\PConfiguration;
use Friendica\Test\MockedTest;
@ -29,7 +30,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
// expected loading
$configAdapter->shouldReceive('load')
@ -51,7 +52,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
// expected loading
$configAdapter->shouldReceive('load')->with($uid, 'testing')->andReturn(['testing' => ['test' => 'it']])->once();
@ -77,7 +78,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
$configuration = new PConfiguration($configCache, $configAdapter);
@ -95,7 +96,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(2);
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', $data)->andReturn(true)->once();
@ -114,7 +115,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
$configuration = new PConfiguration($configCache, $configAdapter);
@ -137,7 +138,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
@ -168,7 +169,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
@ -199,7 +200,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
$configuration = new PConfiguration($configCache, $configAdapter);
@ -218,7 +219,7 @@ class PConfigurationTest extends MockedTest
{
$uid = 234;
$configCache = new ConfigCache();
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
$configAdapter = \Mockery::mock(IPConfigAdapter::class);
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
$configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();

View file

@ -52,9 +52,9 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
$this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
$this->db_pass = getenv('MYSQL_PASSWORD');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'php_path', NULL, false)
->with('config', 'php_path')
->andReturn(false);
$this->mockL10nT();

View file

@ -32,14 +32,14 @@ class ConfigConsoleTest extends ConsoleTest
}
function testSetGetKeyValue() {
$this->configCache
$this->configMock
->shouldReceive('set')
->with('config', 'test', 'now')
->andReturn(true)
->once();
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', NULL, false)
->with('config', 'test')
->andReturn('now')
->twice();
@ -50,9 +50,9 @@ class ConfigConsoleTest extends ConsoleTest
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test <= now\n", $txt);
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null, false)
->with('config', 'test')
->andReturn('now')
->once();
@ -62,9 +62,9 @@ class ConfigConsoleTest extends ConsoleTest
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test => now\n", $txt);
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null, false)
->with('config', 'test')
->andReturn(null)
->once();
@ -77,9 +77,9 @@ class ConfigConsoleTest extends ConsoleTest
function testSetArrayValue() {
$testArray = [1, 2, 3];
$this->configCache
$this->configMock
->shouldReceive('get')
->with('config', 'test', null, false)
->with('config', 'test')
->andReturn($testArray)
->once();
@ -105,9 +105,9 @@ class ConfigConsoleTest extends ConsoleTest
}
function testVerbose() {
$this->configCache
$this->configMock
->shouldReceive('get')
->with('test', 'it', null, false)
->with('test', 'it')
->andReturn('now')
->once();
$console = new Config($this->consoleArgv);
@ -133,14 +133,14 @@ CONF;
}
function testUnableToSet() {
$this->configCache
$this->configMock
->shouldReceive('set')
->with('test', 'it', 'now')
->andReturn(false)
->once();
$this->configCache
$this->configMock
->shouldReceive('get')
->with('test', 'it', NULL, false)
->with('test', 'it')
->andReturn(NULL)
->once();
$console = new Config();

View file

@ -3,12 +3,10 @@
namespace Friendica\Test\src\Core\Console;
use Asika\SimpleConsole\Console;
use Friendica\Core\Config\Configuration;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\Intercept;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Profiler;
abstract class ConsoleTest extends MockedTest
{
@ -31,10 +29,7 @@ abstract class ConsoleTest extends MockedTest
Intercept::setUp();
$this->setUpVfsDir();
$configMock = \Mockery::mock(Configuration::class);
$this->mockApp($this->root, $configMock);
$profileMock = \Mockery::mock(Profiler::class);
$this->app->shouldReceive('getProfiler')->andReturn($profileMock);
$this->mockApp($this->root);
}
/**

View file

@ -27,8 +27,7 @@ abstract class LockTest extends MockedTest
{
// Reusable App object
$this->setUpVfsDir();
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
$this->mockApp($this->root, $configMock);
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');

View file

@ -13,14 +13,14 @@ class MemcacheCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_host', NULL, false)
->with('system', 'memcache_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcache_port', NULL, false)
->with('system', 'memcache_port')
->andReturn(11211);
return new CacheLockDriver(CacheDriverFactory::create('memcache'));

View file

@ -13,9 +13,9 @@ class MemcachedCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'memcached_hosts', NULL, false)
->with('system', 'memcached_hosts')
->andReturn([0 => 'localhost, 11211']);
return new CacheLockDriver(CacheDriverFactory::create('memcached'));

View file

@ -13,14 +13,14 @@ class RedisCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_host', NULL, false)
->with('system', 'redis_host')
->andReturn('localhost');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'redis_port', NULL, false)
->with('system', 'redis_port')
->andReturn(null);
return new CacheLockDriver(CacheDriverFactory::create('redis'));

View file

@ -12,9 +12,9 @@ class SemaphoreLockDriverTest extends LockTest
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
$this->configCache
$this->configMock
->shouldReceive('get')
->with('system', 'temppath', NULL, false)
->with('system', 'temppath')
->andReturn('/tmp/');
}