]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
f8d154034670cdb1f789e06e0c593f7151980be6
[friendica.git] / tests / Util / AppMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Friendica\App;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Render\FriendicaSmartyEngine;
9 use Friendica\Util\Profiler;
10 use Mockery\MockInterface;
11 use org\bovigo\vfs\vfsStreamDirectory;
12
13 /**
14  * Trait to Mock the global App instance
15  */
16 trait AppMockTrait
17 {
18         /**
19          * @var MockInterface|App The mocked Friendica\App
20          */
21         protected $app;
22
23         /**
24          * @var MockInterface|Config\Configuration The mocked Config Cache
25          */
26         protected $configMock;
27
28         /**
29          * @var MockInterface|Profiler The mocked profiler
30          */
31         protected $profilerMock;
32
33         /**
34          * @var MockInterface|App\Mode The mocked App mode
35          */
36         protected $mode;
37
38         /**
39          * Mock the App
40          *
41          * @param vfsStreamDirectory $root The root directory
42          */
43         public function mockApp($root)
44         {
45                 $this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
46                 $this->mode = \Mockery::mock(App\Mode::class);
47                 $configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
48                 // Disable the adapter
49                 $configAdapterMock->shouldReceive('isConnected')->andReturn(false);
50
51                 $config = new Config\Configuration($this->configMock, $configAdapterMock);
52                 // Initialize empty Config
53                 Config::init($config);
54
55                 // Mocking App and most used functions
56                 $this->app = \Mockery::mock(App::class);
57                 $this->app
58                         ->shouldReceive('getBasePath')
59                         ->andReturn($root->url());
60
61                 $this->app
62                         ->shouldReceive('getMode')
63                         ->andReturn($this->mode);
64
65                 $this->configMock
66                         ->shouldReceive('has')
67                         ->andReturn(true);
68                 $this->configMock
69                         ->shouldReceive('get')
70                         ->with('database', 'hostname')
71                         ->andReturn(getenv('MYSQL_HOST'));
72                 $this->configMock
73                         ->shouldReceive('get')
74                         ->with('database', 'username')
75                         ->andReturn(getenv('MYSQL_USERNAME'));
76                 $this->configMock
77                         ->shouldReceive('get')
78                         ->with('database', 'password')
79                         ->andReturn(getenv('MYSQL_PASSWORD'));
80                 $this->configMock
81                         ->shouldReceive('get')
82                         ->with('database', 'database')
83                         ->andReturn(getenv('MYSQL_DATABASE'));
84                 $this->configMock
85                         ->shouldReceive('get')
86                         ->with('config', 'hostname')
87                         ->andReturn('localhost');
88                 $this->configMock
89                         ->shouldReceive('get')
90                         ->with('system', 'theme')
91                         ->andReturn('system_theme');
92
93                 $this->profilerMock = \Mockery::mock(Profiler::class);
94                 $this->profilerMock->shouldReceive('saveTimestamp');
95
96                 $this->app
97                         ->shouldReceive('getConfigCache')
98                         ->andReturn($this->configMock);
99                 $this->app
100                         ->shouldReceive('getTemplateEngine')
101                         ->andReturn(new FriendicaSmartyEngine());
102                 $this->app
103                         ->shouldReceive('getCurrentTheme')
104                         ->andReturn('Smarty3');
105                 $this->app
106                         ->shouldReceive('getBaseUrl')
107                         ->andReturn('http://friendica.local');
108                 $this->app
109                         ->shouldReceive('getProfiler')
110                         ->andReturn($this->profilerMock);
111
112                 BaseObject::setApp($this->app);
113         }
114 }