3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\Util;
26 use Friendica\Core\Config;
28 use Friendica\Network\HTTPException\InternalServerErrorException;
29 use Friendica\Render\FriendicaSmartyEngine;
30 use Friendica\Util\Profiler;
31 use Mockery\MockInterface;
32 use org\bovigo\vfs\vfsStreamDirectory;
35 * Trait to Mock the global App instance
40 * @var MockInterface|App The mocked Friendica\App
45 * @var MockInterface|Config\IConfig The mocked Config Cache
47 protected $configMock;
50 * @var MockInterface|Profiler The mocked profiler
52 protected $profilerMock;
55 * @var MockInterface|App\Mode The mocked App mode
60 * @var MockInterface|Dice The dependency injection library
67 * @param vfsStreamDirectory $root The root directory
68 * @param bool $raw If true, no config mocking will be done
70 * @throws InternalServerErrorException
72 public function mockApp(vfsStreamDirectory $root, $raw = false)
74 $this->dice = \Mockery::mock(Dice::class)->makePartial();
75 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
77 $this->configMock = \Mockery::mock(Config\Cache::class);
78 $this->dice->shouldReceive('create')
79 ->with(Config\Cache::class)
80 ->andReturn($this->configMock);
81 $this->mode = \Mockery::mock(App\Mode::class);
82 $this->dice->shouldReceive('create')
83 ->with(App\Mode::class)
84 ->andReturn($this->mode);
85 $configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
86 // Disable the adapter
87 $configModel->shouldReceive('isConnected')->andReturn(false);
89 $config = new Config\JitConfig($this->configMock, $configModel);
90 $this->dice->shouldReceive('create')
91 ->with(Config\IConfig::class)
94 // Mocking App and most used functions
95 $this->app = \Mockery::mock(App::class);
96 $this->dice->shouldReceive('create')
98 ->andReturn($this->app);
100 ->shouldReceive('getBasePath')
101 ->andReturn($root->url());
103 $this->profilerMock = \Mockery::mock(Profiler::class);
104 $this->profilerMock->shouldReceive('saveTimestamp');
105 $this->dice->shouldReceive('create')
106 ->with(Profiler::class)
107 ->andReturn($this->profilerMock);
110 ->shouldReceive('getConfigCache')
111 ->andReturn($this->configMock);
113 ->shouldReceive('getTemplateEngine')
114 ->andReturn(new FriendicaSmartyEngine('frio', []));
116 ->shouldReceive('getCurrentTheme')
117 ->andReturn('Smarty3');
119 DI::init($this->dice);
126 ->shouldReceive('has')
129 ->shouldReceive('get')
130 ->with('database', 'hostname')
131 ->andReturn(getenv('MYSQL_HOST'));
133 ->shouldReceive('get')
134 ->with('database', 'username')
135 ->andReturn(getenv('MYSQL_USERNAME'));
137 ->shouldReceive('get')
138 ->with('database', 'password')
139 ->andReturn(getenv('MYSQL_PASSWORD'));
141 ->shouldReceive('get')
142 ->with('database', 'database')
143 ->andReturn(getenv('MYSQL_DATABASE'));
145 ->shouldReceive('get')
146 ->with('config', 'hostname')
147 ->andReturn('localhost');
149 ->shouldReceive('get')
150 ->with('system', 'theme')
151 ->andReturn('system_theme');