]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
Merge pull request #10548 from annando/args
[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                 $this->app->shouldReceive('getThemeInfoValue')
119                         ->with('videowidth')
120                         ->andReturn(425);
121                 $this->app->shouldReceive('getThemeInfoValue')
122                         ->with('videoheight')
123                         ->andReturn(350);
124         
125                 DI::init($this->dice);
126
127                 if ($raw) {
128                         return;
129                 }
130
131                 $this->configMock
132                         ->shouldReceive('has')
133                         ->andReturn(true);
134                 $this->configMock
135                         ->shouldReceive('get')
136                         ->with('database', 'hostname')
137                         ->andReturn(getenv('MYSQL_HOST'));
138                 $this->configMock
139                         ->shouldReceive('get')
140                         ->with('database', 'username')
141                         ->andReturn(getenv('MYSQL_USERNAME'));
142                 $this->configMock
143                         ->shouldReceive('get')
144                         ->with('database', 'password')
145                         ->andReturn(getenv('MYSQL_PASSWORD'));
146                 $this->configMock
147                         ->shouldReceive('get')
148                         ->with('database', 'database')
149                         ->andReturn(getenv('MYSQL_DATABASE'));
150                 $this->configMock
151                         ->shouldReceive('get')
152                         ->with('config', 'hostname')
153                         ->andReturn('localhost');
154                 $this->configMock
155                         ->shouldReceive('get')
156                         ->with('system', 'theme')
157                         ->andReturn('system_theme');
158         }
159 }