@@ -13,9 +13,11 @@ class WpTestsStarter
13
13
private SaltGenerator $ saltGenerator ;
14
14
15
15
/**
16
- * @type string []
16
+ * @var callable []
17
17
*/
18
- private array $ definedConstants = [];
18
+ private array $ globalsFactories = [];
19
+
20
+ private array $ constants = [];
19
21
20
22
/**
21
23
* @param string $baseDir Absolute path to the wordpress-develop repository
@@ -24,189 +26,188 @@ public function __construct(string $baseDir, ?SaltGenerator $saltGenerator = nul
24
26
{
25
27
$ this ->baseDir = rtrim ($ baseDir , '\\/ ' );
26
28
$ this ->saltGenerator = $ saltGenerator ?? new SaltGenerator ();
29
+
30
+ // set some common defaults
31
+ $ this ->useDbHost ('localhost ' )
32
+ ->useSiteDomain ('example.tld ' )
33
+
34
+ ->useSiteTitle ('Wp Tests Starter ' )
35
+ ->usePhpBinary ('/usr/bin/php ' )
36
+ ->useTablePrefix ('wp_tests_ ' )
37
+ ->useAbspath ($ this ->baseDir . '/src/ ' )
38
+ ->generateSalts ();
27
39
}
28
40
29
41
/**
30
42
* Loading the WordPress testing bootstrap
31
43
*/
32
44
public function bootstrap ()
33
45
{
34
- // define required constants if they not exists
35
- $ this ->defineDbHost ();
36
- $ this ->defineDbCharset ();
37
- $ this ->defineDbCollate ();
38
-
39
- $ this ->defineTestsDomain ();
40
- $ this ->defineTestsEmail ();
41
- $ this ->defineTestsTitle ();
42
-
43
- $ this ->defineWpLang ();
44
- $ this ->definePhpBinary ();
45
- $ this ->defineWpDebug ();
46
-
47
- $ this ->defineAbspath ();
48
-
49
- $ this ->createDummyConfigFile ();
46
+ $ this ->defineConstants ();
47
+ $ this ->declareGlobals ();
48
+ $ this ->writeConfigFile ();
50
49
51
50
$ wpBoostrapFile = $ this ->baseDir . '/tests/phpunit/includes/bootstrap.php ' ;
52
51
require_once $ wpBoostrapFile ;
53
52
}
54
53
55
- /**
56
- * @param mixed $value
57
- */
58
- public function defineConst (string $ const , $ value ): bool
54
+ public function useConst (string $ const , $ value ): self
59
55
{
60
- if (defined ($ const )) {
61
-
62
- return false ;
63
- }
64
-
65
- $ this ->definedConstants [$ const ] = $ value ;
56
+ $ this ->constants [$ const ] = $ value ;
66
57
67
- return define ( $ const , $ value ) ;
58
+ return $ this ;
68
59
}
69
60
70
- public function defineAbspath (? string $ abspath = null ): void
61
+ public function useGlobalVar ( string $ var , $ value ): self
71
62
{
72
- if (empty ($ abspath )) {
73
- $ abspath = $ this ->baseDir . '/src/ ' ;
74
- }
75
- $ this ->defineConst ('ABSPATH ' , $ abspath );
76
- }
63
+ $ this ->globalsFactories [] = static function (array &$ globals ) use ($ var , $ value ): void {
64
+ $ globals [$ var ] = $ value ;
65
+ };
77
66
78
- public function defineDbName (string $ dbName ): void
79
- {
80
- $ this ->defineConst ('DB_NAME ' , $ dbName );
67
+ return $ this ;
81
68
}
82
69
83
- public function defineDbHost ( string $ dbHost = ' localhost ' ): void
70
+ public function useAbspath (? string $ abspath = null ): self
84
71
{
85
- $ this ->defineConst ( ' DB_HOST ' , $ dbHost );
72
+ return $ this ->useConst ( ' ABSPATH ' , $ abspath );
86
73
}
87
74
88
- public function defineDbUser (string $ dbUser ): void
75
+ public function useDbName (string $ dbName ): self
89
76
{
90
- $ this ->defineConst ( ' DB_USER ' , $ dbUser );
77
+ return $ this ->useConst ( ' DB_NAME ' , $ dbName );
91
78
}
92
79
93
- public function defineDbPassword (string $ dbPassword ): void
80
+ public function useDbHost (string $ dbHost ): self
94
81
{
95
- $ this ->defineConst ( ' DB_PASSWORD ' , $ dbPassword );
82
+ return $ this ->useConst ( ' DB_HOST ' , $ dbHost );
96
83
}
97
84
98
- public function defineDbCharset (string $ dbCharset = ' utf8 ' ): void
85
+ public function useDbUser (string $ dbUser ): self
99
86
{
100
- $ this ->defineConst ( ' DB_CHARSET ' , $ dbCharset );
87
+ return $ this ->useConst ( ' DB_USER ' , $ dbUser );
101
88
}
102
89
103
- public function defineDbCollate (string $ dbCollate = '' ): void
90
+ public function useDbPassword (string $ dbPassword ): self
104
91
{
105
- $ this ->defineConst ( ' DB_COLLATE ' , $ dbCollate );
92
+ return $ this ->useConst ( ' DB_PASSWORD ' , $ dbPassword );
106
93
}
107
94
108
- public function defineWpDebug ( bool $ wpDebug = false ): void
95
+ public function useDbCharset ( string $ dbCharset ): self
109
96
{
110
- $ this ->defineConst ( ' WP_DEBUG ' , $ wpDebug );
97
+ return $ this ->useConst ( ' DB_CHARSET ' , $ dbCharset );
111
98
}
112
99
113
- public function defineSalts ()
100
+ public function useDbCollation ( string $ dbCollation ): self
114
101
{
115
- $ saltConstants = [
116
- 'AUTH_KEY ' ,
117
- 'SECURE_AUTH_KEY ' ,
118
- 'LOGGED_IN_KEY ' ,
119
- 'NONCE_KEY ' ,
120
- 'SECURE_AUTH_SALT ' ,
121
- 'LOGGED_IN_SALT ' ,
122
- 'NONCE_SALT ' ,
123
- 'AUTH_KEY ' ,
124
- ];
125
-
126
- foreach ($ saltConstants as $ constant ) {
127
- $ this ->defineConst (
128
- $ constant ,
129
- $ this ->saltGenerator ->generateSalt ()
130
- );
131
- }
132
- }
133
-
134
- public function defineTestsDomain (string $ domain = 'example.org ' ): void
135
- {
136
- $ this ->defineConst ('WP_TESTS_DOMAIN ' , $ domain );
102
+ return $ this ->useConst ('DB_COLLATE ' , $ dbCollation );
137
103
}
138
104
139
- public function defineTestsEmail ( string $ email = ' [email protected] ' ):
void
105
+ public function useDebugMode ( bool $ wpDebug ): self
140
106
{
141
- $ this ->defineConst ( ' WP_TESTS_EMAIL ' , $ email );
107
+ return $ this ->useConst ( ' WP_DEBUG ' , $ wpDebug );
142
108
}
143
109
144
- public function defineTestsTitle (string $ title = ' Test Blog ' ): void
110
+ public function useSiteDomain (string $ domain ): self
145
111
{
146
- $ this ->defineConst ( ' WP_TESTS_TITLE ' , $ title );
112
+ return $ this ->useConst ( ' WP_TESTS_DOMAIN ' , $ domain );
147
113
}
148
114
149
- public function definePhpBinary (string $ binary = ' php ' ): void
115
+ public function useEmail (string $ email ): self
150
116
{
151
- $ this ->defineConst ( ' WP_PHP_BINARY ' , $ binary );
117
+ return $ this ->useConst ( ' WP_TESTS_EMAIL ' , $ email );
152
118
}
153
119
154
- public function defineWpLang (string $ lang = '' ): void
120
+ public function useSiteTitle (string $ title ): self
155
121
{
156
- $ this ->defineConst ( ' WPLANG ' , $ lang );
122
+ return $ this ->useConst ( ' WP_TESTS_TITLE ' , $ title );
157
123
}
158
124
159
- public function defineTestForceKnownBugs ( bool $ flag ): void
125
+ public function usePhpBinary ( string $ binary ): self
160
126
{
161
- $ this ->defineConst ( ' WP_TESTS_FORCE_KNOWN_BUGS ' , $ flag );
127
+ return $ this ->useConst ( ' WP_PHP_BINARY ' , $ binary );
162
128
}
163
129
164
- public function defineTestMultisite (bool $ flag ): void
130
+ public function testAsMultisite (bool $ isMultisite ): self
165
131
{
166
- $ this ->defineConst ('WP_TESTS_MULTISITE ' , $ flag );
132
+ return $ this ->useConst ('WP_TESTS_MULTISITE ' , $ isMultisite );
167
133
}
168
134
169
- public function defineWpPluginDir (string $ dir ): void
135
+ public function useWpPluginDir (string $ dir ): self
170
136
{
171
137
$ dir = rtrim ($ dir , '\\/ ' );
172
- $ this ->defineConst ('WP_PLUGIN_DIR ' , $ dir );
138
+
139
+ return $ this ->useConst ('WP_PLUGIN_DIR ' , $ dir );
173
140
}
174
141
175
142
/**
176
143
* @param string $plugin a plugin file relative to WP's plugin directory like 'directory/plugin-file.php'
177
144
*/
178
- public function setActivePlugin (string $ plugin ): void
145
+ public function addActivePlugin (string $ plugin ): self
179
146
{
180
- if (! isset ($ GLOBALS ['wp_tests_options ' ])) {
181
- $ GLOBALS ['wp_tests_options ' ] = [];
182
- }
147
+ $ this ->globalsFactories [] = static function () use ($ plugin ): void {
148
+ if (! isset ($ GLOBALS ['wp_tests_options ' ])) {
149
+ $ GLOBALS ['wp_tests_options ' ] = [];
150
+ }
183
151
184
- if (! isset ($ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
185
- $ GLOBALS ['wp_tests_options ' ]['active_plugins ' ] = [];
186
- }
152
+ if (! isset ($ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
153
+ $ GLOBALS ['wp_tests_options ' ]['active_plugins ' ] = [];
154
+ }
187
155
188
- if (in_array ($ plugin , $ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
189
- return ;
190
- }
156
+ if (in_array ($ plugin , $ GLOBALS ['wp_tests_options ' ]['active_plugins ' ])) {
157
+ return ;
158
+ }
159
+
160
+ $ GLOBALS ['wp_tests_options ' ]['active_plugins ' ][] = $ plugin ;
161
+ };
191
162
192
- $ GLOBALS [ ' wp_tests_options ' ][ ' active_plugins ' ][] = $ plugin ;
163
+ return $ this ;
193
164
}
194
165
195
- public function setTablePrefix (string $ prefix = ' wptests_ ' ): void
166
+ public function useTablePrefix (string $ prefix ): self
196
167
{
197
- $ var = 'table_prefix ' ;
198
- $ this ->setGlobal ($ var , $ prefix );
168
+ return $ this ->useGlobalVar ('table_prefix ' , $ prefix );
199
169
}
200
170
201
- /**
202
- * @param mixed $value
203
- */
204
- public function setGlobal (string $ var , $ value ): void
171
+ private function defineConstants (): void
172
+ {
173
+ foreach ($ this ->constants as $ constant => $ value ) {
174
+ if (defined ($ constant )) {
175
+ continue ;
176
+ }
177
+
178
+ define ($ constant , $ value );
179
+ }
180
+ }
181
+
182
+ private function declareGlobals (): void
205
183
{
206
- $ GLOBALS [$ var ] = $ value ;
184
+ foreach ($ this ->globalsFactories as $ factory ) {
185
+ $ factory ($ GLOBALS );
186
+ }
207
187
}
208
188
209
- public function createDummyConfigFile ()
189
+ private function generateSalts ()
190
+ {
191
+ $ saltConstants = [
192
+ 'AUTH_KEY ' ,
193
+ 'SECURE_AUTH_KEY ' ,
194
+ 'LOGGED_IN_KEY ' ,
195
+ 'NONCE_KEY ' ,
196
+ 'SECURE_AUTH_SALT ' ,
197
+ 'LOGGED_IN_SALT ' ,
198
+ 'NONCE_SALT ' ,
199
+ 'AUTH_KEY ' ,
200
+ ];
201
+
202
+ foreach ($ saltConstants as $ constant ) {
203
+ $ this ->useConst (
204
+ $ constant ,
205
+ $ this ->saltGenerator ->generateSalt ()
206
+ );
207
+ }
208
+ }
209
+
210
+ private function writeConfigFile ()
210
211
{
211
212
$ configFile = $ this ->getConfigFile ();
212
213
if (! file_exists ($ configFile )) {
@@ -227,23 +228,15 @@ public function createDummyConfigFile()
227
228
file_put_contents ($ configFile , $ content , LOCK_EX );
228
229
}
229
230
230
- public function getConfigFile (): string
231
+ private function getConfigFile (): string
231
232
{
232
233
return $ this ->baseDir . '/wp-tests-config.php ' ;
233
234
}
234
235
235
- /**
236
- * @return string[]
237
- */
238
- public function getDefinedConstants (): array
239
- {
240
- return $ this ->definedConstants ;
241
- }
242
-
243
- public function getDefinedConstantsCode (): string
236
+ private function getDefinedConstantsCode (): string
244
237
{
245
238
$ code = '' ;
246
- foreach ($ this ->definedConstants as $ constant => $ value ) {
239
+ foreach ($ this ->constants as $ constant => $ value ) {
247
240
$ constant = $ this ->escapePhpString ($ constant );
248
241
$ value = $ this ->escapePhpString ($ value );
249
242
$ code .= "if ( ! defined( ' {$ constant }' ) ) \n" ;
@@ -253,7 +246,7 @@ public function getDefinedConstantsCode(): string
253
246
return $ code ;
254
247
}
255
248
256
- public function escapePhpString ($ value ): string
249
+ private function escapePhpString ($ value ): string
257
250
{
258
251
$ value = str_replace (
259
252
['<?php ' , '<? ' , '?> ' ],
0 commit comments