]> git.mxchange.org Git - friendica.git/blob - AppMockTrait.php
23920ff6f434d30f666472f8011184ed3aebe951
[friendica.git] / 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          * @param Config\Cache\ConfigCache $configCache
43          * @param bool $raw If true, no config mocking will be done
44          */
45         public function mockApp(vfsStreamDirectory $root, $configCache = null, $raw = false)
46         {
47                 $this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
48                 $this->mode = \Mockery::mock(App\Mode::class);
49                 $configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
50                 // Disable the adapter
51                 $configAdapterMock->shouldReceive('isConnected')->andReturn(false);
52
53                 $config = new Config\Configuration((isset($configCache) ? $configCache : $this->configMock), $configAdapterMock);
54                 // Initialize empty Config
55                 Config::init($config);
56
57                 // Mocking App and most used functions
58                 $this->app = \Mockery::mock(App::class);
59                 $this->app
60                         ->shouldReceive('getBasePath')
61                         ->andReturn($root->url());
62
63                 $this->app
64                         ->shouldReceive('getMode')
65                         ->andReturn($this->mode);
66
67                 $this->profilerMock = \Mockery::mock(Profiler::class);
68                 $this->profilerMock->shouldReceive('saveTimestamp');
69
70                 $this->app
71                         ->shouldReceive('getConfigCache')
72                         ->andReturn((isset($configCache) ? $configCache : $this->configMock));
73                 $this->app
74                         ->shouldReceive('getTemplateEngine')
75                         ->andReturn(new FriendicaSmartyEngine());
76                 $this->app
77                         ->shouldReceive('getCurrentTheme')
78                         ->andReturn('Smarty3');
79                 $this->app
80                         ->shouldReceive('getProfiler')
81                         ->andReturn($this->profilerMock);
82                 $this->app
83                         ->shouldReceive('getBaseUrl')
84                         ->andReturnUsing(function () {
85                                 return $this->app->getConfigCache()->get('system', 'url');
86                         });
87
88                 BaseObject::setApp($this->app);
89
90                 if ($raw) {
91                         return;
92                 }
93
94                 $this->configMock
95                         ->shouldReceive('has')
96                         ->andReturn(true);
97                 $this->configMock
98                         ->shouldReceive('get')
99                         ->with('database', 'hostname')
100                         ->andReturn(getenv('MYSQL_HOST'));
101                 $this->configMock
102                         ->shouldReceive('get')
103                         ->with('database', 'username')
104                         ->andReturn(getenv('MYSQL_USERNAME'));
105                 $this->configMock
106                         ->shouldReceive('get')
107                         ->with('database', 'password')
108                         ->andReturn(getenv('MYSQL_PASSWORD'));
109                 $this->configMock
110                         ->shouldReceive('get')
111                         ->with('database', 'database')
112                         ->andReturn(getenv('MYSQL_DATABASE'));
113                 $this->configMock
114                         ->shouldReceive('get')
115                         ->with('config', 'hostname')
116                         ->andReturn('localhost');
117                 $this->configMock
118                         ->shouldReceive('get')
119                         ->with('system', 'theme')
120                         ->andReturn('system_theme');
121         }
122 }