packages/php/email-editor/writing-tests.md
Environment Setup:
# Install wp-env
npm install
# Start test environment
composer run env:start
Required Dependencies:
tests/
├── unit/
│ ├── Engine/
│ ├── Integrations/
│ ├── bootstrap.php
│ └── stubs.php
└── integration/
tests/unit/tests/integration/_Test.php suffix@group annotation# Run all unit tests
composer run test:unit
# Run specific test file
pnpm --filter='@woocommerce/email-editor-config' test:unit -- tests/unit/Engine/Settings_Controller_Test.php
# Run tests with specific group
composer run test:unit -- --group=settings
<?php
declare(strict_types = 1);
namespace Automattic\WooCommerce\EmailEditor\Engine;
class Settings_Controller_Test extends \Email_Editor_Unit_Test {
/**
* @var Settings_Controller
*/
private $controller;
public function setUp(): void {
parent::setUp();
$this->controller = new Settings_Controller();
}
public function tearDown(): void {
parent::tearDown();
}
/**
* @test
* @group settings
*/
public function test_can_register_settings() {
// Arrange
$expected_settings = [
'email_subject' => 'Test Subject',
'email_preheader' => 'Test Preheader'
];
// Act
$this->controller->register_settings($expected_settings);
// Assert
$this->assertEquals(
$expected_settings,
$this->controller->get_settings()
);
}
}
# Run all integration tests
composer run test:integration
# Run specific integration test
pnpm --filter='@woocommerce/email-editor-config' test:integration -- tests/integration/Integrations/Core/Renderer/Blocks/Social_Links_Test.php
# Run tests with specific group
composer run test:integration -- --group=email-templates
<?php
declare(strict_types = 1);
namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer;
class Renderer_Test extends \Email_Editor_Integration_Test_Case {
/**
* @var Renderer
*/
private $renderer;
public function setUp(): void {
parent::setUp();
$this->renderer = $this->renderer = $this->di_container->get( Renderer::class );
}
/**
* @test
* @group email-templates
*/
public function test_can_render_email_template() {
// Test here
}
private function create_mock_order() {
// Create a mock order for testing
return wc_create_order();
}
}
Test Isolation:
setUp() and tearDown() for test fixturesNaming Conventions:
test_ prefix or @test annotation@group annotationAssertions:
Mocking:
Running Tests in Debug Mode:
composer run test:unit -- --debug
Using Xdebug:
Viewing Test Coverage:
composer run test:unit -- --coverage-html coverage/
Using wp-env for Testing:
# Start test environment
composer run env:start
# Stop test environment
composer run env:stop
# Reset test environment
composer run env:clean
Guide for writing E2E tests can be found at packages/js/email-editor/writing-e2e-tests.md