4
4
5
5
namespace WpTestsStarter ;
6
6
7
+ use WpTestsStarter \Helper \SaltGenerator ;
8
+
7
9
class WpTestsStarter
8
10
{
9
- /**
10
- * @type string
11
- */
12
- private $ baseDir ;
11
+ private string $ baseDir ;
13
12
14
- /**
15
- * @type Helper\SaltGenerator
16
- */
17
- private $ saltGenerator ;
13
+ private SaltGenerator $ saltGenerator ;
18
14
19
15
/**
20
- * list of all by this class defined constants
21
- *
22
- * @type array
16
+ * @type string[]
23
17
*/
24
- private $ definedConstants = [];
18
+ private array $ definedConstants = [];
25
19
26
20
/**
27
- * Pass the absolute path of the wordpress-dev package here.
28
- * It is "$baseDir/vendor/inpsyde/wordpress-dev" if you're using
29
- * the inpsyde/wordpress-dev package
30
- *
31
- * @param string $baseDir
32
- * @param Helper\SaltGenerator $saltGenerator
21
+ * @param string $baseDir Absolute path to the wordpress-develop repository
33
22
*/
34
- public function __construct ($ baseDir , Helper \ SaltGenerator $ saltGenerator = null )
23
+ public function __construct (string $ baseDir , ? SaltGenerator $ saltGenerator = null )
35
24
{
36
25
$ this ->baseDir = rtrim ($ baseDir , '\\/ ' );
37
- if (!$ saltGenerator ) {
38
- $ saltGenerator = new Helper \SaltGenerator ();
39
- }
40
- $ this ->saltGenerator = $ saltGenerator ;
26
+ $ this ->saltGenerator = $ saltGenerator ?? new SaltGenerator ();
41
27
}
42
28
43
29
/**
@@ -67,92 +53,63 @@ public function bootstrap()
67
53
}
68
54
69
55
/**
70
- * Define a given constant if it not already exists
71
- *
72
- * @param string $const
73
56
* @param mixed $value
74
- * @return bool
75
57
*/
76
- public function defineConst ($ const , $ value )
58
+ public function defineConst (string $ const , $ value ): bool
77
59
{
78
60
if (defined ($ const )) {
61
+
79
62
return false ;
80
63
}
81
64
82
65
$ this ->definedConstants [$ const ] = $ value ;
66
+
83
67
return define ($ const , $ value );
84
68
}
85
69
86
- /**
87
- * @param string $abspath
88
- */
89
- public function defineAbspath ($ abspath = '' )
70
+ public function defineAbspath (?string $ abspath = null ): void
90
71
{
91
72
if (empty ($ abspath )) {
92
73
$ abspath = $ this ->baseDir . '/src/ ' ;
93
74
}
94
75
$ this ->defineConst ('ABSPATH ' , $ abspath );
95
76
}
96
77
97
- /**
98
- * @param string $dbName
99
- */
100
- public function defineDbName ($ dbName )
78
+ public function defineDbName (string $ dbName ): void
101
79
{
102
80
$ this ->defineConst ('DB_NAME ' , $ dbName );
103
81
}
104
82
105
- /**
106
- * @param string $dbHost
107
- */
108
- public function defineDbHost ($ dbHost = 'localhost ' )
83
+ public function defineDbHost (string $ dbHost = 'localhost ' ): void
109
84
{
110
85
$ this ->defineConst ('DB_HOST ' , $ dbHost );
111
86
}
112
87
113
- /**
114
- * @param string $dbUser
115
- */
116
- public function defineDbUser ($ dbUser )
88
+ public function defineDbUser (string $ dbUser ): void
117
89
{
118
90
$ this ->defineConst ('DB_USER ' , $ dbUser );
119
91
}
120
92
121
- /**
122
- * @param string $dbPassword
123
- */
124
- public function defineDbPassword ($ dbPassword )
93
+ public function defineDbPassword (string $ dbPassword ): void
125
94
{
126
95
$ this ->defineConst ('DB_PASSWORD ' , $ dbPassword );
127
96
}
128
97
129
- /**
130
- * @param string $dbCharset
131
- */
132
- public function defineDbCharset ($ dbCharset = 'utf8 ' )
98
+ public function defineDbCharset (string $ dbCharset = 'utf8 ' ): void
133
99
{
134
100
$ this ->defineConst ('DB_CHARSET ' , $ dbCharset );
135
101
}
136
102
137
- /**
138
- * @param string $dbCollate
139
- */
140
- public function defineDbCollate ($ dbCollate = '' )
103
+ public function defineDbCollate (string $ dbCollate = '' ): void
141
104
{
142
105
$ this ->defineConst ('DB_COLLATE ' , $ dbCollate );
143
106
}
144
107
145
- /**
146
- * @param bool $wpDebug
147
- */
148
- public function defineWpDebug ($ wpDebug = false )
108
+ public function defineWpDebug (bool $ wpDebug = false ): void
149
109
{
150
- $ this ->defineConst ('WP_DEBUG ' , ( bool ) $ wpDebug );
110
+ $ this ->defineConst ('WP_DEBUG ' , $ wpDebug );
151
111
}
152
112
153
- /**
154
- * define the security keys and salts
155
- */
156
113
public function defineSalts ()
157
114
{
158
115
$ saltConstants = [
@@ -174,83 +131,57 @@ public function defineSalts()
174
131
}
175
132
}
176
133
177
- /**
178
- * @param string $domain
179
- */
180
- public function defineTestsDomain ($ domain = 'example.org ' )
134
+ public function defineTestsDomain (string $ domain = 'example.org ' ): void
181
135
{
182
136
$ this ->defineConst ('WP_TESTS_DOMAIN ' , $ domain );
183
137
}
184
138
185
- /**
186
- * @param string $email
187
- */
188
- public function defineTestsEmail (
$ email =
'[email protected] ' )
139
+ public function defineTestsEmail (
string $ email =
'[email protected] ' ):
void
189
140
{
190
141
$ this ->defineConst ('WP_TESTS_EMAIL ' , $ email );
191
142
}
192
143
193
- /**
194
- * @param string $title
195
- */
196
- public function defineTestsTitle ($ title = 'Test Blog ' )
144
+ public function defineTestsTitle (string $ title = 'Test Blog ' ): void
197
145
{
198
146
$ this ->defineConst ('WP_TESTS_TITLE ' , $ title );
199
147
}
200
148
201
- /**
202
- * @param string $binary
203
- */
204
- public function definePhpBinary ($ binary = 'php ' )
149
+ public function definePhpBinary (string $ binary = 'php ' ): void
205
150
{
206
151
$ this ->defineConst ('WP_PHP_BINARY ' , $ binary );
207
152
}
208
153
209
- /**
210
- * @param string $lang
211
- */
212
- public function defineWpLang ($ lang = '' )
154
+ public function defineWpLang (string $ lang = '' ): void
213
155
{
214
156
$ this ->defineConst ('WPLANG ' , $ lang );
215
157
}
216
158
217
- /**
218
- * @param bool $flag
219
- */
220
- public function defineTestForceKnownBugs ($ flag )
159
+ public function defineTestForceKnownBugs (bool $ flag ): void
221
160
{
222
- $ this ->defineConst ('WP_TESTS_FORCE_KNOWN_BUGS ' , ( bool ) $ flag );
161
+ $ this ->defineConst ('WP_TESTS_FORCE_KNOWN_BUGS ' , $ flag );
223
162
}
224
163
225
- /**
226
- * @param $flag
227
- */
228
- public function defineTestMultisite ($ flag )
164
+ public function defineTestMultisite (bool $ flag ): void
229
165
{
230
- $ this ->defineConst ('WP_TESTS_MULTISITE ' , ( bool ) $ flag );
166
+ $ this ->defineConst ('WP_TESTS_MULTISITE ' , $ flag );
231
167
}
232
168
233
- /**
234
- * @param string $dir
235
- */
236
- public function defineWpPluginDir ($ dir )
169
+ public function defineWpPluginDir (string $ dir ): void
237
170
{
238
171
$ dir = rtrim ($ dir , '\\/ ' );
239
172
$ this ->defineConst ('WP_PLUGIN_DIR ' , $ dir );
240
173
}
241
174
242
175
/**
243
- * pass a plugin slug like 'directory/plugin-file.php'
244
- *
245
- * @param string $plugin
176
+ * @param string $plugin a plugin file relative to WP's plugin directory like 'directory/plugin-file.php'
246
177
*/
247
- public function setActivePlugin ($ plugin )
178
+ public function setActivePlugin (string $ plugin ): void
248
179
{
249
- if (!isset ($ GLOBALS ['wp_tests_options ' ])) {
180
+ if (! isset ($ GLOBALS ['wp_tests_options ' ])) {
250
181
$ GLOBALS ['wp_tests_options ' ] = [];
251
182
}
252
183
253
- if (!isset ($ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
184
+ if (! isset ($ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
254
185
$ GLOBALS ['wp_tests_options ' ]['active_plugins ' ] = [];
255
186
}
256
187
@@ -261,43 +192,30 @@ public function setActivePlugin($plugin)
261
192
$ GLOBALS ['wp_tests_options ' ]['active_plugins ' ][] = $ plugin ;
262
193
}
263
194
264
- /**
265
- * @param string $prefix
266
- */
267
- public function setTablePrefix ($ prefix = 'wptests_ ' )
195
+ public function setTablePrefix (string $ prefix = 'wptests_ ' ): void
268
196
{
269
197
$ var = 'table_prefix ' ;
270
198
$ this ->setGlobal ($ var , $ prefix );
271
199
}
272
200
273
201
/**
274
- * @param $var
275
- * @param $value
202
+ * @param mixed $value
276
203
*/
277
- public function setGlobal ($ var , $ value )
204
+ public function setGlobal (string $ var , $ value ): void
278
205
{
279
206
$ GLOBALS [$ var ] = $ value ;
280
207
}
281
208
282
- /**
283
- * the WordPress bootstrap process does not allow
284
- * to define a custom path of the config but looks
285
- * for this file. so we create just an empty one.
286
- */
287
209
public function createDummyConfigFile ()
288
210
{
289
211
$ configFile = $ this ->getConfigFile ();
290
- if (!file_exists ($ configFile )) {
212
+ if (! file_exists ($ configFile )) {
291
213
touch ($ configFile );
292
214
}
293
215
294
216
/**
295
- * the WordPress testing bootstrap requires the definitions
296
- * of all these content in exactly this file, there's no way
297
- * to dynamically define these constants as
298
- * tests/phpunit/includes/bootstrap.php triggers a system() call to
299
- * tests/phpunit/includes/install.php with a static path to the
300
- * config file
217
+ * We have to persist all dynamic configuration (constants and globals) in a wp-config.php
218
+ * as the WordPress internal boostrap process runs a sub process for the installation (setup DB tables)
301
219
*/
302
220
$ constantsDefinition = $ this ->getDefinedConstantsCode ();
303
221
$ content = <<<PHP
@@ -309,26 +227,20 @@ public function createDummyConfigFile()
309
227
file_put_contents ($ configFile , $ content , LOCK_EX );
310
228
}
311
229
312
- /**
313
- * @return string
314
- */
315
- public function getConfigFile ()
230
+ public function getConfigFile (): string
316
231
{
317
232
return $ this ->baseDir . '/wp-tests-config.php ' ;
318
233
}
319
234
320
- /**
321
- * @return array
322
- */
323
- public function getDefinedConstants ()
235
+ /**
236
+ * @return string[]
237
+ */
238
+ public function getDefinedConstants (): array
324
239
{
325
240
return $ this ->definedConstants ;
326
241
}
327
242
328
- /**
329
- * that feels so ugly
330
- */
331
- public function getDefinedConstantsCode ()
243
+ public function getDefinedConstantsCode (): string
332
244
{
333
245
$ code = '' ;
334
246
foreach ($ this ->definedConstants as $ constant => $ value ) {
@@ -341,10 +253,7 @@ public function getDefinedConstantsCode()
341
253
return $ code ;
342
254
}
343
255
344
- /**
345
- * that feels even more ugly
346
- */
347
- public function escapePhpString ($ value )
256
+ public function escapePhpString ($ value ): string
348
257
{
349
258
$ value = str_replace (
350
259
['<?php ' , '<? ' , '?> ' ],
0 commit comments