Skip to content

Commit 8c4609a

Browse files
committed
Refactor API #10
* Rename setters: ::defineDbName() → ::useDbName() and thus * Declare constants and globals on WpTestsStarter::bootstrap() and not immediately on the setters call * set all unnecessary public methods from WpTestsStarter to private
1 parent 36df270 commit 8c4609a

File tree

5 files changed

+144
-439
lines changed

5 files changed

+144
-439
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $starter = new WpTestsStarter( '/path/to/your-project/vendor/inpsyde/wordpress-d
4242
$starter->defineDbName( 'your-test-db' );
4343
$starter->defineDbUser( 'your-test-user' );
4444
$starter->defineDbPassword( 'your-test-user' );
45-
$starter->setTablePrefix( 'your_table_prefix_' );
45+
$starter->useTablePrefix( 'your_table_prefix_' );
4646

4747
// This will finally include the WordPress test bootstrap
4848
$starter->bootstrap();
@@ -154,7 +154,7 @@ $starter = new WpTestsStarter( "{$base_dir}/vendor/inpsyde/wordpress-dev" );
154154
$starter->defineDbName( DB_NAME );
155155
$starter->defineDbUser( DB_USER );
156156
$starter->defineDbPassword( DB_PASSWORD );
157-
$starter->setTablePrefix( DB_TABLE_PREFIX );
157+
$starter->useTablePrefix( DB_TABLE_PREFIX );
158158

159159
// this will finally create the wp-tests-config.php and include the wordpress core tests bootstrap
160160
$starter->bootstrap();

src/WpTestsStarter/WpTestsStarter.php

+112-119
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ class WpTestsStarter
1313
private SaltGenerator $saltGenerator;
1414

1515
/**
16-
* @type string[]
16+
* @var callable[]
1717
*/
18-
private array $definedConstants = [];
18+
private array $globalsFactories = [];
19+
20+
private array $constants = [];
1921

2022
/**
2123
* @param string $baseDir Absolute path to the wordpress-develop repository
@@ -24,189 +26,188 @@ public function __construct(string $baseDir, ?SaltGenerator $saltGenerator = nul
2426
{
2527
$this->baseDir = rtrim($baseDir, '\\/');
2628
$this->saltGenerator = $saltGenerator ?? new SaltGenerator();
29+
30+
// set some common defaults
31+
$this->useDbHost('localhost')
32+
->useSiteDomain('example.tld')
33+
->useEmail('[email protected]')
34+
->useSiteTitle('Wp Tests Starter')
35+
->usePhpBinary('/usr/bin/php')
36+
->useTablePrefix('wp_tests_')
37+
->useAbspath($this->baseDir . '/src/')
38+
->generateSalts();
2739
}
2840

2941
/**
3042
* Loading the WordPress testing bootstrap
3143
*/
3244
public function bootstrap()
3345
{
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();
5049

5150
$wpBoostrapFile = $this->baseDir . '/tests/phpunit/includes/bootstrap.php';
5251
require_once $wpBoostrapFile;
5352
}
5453

55-
/**
56-
* @param mixed $value
57-
*/
58-
public function defineConst(string $const, $value): bool
54+
public function useConst(string $const, $value): self
5955
{
60-
if (defined($const)) {
61-
62-
return false;
63-
}
64-
65-
$this->definedConstants[$const] = $value;
56+
$this->constants[$const] = $value;
6657

67-
return define($const, $value);
58+
return $this;
6859
}
6960

70-
public function defineAbspath(?string $abspath = null): void
61+
public function useGlobalVar(string $var, $value): self
7162
{
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+
};
7766

78-
public function defineDbName(string $dbName): void
79-
{
80-
$this->defineConst('DB_NAME', $dbName);
67+
return $this;
8168
}
8269

83-
public function defineDbHost(string $dbHost = 'localhost'): void
70+
public function useAbspath(?string $abspath = null): self
8471
{
85-
$this->defineConst('DB_HOST', $dbHost);
72+
return $this->useConst('ABSPATH', $abspath);
8673
}
8774

88-
public function defineDbUser(string $dbUser): void
75+
public function useDbName(string $dbName): self
8976
{
90-
$this->defineConst('DB_USER', $dbUser);
77+
return $this->useConst('DB_NAME', $dbName);
9178
}
9279

93-
public function defineDbPassword(string $dbPassword): void
80+
public function useDbHost(string $dbHost): self
9481
{
95-
$this->defineConst('DB_PASSWORD', $dbPassword);
82+
return $this->useConst('DB_HOST', $dbHost);
9683
}
9784

98-
public function defineDbCharset(string $dbCharset = 'utf8'): void
85+
public function useDbUser(string $dbUser): self
9986
{
100-
$this->defineConst('DB_CHARSET', $dbCharset);
87+
return $this->useConst('DB_USER', $dbUser);
10188
}
10289

103-
public function defineDbCollate(string $dbCollate = ''): void
90+
public function useDbPassword(string $dbPassword): self
10491
{
105-
$this->defineConst('DB_COLLATE', $dbCollate);
92+
return $this->useConst('DB_PASSWORD', $dbPassword);
10693
}
10794

108-
public function defineWpDebug(bool $wpDebug = false): void
95+
public function useDbCharset(string $dbCharset): self
10996
{
110-
$this->defineConst('WP_DEBUG', $wpDebug);
97+
return $this->useConst('DB_CHARSET', $dbCharset);
11198
}
11299

113-
public function defineSalts()
100+
public function useDbCollation(string $dbCollation): self
114101
{
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);
137103
}
138104

139-
public function defineTestsEmail(string $email = '[email protected]'): void
105+
public function useDebugMode(bool $wpDebug): self
140106
{
141-
$this->defineConst('WP_TESTS_EMAIL', $email);
107+
return $this->useConst('WP_DEBUG', $wpDebug);
142108
}
143109

144-
public function defineTestsTitle(string $title = 'Test Blog'): void
110+
public function useSiteDomain(string $domain): self
145111
{
146-
$this->defineConst('WP_TESTS_TITLE', $title);
112+
return $this->useConst('WP_TESTS_DOMAIN', $domain);
147113
}
148114

149-
public function definePhpBinary(string $binary = 'php'): void
115+
public function useEmail(string $email): self
150116
{
151-
$this->defineConst('WP_PHP_BINARY', $binary);
117+
return $this->useConst('WP_TESTS_EMAIL', $email);
152118
}
153119

154-
public function defineWpLang(string $lang = ''): void
120+
public function useSiteTitle(string $title): self
155121
{
156-
$this->defineConst('WPLANG', $lang);
122+
return $this->useConst('WP_TESTS_TITLE', $title);
157123
}
158124

159-
public function defineTestForceKnownBugs(bool $flag): void
125+
public function usePhpBinary(string $binary): self
160126
{
161-
$this->defineConst('WP_TESTS_FORCE_KNOWN_BUGS', $flag);
127+
return $this->useConst('WP_PHP_BINARY', $binary);
162128
}
163129

164-
public function defineTestMultisite(bool $flag): void
130+
public function testAsMultisite(bool $isMultisite): self
165131
{
166-
$this->defineConst('WP_TESTS_MULTISITE', $flag);
132+
return $this->useConst('WP_TESTS_MULTISITE', $isMultisite);
167133
}
168134

169-
public function defineWpPluginDir(string $dir): void
135+
public function useWpPluginDir(string $dir): self
170136
{
171137
$dir = rtrim($dir, '\\/');
172-
$this->defineConst('WP_PLUGIN_DIR', $dir);
138+
139+
return $this->useConst('WP_PLUGIN_DIR', $dir);
173140
}
174141

175142
/**
176143
* @param string $plugin a plugin file relative to WP's plugin directory like 'directory/plugin-file.php'
177144
*/
178-
public function setActivePlugin(string $plugin): void
145+
public function addActivePlugin(string $plugin): self
179146
{
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+
}
183151

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+
}
187155

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+
};
191162

192-
$GLOBALS['wp_tests_options']['active_plugins'][] = $plugin;
163+
return $this;
193164
}
194165

195-
public function setTablePrefix(string $prefix = 'wptests_'): void
166+
public function useTablePrefix(string $prefix): self
196167
{
197-
$var = 'table_prefix';
198-
$this->setGlobal($var, $prefix);
168+
return $this->useGlobalVar('table_prefix', $prefix);
199169
}
200170

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
205183
{
206-
$GLOBALS[$var] = $value;
184+
foreach ($this->globalsFactories as $factory) {
185+
$factory($GLOBALS);
186+
}
207187
}
208188

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()
210211
{
211212
$configFile = $this->getConfigFile();
212213
if (! file_exists($configFile)) {
@@ -227,23 +228,15 @@ public function createDummyConfigFile()
227228
file_put_contents($configFile, $content, LOCK_EX);
228229
}
229230

230-
public function getConfigFile(): string
231+
private function getConfigFile(): string
231232
{
232233
return $this->baseDir . '/wp-tests-config.php';
233234
}
234235

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
244237
{
245238
$code = '';
246-
foreach ($this->definedConstants as $constant => $value) {
239+
foreach ($this->constants as $constant => $value) {
247240
$constant = $this->escapePhpString($constant);
248241
$value = $this->escapePhpString($value);
249242
$code .= "if ( ! defined( '{$constant}' ) )\n";
@@ -253,7 +246,7 @@ public function getDefinedConstantsCode(): string
253246
return $code;
254247
}
255248

256-
public function escapePhpString($value): string
249+
private function escapePhpString($value): string
257250
{
258251
$value = str_replace(
259252
['<?php', '<?', '?>'],

0 commit comments

Comments
 (0)