-
Notifications
You must be signed in to change notification settings - Fork 672
/
BaseTestCase.php
293 lines (248 loc) · 6.98 KB
/
BaseTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
*
*/
namespace Elgg;
use Elgg\Database\Seeds\Seedable;
use Elgg\Di\ServiceProvider;
use Elgg\Plugins\PluginTesting;
use Elgg\Project\Paths;
use PHPUnit\Framework\TestCase;
/**
* Base test case abstraction
*/
abstract class BaseTestCase extends TestCase implements Seedable, Testable {
use Testing;
use PluginTesting;
use HookTesting;
static $_instance;
static $_settings;
public function __construct($name = null, array $data = [], $dataName = '') {
parent::__construct($name, $data, $dataName);
self::$_instance = $this;
}
public function __destruct() {
self::$_instance = null;
}
/**
* Build a new testing application
* @return Application|false
*/
public static function createApplication() {
return false;
}
/**
* Returns testing config
* @return Config
*/
public static function getTestingConfig() {
if (!empty($_ENV['ELGG_SETTINGS_FILE'])) {
$settings_path = $_ENV['ELGG_SETTINGS_FILE'];
return Config::factory($settings_path);
}
return new Config([
'dbprefix' => getenv('ELGG_DB_PREFIX') ? : 't_i_elgg_',
'dbname' => getenv('ELGG_DB_NAME') ? : '',
'dbuser' => getenv('ELGG_DB_USER') ? : '',
'dbpass' => getenv('ELGG_DB_PASS') ? : '',
'dbhost' => getenv('ELGG_DB_HOST') ? : 'localhost',
'dbencoding' => getenv('ELGG_DB_ENCODING') ? : 'utf8mb4',
'memcache' => (bool) getenv('ELGG_MEMCACHE'),
'memcache_servers' => [
[getenv('ELGG_MEMCACHE_SERVER1_HOST'), getenv('ELGG_MEMCACHE_SERVER1_PORT')],
[getenv('ELGG_MEMCACHE_SERVER2_HOST'), getenv('ELGG_MEMCACHE_SERVER2_PORT')],
],
'memcache_namespace_prefix' => getenv('ELGG_MEMCACHE_NAMESPACE_PREFIX') ? : 'elgg_mc_prefix_',
// These are fixed, because tests rely on specific location of the dataroot for source files
'wwwroot' => getenv('ELGG_WWWROOT') ? : 'http://localhost/',
'dataroot' => Paths::elgg() . 'engine/tests/test_files/dataroot/',
'cacheroot' => Paths::elgg() . 'engine/tests/test_files/cacheroot/',
'system_cache_enabled' => false,
'simplecache_enabled' => false,
'boot_cache_ttl' => 0,
'profile_files' => [],
'group' => [],
'group_tool_options' => [],
'minusername' => 10,
'profile_custom_fields' => [],
'elgg_maintenance_mode' => false,
'icon_sizes' => [
'topbar' => [
'w' => 16,
'h' => 16,
'square' => true,
'upscale' => true
],
'tiny' => [
'w' => 25,
'h' => 25,
'square' => true,
'upscale' => true
],
'small' => [
'w' => 40,
'h' => 40,
'square' => true,
'upscale' => true
],
'medium' => [
'w' => 100,
'h' => 100,
'square' => true,
'upscale' => true
],
'large' => [
'w' => 200,
'h' => 200,
'square' => true,
'upscale' => true
],
'master' => [
'w' => 2048,
'h' => 2048,
'square' => false,
'upscale' => false
],
],
'debug' => 'NOTICE',
]);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
Application::setInstance(null);
$app = static::createApplication();
if (!$app) {
$this->markTestSkipped();
}
$dt = new \DateTime();
$app->_services->entityTable->setCurrentTime($dt);
$app->_services->metadataTable->setCurrentTime($dt);
$app->_services->relationshipsTable->setCurrentTime($dt);
$app->_services->annotationsTable->setCurrentTime($dt);
$app->_services->usersTable->setCurrentTime($dt);
$app->_services->session->removeLoggedInUser();
$app->_services->session->setIgnoreAccess(false);
access_show_hidden_entities(false);
// Make sure the application has been bootstrapped correctly
$this->assertInstanceOf(Application::class, elgg(), __METHOD__ . ': Elgg not bootstrapped');
$this->assertInstanceOf(ServiceProvider::class, $app->_services, __METHOD__ . ': ServiceProvider not bootstrapped');
$this->assertInstanceOf(Config::class, $app->_services->config, __METHOD__ . ': Config not bootstrapped');
}
/**
* {@inheritdoc}
*/
protected function tearDown() {
// We do not want overflowing ignored access
$this->assertFalse((bool) elgg_get_ignore_access(), __METHOD__ . ': ignore acces not reset');
// We do not want overflowing show hidden status
$this->assertFalse((bool) access_get_show_hidden_status(), __METHOD__ . ': hidden entities not reset');
// Tests should run without a logged in user
$this->assertFalse((bool) elgg_is_logged_in(), __METHOD__ . ': there should be no logged in user');
}
/**
* Called after setUp() method and can be used by test cases to setup their test logic
* @return mixed
*/
abstract function up();
/**
* Called before tearDown() method and can be used by test cases to clear their test logic
* @return mixed
*/
abstract function down();
/**
* @source https://gist.github.com/gnutix/7746893
* @return \Doctrine\DBAL\Platforms\AbstractPlatform|\PHPUnit_Framework_MockObject_MockObject
*/
public function getDatabasePlatformMock() {
$mock = $this->getAbstractMock(
'Doctrine\DBAL\Platforms\AbstractPlatform',
[
'getName',
'getTruncateTableSQL',
]
);
$mock->expects($this->any())
->method('getName')
->will($this->returnValue('mysql'));
$mock->expects($this->any())
->method('getTruncateTableSQL')
->with($this->anything())
->will($this->returnValue('#TRUNCATE {table}'));
return $mock;
}
/**
* @source https://gist.github.com/gnutix/7746893
* @return \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject
*/
public function getConnectionMock() {
$mock = $this->getMockBuilder('Doctrine\DBAL\Connection')
->disableOriginalConstructor()
->setMethods(
[
'beginTransaction',
'commit',
'rollback',
'prepare',
'query',
'executeQuery',
'executeUpdate',
'getDatabasePlatform',
'lastInsertId',
'getExpressionBuilder',
'quote',
]
)
->getMock();
$mock->expects($this->any())
->method('prepare')
->will($this->returnValue($this->getStatementMock()));
// $mock->expects($this->any())
// ->method('query')
// ->will($this->returnValue($this->getStatementMock()));
$mock->expects($this->any())
->method('getDatabasePlatform')
->will($this->returnValue($this->getDatabasePlatformMock()));
return $mock;
}
/**
* @source https://gist.github.com/gnutix/7746893
* @return \Doctrine\DBAL\Driver\Statement|\PHPUnit_Framework_MockObject_MockObject
*/
public function getStatementMock() {
$mock = $this->getAbstractMock(
'Doctrine\DBAL\Driver\Statement',
[
'bindValue',
'execute',
'rowCount',
'fetchColumn',
]
);
$mock->expects($this->any())
->method('fetchColumn')
->will($this->returnValue(1));
return $mock;
}
/**
* @source https://gist.github.com/gnutix/7746893
*
* @param string $class The class name
* @param array $methods The available methods
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getAbstractMock($class, array $methods) {
return $this->getMockForAbstractClass(
$class,
[],
'',
true,
true,
true,
$methods,
false
);
}
}