Merge pull request #6611 from annando/worker-performance
[friendica.git/.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\Core\Config\ConfigCache;
9 use Friendica\Render\FriendicaSmartyEngine;
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|ConfigCache The mocked Config Cache
25          */
26         protected $configCache;
27
28         /**
29          * Mock the App
30          *
31          * @param vfsStreamDirectory $root The root directory
32          * @param MockInterface|ConfigCache $config The config cache
33          */
34         public function mockApp($root, $config)
35         {
36                 $this->configCache = $config;
37                 // Mocking App and most used functions
38                 $this->app = \Mockery::mock(App::class);
39                 $this->app
40                         ->shouldReceive('getBasePath')
41                         ->andReturn($root->url());
42
43                 $config
44                         ->shouldReceive('get')
45                         ->with('database', 'hostname')
46                         ->andReturn(getenv('MYSQL_HOST'));
47                 $config
48                         ->shouldReceive('get')
49                         ->with('database', 'username')
50                         ->andReturn(getenv('MYSQL_USERNAME'));
51                 $config
52                         ->shouldReceive('get')
53                         ->with('database', 'password')
54                         ->andReturn(getenv('MYSQL_PASSWORD'));
55                 $config
56                         ->shouldReceive('get')
57                         ->with('database', 'database')
58                         ->andReturn(getenv('MYSQL_DATABASE'));
59                 $config
60                         ->shouldReceive('get')
61                         ->with('config', 'hostname')
62                         ->andReturn('localhost');
63                 $config
64                         ->shouldReceive('get')
65                         ->with('system', 'theme', NULL)
66                         ->andReturn('system_theme');
67
68                 $this->app
69                         ->shouldReceive('getConfig')
70                         ->andReturn($config);
71
72                 $this->app
73                         ->shouldReceive('getTemplateEngine')
74                         ->andReturn(new FriendicaSmartyEngine());
75                 $this->app
76                         ->shouldReceive('getCurrentTheme')
77                         ->andReturn('Smarty3');
78                 $this->app
79                         ->shouldReceive('saveTimestamp')
80                         ->andReturn(true);
81                 $this->app
82                         ->shouldReceive('getBaseUrl')
83                         ->andReturn('http://friendica.local');
84
85                 // Initialize empty Config
86                 Config::init($config);
87                 $configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
88                 $configAdapter
89                         ->shouldReceive('isConnected')
90                         ->andReturn(false);
91                 Config::setAdapter($configAdapter);
92
93                 BaseObject::setApp($this->app);
94         }
95 }