]> git.mxchange.org Git - friendica.git/blob - tests/Util/AppMockTrait.php
add missing namespaces/fix wrong class-names
[friendica.git] / tests / Util / AppMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Dice\Dice;
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\DI;
9 use Friendica\Render\FriendicaSmartyEngine;
10 use Friendica\Util\Profiler;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStreamDirectory;
13
14 /**
15  * Trait to Mock the global App instance
16  */
17 trait AppMockTrait
18 {
19         /**
20          * @var MockInterface|App The mocked Friendica\App
21          */
22         protected $app;
23
24         /**
25          * @var MockInterface|Config\IConfiguration The mocked Config Cache
26          */
27         protected $configMock;
28
29         /**
30          * @var MockInterface|Profiler The mocked profiler
31          */
32         protected $profilerMock;
33
34         /**
35          * @var MockInterface|App\Mode The mocked App mode
36          */
37         protected $mode;
38
39         /**
40          * @var MockInterface|Dice The dependency injection library
41          */
42         protected $dice;
43
44         /**
45          * Mock the App
46          *
47          * @param vfsStreamDirectory $root The root directory
48          * @param bool $raw If true, no config mocking will be done
49          */
50         public function mockApp(vfsStreamDirectory $root, $raw = false)
51         {
52                 $this->dice = \Mockery::mock(Dice::class)->makePartial();
53                 $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php');
54
55                 $this->configMock = \Mockery::mock(Config\Cache\ConfigCache::class);
56                 $this->dice->shouldReceive('create')
57                            ->with(Config\Cache\ConfigCache::class)
58                            ->andReturn($this->configMock);
59                 $this->mode = \Mockery::mock(App\Mode::class);
60                 $this->dice->shouldReceive('create')
61                            ->with(App\Mode::class)
62                            ->andReturn($this->mode);
63                 $configModel= \Mockery::mock(\Friendica\Model\Config\Config::class);
64                 // Disable the adapter
65                 $configModel->shouldReceive('isConnected')->andReturn(false);
66
67                 $config = new Config\JitConfiguration($this->configMock, $configModel);
68                 $this->dice->shouldReceive('create')
69                            ->with(Config\IConfiguration::class)
70                            ->andReturn($config);
71
72                 // Mocking App and most used functions
73                 $this->app = \Mockery::mock(App::class);
74                 $this->dice->shouldReceive('create')
75                            ->with(App::class)
76                            ->andReturn($this->app);
77                 $this->app
78                         ->shouldReceive('getBasePath')
79                         ->andReturn($root->url());
80
81                 $this->profilerMock = \Mockery::mock(Profiler::class);
82                 $this->profilerMock->shouldReceive('saveTimestamp');
83                 $this->dice->shouldReceive('create')
84                            ->with(Profiler::class)
85                            ->andReturn($this->profilerMock);
86
87                 $this->app
88                         ->shouldReceive('getConfigCache')
89                         ->andReturn($this->configMock);
90                 $this->app
91                         ->shouldReceive('getTemplateEngine')
92                         ->andReturn(new FriendicaSmartyEngine());
93                 $this->app
94                         ->shouldReceive('getCurrentTheme')
95                         ->andReturn('Smarty3');
96
97                 DI::init($this->dice);
98
99                 if ($raw) {
100                         return;
101                 }
102
103                 $this->configMock
104                         ->shouldReceive('has')
105                         ->andReturn(true);
106                 $this->configMock
107                         ->shouldReceive('get')
108                         ->with('database', 'hostname')
109                         ->andReturn(getenv('MYSQL_HOST'));
110                 $this->configMock
111                         ->shouldReceive('get')
112                         ->with('database', 'username')
113                         ->andReturn(getenv('MYSQL_USERNAME'));
114                 $this->configMock
115                         ->shouldReceive('get')
116                         ->with('database', 'password')
117                         ->andReturn(getenv('MYSQL_PASSWORD'));
118                 $this->configMock
119                         ->shouldReceive('get')
120                         ->with('database', 'database')
121                         ->andReturn(getenv('MYSQL_DATABASE'));
122                 $this->configMock
123                         ->shouldReceive('get')
124                         ->with('config', 'hostname')
125                         ->andReturn('localhost');
126                 $this->configMock
127                         ->shouldReceive('get')
128                         ->with('system', 'theme')
129                         ->andReturn('system_theme');
130         }
131 }