3 * @copyright Copyright (C) 2020, Friendica
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\Render\FriendicaSmartyEngine;
29 use Friendica\Util\Profiler;
30 use Mockery\MockInterface;
31 use org\bovigo\vfs\vfsStreamDirectory;
34 * Trait to Mock the global App instance
39 * @var MockInterface|App The mocked Friendica\App
44 * @var MockInterface|Config\IConfig The mocked Config Cache
46 protected $configMock;
49 * @var MockInterface|Profiler The mocked profiler
51 protected $profilerMock;
54 * @var MockInterface|App\Mode The mocked App mode
59 * @var MockInterface|Dice The dependency injection library
66 * @param vfsStreamDirectory $root The root directory
67 * @param bool $raw If true, no config mocking will be done
69 public function mockApp(vfsStreamDirectory $root, $raw = false)
71 $this->dice = \Mockery::mock(Dice::class)->makePartial();
72 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
74 $this->configMock = \Mockery::mock(Config\Cache::class);
75 $this->dice->shouldReceive('create')
76 ->with(Config\Cache::class)
77 ->andReturn($this->configMock);
78 $this->mode = \Mockery::mock(App\Mode::class);
79 $this->dice->shouldReceive('create')
80 ->with(App\Mode::class)
81 ->andReturn($this->mode);
82 $configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
83 // Disable the adapter
84 $configModel->shouldReceive('isConnected')->andReturn(false);
86 $config = new Config\JitConfig($this->configMock, $configModel);
87 $this->dice->shouldReceive('create')
88 ->with(Config\IConfig::class)
91 // Mocking App and most used functions
92 $this->app = \Mockery::mock(App::class);
93 $this->dice->shouldReceive('create')
95 ->andReturn($this->app);
97 ->shouldReceive('getBasePath')
98 ->andReturn($root->url());
100 $this->profilerMock = \Mockery::mock(Profiler::class);
101 $this->profilerMock->shouldReceive('saveTimestamp');
102 $this->dice->shouldReceive('create')
103 ->with(Profiler::class)
104 ->andReturn($this->profilerMock);
107 ->shouldReceive('getConfigCache')
108 ->andReturn($this->configMock);
110 ->shouldReceive('getTemplateEngine')
111 ->andReturn(new FriendicaSmartyEngine('frio', []));
113 ->shouldReceive('getCurrentTheme')
114 ->andReturn('Smarty3');
116 DI::init($this->dice);
123 ->shouldReceive('has')
126 ->shouldReceive('get')
127 ->with('database', 'hostname')
128 ->andReturn(getenv('MYSQL_HOST'));
130 ->shouldReceive('get')
131 ->with('database', 'username')
132 ->andReturn(getenv('MYSQL_USERNAME'));
134 ->shouldReceive('get')
135 ->with('database', 'password')
136 ->andReturn(getenv('MYSQL_PASSWORD'));
138 ->shouldReceive('get')
139 ->with('database', 'database')
140 ->andReturn(getenv('MYSQL_DATABASE'));
142 ->shouldReceive('get')
143 ->with('config', 'hostname')
144 ->andReturn('localhost');
146 ->shouldReceive('get')
147 ->with('system', 'theme')
148 ->andReturn('system_theme');