wpstarter/o-testbench
WpStarter port of Laravel Testbench for package development. Provides a lightweight Laravel app environment for running package tests, bootstrapping service providers, and simulating framework features without installing a full application.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require --dev wpstarter/o-testbench
Add to composer.json under require-dev if not using package manager.
Note: v1.0 now includes built-in support for WordPress 6.0+ core compatibility.
Basic Configuration
Ensure your phpunit.xml includes:
<phpunit>
<extensions>
<extension class="WpStarter\TestBench\TestBenchExtension"/>
</extensions>
<php>
<server name="WP_VERSION" value="6.0"/>
</php>
</phpunit>
First Test Case
Create a test class extending WpStarter\TestBench\TestCase:
use WpStarter\TestBench\TestCase;
class ExampleTest extends TestCase {
public function test_basic_environment() {
$this->assertTrue(true); // Verify TestBench is loaded
$this->assertEquals('6.0', $this->wpVersion()); // New helper
}
}
Bootstrap WordPress
Override getEnvironmentSetUp() in your test class:
protected function getEnvironmentSetUp($app) {
$app['config']->set('database.default', 'sqlite');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// New: Use $this->activatePlugin() helper
$this->activatePlugin('my-plugin');
}
createApplication() with custom DB config:
$app = $this->createApplication([
'database' => [
'default' => 'testing',
'connections' => [
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
],
],
]);
assertDatabaseHas() with WordPress table support
$this->assertDatabaseHas('wp_posts', ['post_title' => 'Test']);
getEnvironmentSetUp():
$this->activatePlugin('my-plugin');
$this->activateTheme('my-theme');
$this->deactivatePlugin('unwanted-plugin');
$this->mockFunction('get_current_user_id', function () {
return 1;
});
$this->mockFunction('register_block_type', function () {
return true;
});
call() for WordPress REST API or admin routes:
$response = $this->call('GET', '/wp-json/wp/v2/posts');
$this->assertEquals(200, $response->status());
$response = $this->call('POST', '/wp-json/wp/v2/blocks');
$this->artisan('wpstarter:command')
->expectsQuestion('confirm', 'yes')
->assertExitCode(0)
->assertOutputContains('Success');
$this->assertBlockRegistered('core/paragraph');
$this->assertBlockOutput('core/paragraph', '<p>Test</p>');
Extend TestCase for reusable logic:
class CustomTestCase extends TestCase {
protected function setUp(): void {
parent::setUp();
$this->mockFunction('current_user_can', fn() => true);
// New: Use $this->withBlockEditor() for block-specific tests
$this->withBlockEditor();
}
}
wpstarter/o-testbench to test:
getEnvironmentSetUp).$this->assertBlockEditorSetting('core/block-editor', 'allowedBlockTypes', ['core/paragraph']);
Configure PHPUnit for parallel runs with WordPress isolation:
<phpunit>
<extensions>
<extension class="WpStarter\TestBench\TestBenchExtension"/>
</extensions>
<server name="APP_ENV" value="testing"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="WP_TESTS_DOMAIN" value="example.test"/>
</phpunit>
WP_DEBUG).getEnvironmentSetUp():
define('WP_DEBUG', true);
define('WP_TESTS_DIR', __DIR__);
define('WP_VERSION', '6.0'); // Required for v1.0
$this->activatePlugin('plugin-a')->activatePlugin('plugin-b');
wpdb) are singletons, making mocking tricky.WP_Mock alongside TestBench:
WP_Mock::userFunction('get_option', [
'args' => ['key'],
'return' => 'value',
]);
resetMocks() helper
$this->resetMocks();
TestCase methods as undefined.WpStarter\TestBench\TestCase.withBlockEditor() contextually:
public function test_block_editor() {
$this->withBlockEditor();
// Test block-specific functionality
}
Use WP_Mock for debugging:
$this->assertEquals(
['key' => 'value'],
WP_Mock::userFunction('get_option')->getLastCallArgs()
);
$this->dumpWordPressState() for comprehensive debugging
$this->dumpWordPressState(); // Outputs WP globals, hooks, and more
$app['db']->disableTransactions();
disableBlockEditorCache() for faster block tests
$this->disableBlockEditorCache();
Class 'WpStarter\...' not found.wpstarter/o-testbench and wpstarter/framework are installed.$this->assertBlockEditorLoaded();
$this->assertBlockEditorSettingsValid();
Extend TestCase for domain-specific assertions:
class PostTestCase extends TestCase {
protected function assertPostExists($id) {
$this->assertDatabaseHas('wp_posts', ['ID' => $id]);
}
// New: Block-specific assertions
protected function assertBlockRegistered($name) {
$this->assertTrue(has_block($name));
}
}
Use Laravel factories with Word
How can I help you explore Laravel packages today?