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