Add type-hint in ActivityPub\Receiver::fetchObject to catch wrong type coercion
[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\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('getConfig')
74                         ->andReturn($config);
75                 $this->app
76                         ->shouldReceive('getTemplateEngine')
77                         ->andReturn(new FriendicaSmartyEngine());
78                 $this->app
79                         ->shouldReceive('getCurrentTheme')
80                         ->andReturn('Smarty3');
81                 $this->app
82                         ->shouldReceive('getProfiler')
83                         ->andReturn($this->profilerMock);
84                 $this->app
85                         ->shouldReceive('getBaseUrl')
86                         ->andReturnUsing(function () {
87                                 return $this->configMock->get('system', 'url');
88                         });
89
90                 BaseObject::setApp($this->app);
91
92                 if ($raw) {
93                         return;
94                 }
95
96                 $this->configMock
97                         ->shouldReceive('has')
98                         ->andReturn(true);
99                 $this->configMock
100                         ->shouldReceive('get')
101                         ->with('database', 'hostname')
102                         ->andReturn(getenv('MYSQL_HOST'));
103                 $this->configMock
104                         ->shouldReceive('get')
105                         ->with('database', 'username')
106                         ->andReturn(getenv('MYSQL_USERNAME'));
107                 $this->configMock
108                         ->shouldReceive('get')
109                         ->with('database', 'password')
110                         ->andReturn(getenv('MYSQL_PASSWORD'));
111                 $this->configMock
112                         ->shouldReceive('get')
113                         ->with('database', 'database')
114                         ->andReturn(getenv('MYSQL_DATABASE'));
115                 $this->configMock
116                         ->shouldReceive('get')
117                         ->with('config', 'hostname')
118                         ->andReturn('localhost');
119                 $this->configMock
120                         ->shouldReceive('get')
121                         ->with('system', 'theme')
122                         ->andReturn('system_theme');
123         }
124 }