]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
Merge pull request #1 from friendica/develop
[friendica.git] / tests / Util / AppMockTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Render\FriendicaSmartyEngine;
29 use Friendica\Util\Profiler;
30 use Mockery\MockInterface;
31 use org\bovigo\vfs\vfsStreamDirectory;
32
33 /**
34  * Trait to Mock the global App instance
35  */
36 trait AppMockTrait
37 {
38         /**
39          * @var MockInterface|App The mocked Friendica\App
40          */
41         protected $app;
42
43         /**
44          * @var MockInterface|Config\IConfig The mocked Config Cache
45          */
46         protected $configMock;
47
48         /**
49          * @var MockInterface|Profiler The mocked profiler
50          */
51         protected $profilerMock;
52
53         /**
54          * @var MockInterface|App\Mode The mocked App mode
55          */
56         protected $mode;
57
58         /**
59          * @var MockInterface|Dice The dependency injection library
60          */
61         protected $dice;
62
63         /**
64          * Mock the App
65          *
66          * @param vfsStreamDirectory $root The root directory
67          * @param bool $raw If true, no config mocking will be done
68          */
69         public function mockApp(vfsStreamDirectory $root, $raw = false)
70         {
71                 $this->dice = \Mockery::mock(Dice::class)->makePartial();
72                 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
73
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);
85
86                 $config = new Config\JitConfig($this->configMock, $configModel);
87                 $this->dice->shouldReceive('create')
88                            ->with(Config\IConfig::class)
89                            ->andReturn($config);
90
91                 // Mocking App and most used functions
92                 $this->app = \Mockery::mock(App::class);
93                 $this->dice->shouldReceive('create')
94                            ->with(App::class)
95                            ->andReturn($this->app);
96                 $this->app
97                         ->shouldReceive('getBasePath')
98                         ->andReturn($root->url());
99
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);
105
106                 $this->app
107                         ->shouldReceive('getConfigCache')
108                         ->andReturn($this->configMock);
109                 $this->app
110                         ->shouldReceive('getTemplateEngine')
111                         ->andReturn(new FriendicaSmartyEngine('frio', []));
112                 $this->app
113                         ->shouldReceive('getCurrentTheme')
114                         ->andReturn('Smarty3');
115
116                 DI::init($this->dice);
117
118                 if ($raw) {
119                         return;
120                 }
121
122                 $this->configMock
123                         ->shouldReceive('has')
124                         ->andReturn(true);
125                 $this->configMock
126                         ->shouldReceive('get')
127                         ->with('database', 'hostname')
128                         ->andReturn(getenv('MYSQL_HOST'));
129                 $this->configMock
130                         ->shouldReceive('get')
131                         ->with('database', 'username')
132                         ->andReturn(getenv('MYSQL_USERNAME'));
133                 $this->configMock
134                         ->shouldReceive('get')
135                         ->with('database', 'password')
136                         ->andReturn(getenv('MYSQL_PASSWORD'));
137                 $this->configMock
138                         ->shouldReceive('get')
139                         ->with('database', 'database')
140                         ->andReturn(getenv('MYSQL_DATABASE'));
141                 $this->configMock
142                         ->shouldReceive('get')
143                         ->with('config', 'hostname')
144                         ->andReturn('localhost');
145                 $this->configMock
146                         ->shouldReceive('get')
147                         ->with('system', 'theme')
148                         ->andReturn('system_theme');
149         }
150 }