]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
Merge pull request #7643 from nupplaphil/task/add_drone_ci
[friendica.git] / tests / Util / AppMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Dice\Dice;
6 use Friendica\App;
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Render\FriendicaSmartyEngine;
10 use Friendica\Util\Profiler;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStreamDirectory;
13
14 /**
15  * Trait to Mock the global App instance
16  */
17 trait AppMockTrait
18 {
19         /**
20          * @var MockInterface|App The mocked Friendica\App
21          */
22         protected $app;
23
24         /**
25          * @var MockInterface|Config\Configuration The mocked Config Cache
26          */
27         protected $configMock;
28
29         /**
30          * @var MockInterface|Profiler The mocked profiler
31          */
32         protected $profilerMock;
33
34         /**
35          * @var MockInterface|App\Mode The mocked App mode
36          */
37         protected $mode;
38
39         /**
40          * @var MockInterface|Dice The dependency injection library
41          */
42         protected $dice;
43
44         /**
45          * Mock the App
46          *
47          * @param vfsStreamDirectory $root The root directory
48          * @param bool $raw If true, no config mocking will be done
49          */
50         public function mockApp(vfsStreamDirectory $root, $raw = false)
51         {
52                 $this->dice = \Mockery::mock(Dice::class)->makePartial();
53                 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
54
55                 $this->configMock = \Mockery::mock(Config\Cache\ConfigCache::class);
56                 $this->dice->shouldReceive('create')
57                            ->with(Config\Cache\ConfigCache::class)
58                            ->andReturn($this->configMock);
59                 $this->mode = \Mockery::mock(App\Mode::class);
60                 $this->dice->shouldReceive('create')
61                            ->with(App\Mode::class)
62                            ->andReturn($this->mode);
63                 $configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
64                 // Disable the adapter
65                 $configModel->shouldReceive('isConnected')->andReturn(false);
66
67                 $config = new Config\JitConfiguration($this->configMock, $configModel);
68                 $this->dice->shouldReceive('create')
69                            ->with(Config\Configuration::class)
70                            ->andReturn($config);
71
72                 // Mocking App and most used functions
73                 $this->app = \Mockery::mock(App::class);
74                 $this->dice->shouldReceive('create')
75                            ->with(App::class)
76                            ->andReturn($this->app);
77                 $this->app
78                         ->shouldReceive('getBasePath')
79                         ->andReturn($root->url());
80
81                 $this->app
82                         ->shouldReceive('getMode')
83                         ->andReturn($this->mode);
84
85                 $this->profilerMock = \Mockery::mock(Profiler::class);
86                 $this->profilerMock->shouldReceive('saveTimestamp');
87                 $this->dice->shouldReceive('create')
88                            ->with(Profiler::class)
89                            ->andReturn($this->profilerMock);
90
91                 $this->app
92                         ->shouldReceive('getConfigCache')
93                         ->andReturn($this->configMock);
94                 $this->app
95                         ->shouldReceive('getConfig')
96                         ->andReturn($config);
97                 $this->app
98                         ->shouldReceive('getTemplateEngine')
99                         ->andReturn(new FriendicaSmartyEngine());
100                 $this->app
101                         ->shouldReceive('getCurrentTheme')
102                         ->andReturn('Smarty3');
103                 $this->app
104                         ->shouldReceive('getProfiler')
105                         ->andReturn($this->profilerMock);
106                 $this->app
107                         ->shouldReceive('getBaseUrl')
108                         ->andReturnUsing(function () {
109                                 return $this->configMock->get('system', 'url');
110                         });
111
112                 BaseObject::setDependencyInjection($this->dice);
113
114                 if ($raw) {
115                         return;
116                 }
117
118                 $this->configMock
119                         ->shouldReceive('has')
120                         ->andReturn(true);
121                 $this->configMock
122                         ->shouldReceive('get')
123                         ->with('database', 'hostname')
124                         ->andReturn(getenv('MYSQL_HOST'));
125                 $this->configMock
126                         ->shouldReceive('get')
127                         ->with('database', 'username')
128                         ->andReturn(getenv('MYSQL_USERNAME'));
129                 $this->configMock
130                         ->shouldReceive('get')
131                         ->with('database', 'password')
132                         ->andReturn(getenv('MYSQL_PASSWORD'));
133                 $this->configMock
134                         ->shouldReceive('get')
135                         ->with('database', 'database')
136                         ->andReturn(getenv('MYSQL_DATABASE'));
137                 $this->configMock
138                         ->shouldReceive('get')
139                         ->with('config', 'hostname')
140                         ->andReturn('localhost');
141                 $this->configMock
142                         ->shouldReceive('get')
143                         ->with('system', 'theme')
144                         ->andReturn('system_theme');
145         }
146 }