3 namespace Friendica\Test\Util;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Render\FriendicaSmartyEngine;
9 use Friendica\Util\Profiler;
10 use Mockery\MockInterface;
11 use org\bovigo\vfs\vfsStreamDirectory;
14 * Trait to Mock the global App instance
19 * @var MockInterface|App The mocked Friendica\App
24 * @var MockInterface|Config\Configuration The mocked Config Cache
26 protected $configMock;
29 * @var MockInterface|Profiler The mocked profiler
31 protected $profilerMock;
34 * @var MockInterface|App\Mode The mocked App mode
41 * @param vfsStreamDirectory $root The root directory
42 * @param bool $raw If true, no config mocking will be done
44 public function mockApp(vfsStreamDirectory $root, $raw = false)
46 $this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
47 $this->mode = \Mockery::mock(App\Mode::class);
48 $configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
49 // Disable the adapter
50 $configAdapterMock->shouldReceive('isConnected')->andReturn(false);
52 $config = new Config\Configuration($this->configMock, $configAdapterMock);
53 // Initialize empty Config
54 Config::init($config);
56 // Mocking App and most used functions
57 $this->app = \Mockery::mock(App::class);
59 ->shouldReceive('getBasePath')
60 ->andReturn($root->url());
63 ->shouldReceive('getMode')
64 ->andReturn($this->mode);
66 $this->profilerMock = \Mockery::mock(Profiler::class);
67 $this->profilerMock->shouldReceive('saveTimestamp');
70 ->shouldReceive('getConfigCache')
71 ->andReturn($this->configMock);
73 ->shouldReceive('getConfig')
76 ->shouldReceive('getTemplateEngine')
77 ->andReturn(new FriendicaSmartyEngine());
79 ->shouldReceive('getCurrentTheme')
80 ->andReturn('Smarty3');
82 ->shouldReceive('getProfiler')
83 ->andReturn($this->profilerMock);
85 ->shouldReceive('getBaseUrl')
86 ->andReturnUsing(function () {
87 return $this->configMock->get('system', 'url');
90 BaseObject::setApp($this->app);
97 ->shouldReceive('has')
100 ->shouldReceive('get')
101 ->with('database', 'hostname')
102 ->andReturn(getenv('MYSQL_HOST'));
104 ->shouldReceive('get')
105 ->with('database', 'username')
106 ->andReturn(getenv('MYSQL_USERNAME'));
108 ->shouldReceive('get')
109 ->with('database', 'password')
110 ->andReturn(getenv('MYSQL_PASSWORD'));
112 ->shouldReceive('get')
113 ->with('database', 'database')
114 ->andReturn(getenv('MYSQL_DATABASE'));
116 ->shouldReceive('get')
117 ->with('config', 'hostname')
118 ->andReturn('localhost');
120 ->shouldReceive('get')
121 ->with('system', 'theme')
122 ->andReturn('system_theme');