3 namespace Friendica\Test\Util;
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Render\FriendicaSmartyEngine;
10 use Friendica\Util\Profiler;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStreamDirectory;
15 * Trait to Mock the global App instance
20 * @var MockInterface|App The mocked Friendica\App
25 * @var MockInterface|Config\Configuration The mocked Config Cache
27 protected $configMock;
30 * @var MockInterface|Profiler The mocked profiler
32 protected $profilerMock;
35 * @var MockInterface|App\Mode The mocked App mode
40 * @var MockInterface|Dice The dependency injection library
47 * @param vfsStreamDirectory $root The root directory
48 * @param bool $raw If true, no config mocking will be done
50 public function mockApp(vfsStreamDirectory $root, $raw = false)
52 $this->dice = \Mockery::mock(Dice::class)->makePartial();
53 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
55 $this->configMock = \Mockery::mock(Config\Cache\ConfigCache::class);
56 $this->dice->shouldReceive('create')
57 ->with(Config\Cache\ConfigCache::class)
58 ->andReturn($this->configMock);
59 $this->mode = \Mockery::mock(App\Mode::class);
60 $this->dice->shouldReceive('create')
61 ->with(App\Mode::class)
62 ->andReturn($this->mode);
63 $configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
64 // Disable the adapter
65 $configModel->shouldReceive('isConnected')->andReturn(false);
67 $config = new Config\JitConfiguration($this->configMock, $configModel);
68 $this->dice->shouldReceive('create')
69 ->with(Config\Configuration::class)
72 // Mocking App and most used functions
73 $this->app = \Mockery::mock(App::class);
74 $this->dice->shouldReceive('create')
76 ->andReturn($this->app);
78 ->shouldReceive('getBasePath')
79 ->andReturn($root->url());
82 ->shouldReceive('getMode')
83 ->andReturn($this->mode);
85 $this->profilerMock = \Mockery::mock(Profiler::class);
86 $this->profilerMock->shouldReceive('saveTimestamp');
87 $this->dice->shouldReceive('create')
88 ->with(Profiler::class)
89 ->andReturn($this->profilerMock);
92 ->shouldReceive('getConfigCache')
93 ->andReturn($this->configMock);
95 ->shouldReceive('getConfig')
98 ->shouldReceive('getTemplateEngine')
99 ->andReturn(new FriendicaSmartyEngine());
101 ->shouldReceive('getCurrentTheme')
102 ->andReturn('Smarty3');
104 ->shouldReceive('getProfiler')
105 ->andReturn($this->profilerMock);
107 ->shouldReceive('getBaseUrl')
108 ->andReturnUsing(function () {
109 return $this->configMock->get('system', 'url');
112 BaseObject::setDependencyInjection($this->dice);
119 ->shouldReceive('has')
122 ->shouldReceive('get')
123 ->with('database', 'hostname')
124 ->andReturn(getenv('MYSQL_HOST'));
126 ->shouldReceive('get')
127 ->with('database', 'username')
128 ->andReturn(getenv('MYSQL_USERNAME'));
130 ->shouldReceive('get')
131 ->with('database', 'password')
132 ->andReturn(getenv('MYSQL_PASSWORD'));
134 ->shouldReceive('get')
135 ->with('database', 'database')
136 ->andReturn(getenv('MYSQL_DATABASE'));
138 ->shouldReceive('get')
139 ->with('config', 'hostname')
140 ->andReturn('localhost');
142 ->shouldReceive('get')
143 ->with('system', 'theme')
144 ->andReturn('system_theme');