]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
Merge pull request #10748 from MrPetovan/task/10740-json-ld-debug
[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('startRecording');
105                 $this->profilerMock->shouldReceive('stopRecording');
106                 $this->profilerMock->shouldReceive('saveTimestamp');
107                 $this->dice->shouldReceive('create')
108                            ->with(Profiler::class)
109                            ->andReturn($this->profilerMock);
110
111                 $this->app
112                         ->shouldReceive('getConfigCache')
113                         ->andReturn($this->configMock);
114                 $this->app
115                         ->shouldReceive('getTemplateEngine')
116                         ->andReturn(new FriendicaSmartyEngine('frio', []));
117                 $this->app
118                         ->shouldReceive('getCurrentTheme')
119                         ->andReturn('Smarty3');
120                 $this->app->shouldReceive('getThemeInfoValue')
121                         ->with('videowidth')
122                         ->andReturn(425);
123                 $this->app->shouldReceive('getThemeInfoValue')
124                         ->with('videoheight')
125                         ->andReturn(350);
126         
127                 DI::init($this->dice);
128
129                 if ($raw) {
130                         return;
131                 }
132
133                 $this->configMock
134                         ->shouldReceive('has')
135                         ->andReturn(true);
136                 $this->configMock
137                         ->shouldReceive('get')
138                         ->with('database', 'hostname')
139                         ->andReturn(getenv('MYSQL_HOST'));
140                 $this->configMock
141                         ->shouldReceive('get')
142                         ->with('database', 'username')
143                         ->andReturn(getenv('MYSQL_USERNAME'));
144                 $this->configMock
145                         ->shouldReceive('get')
146                         ->with('database', 'password')
147                         ->andReturn(getenv('MYSQL_PASSWORD'));
148                 $this->configMock
149                         ->shouldReceive('get')
150                         ->with('database', 'database')
151                         ->andReturn(getenv('MYSQL_DATABASE'));
152                 $this->configMock
153                         ->shouldReceive('get')
154                         ->with('config', 'hostname')
155                         ->andReturn('localhost');
156                 $this->configMock
157                         ->shouldReceive('get')
158                         ->with('system', 'theme')
159                         ->andReturn('system_theme');
160         }
161 }