]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
Reduce the usage of the "proxifyUrl" function
[friendica.git] / tests / Util / AppMockTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\Util;
23
24 use Dice\Dice;
25 use Friendica\App;
26 use Friendica\Core\Config;
27 use Friendica\DI;
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;
33
34 /**
35  * Trait to Mock the global App instance
36  */
37 trait AppMockTrait
38 {
39         /**
40          * @var MockInterface|App The mocked Friendica\App
41          */
42         protected $app;
43
44         /**
45          * @var MockInterface|Config\IConfig The mocked Config Cache
46          */
47         protected $configMock;
48
49         /**
50          * @var MockInterface|Profiler The mocked profiler
51          */
52         protected $profilerMock;
53
54         /**
55          * @var MockInterface|App\Mode The mocked App mode
56          */
57         protected $mode;
58
59         /**
60          * @var MockInterface|Dice The dependency injection library
61          */
62         protected $dice;
63
64         /**
65          * Mock the App
66          *
67          * @param vfsStreamDirectory $root The root directory
68          * @param bool               $raw  If true, no config mocking will be done
69          *
70          * @throws InternalServerErrorException
71          */
72         public function mockApp(vfsStreamDirectory $root, $raw = false)
73         {
74                 $this->dice = \Mockery::mock(Dice::class)->makePartial();
75                 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
76
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);
88
89                 $config = new Config\JitConfig($this->configMock, $configModel);
90                 $this->dice->shouldReceive('create')
91                            ->with(Config\IConfig::class)
92                            ->andReturn($config);
93
94                 // Mocking App and most used functions
95                 $this->app = \Mockery::mock(App::class);
96                 $this->dice->shouldReceive('create')
97                            ->with(App::class)
98                            ->andReturn($this->app);
99                 $this->app
100                         ->shouldReceive('getBasePath')
101                         ->andReturn($root->url());
102
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);
108
109                 $this->app
110                         ->shouldReceive('getConfigCache')
111                         ->andReturn($this->configMock);
112                 $this->app
113                         ->shouldReceive('getTemplateEngine')
114                         ->andReturn(new FriendicaSmartyEngine('frio', []));
115                 $this->app
116                         ->shouldReceive('getCurrentTheme')
117                         ->andReturn('Smarty3');
118
119                 DI::init($this->dice);
120
121                 if ($raw) {
122                         return;
123                 }
124
125                 $this->configMock
126                         ->shouldReceive('has')
127                         ->andReturn(true);
128                 $this->configMock
129                         ->shouldReceive('get')
130                         ->with('database', 'hostname')
131                         ->andReturn(getenv('MYSQL_HOST'));
132                 $this->configMock
133                         ->shouldReceive('get')
134                         ->with('database', 'username')
135                         ->andReturn(getenv('MYSQL_USERNAME'));
136                 $this->configMock
137                         ->shouldReceive('get')
138                         ->with('database', 'password')
139                         ->andReturn(getenv('MYSQL_PASSWORD'));
140                 $this->configMock
141                         ->shouldReceive('get')
142                         ->with('database', 'database')
143                         ->andReturn(getenv('MYSQL_DATABASE'));
144                 $this->configMock
145                         ->shouldReceive('get')
146                         ->with('config', 'hostname')
147                         ->andReturn('localhost');
148                 $this->configMock
149                         ->shouldReceive('get')
150                         ->with('system', 'theme')
151                         ->andReturn('system_theme');
152         }
153 }