Commit fd9c578cb4 for woocommerce
commit fd9c578cb4363a55ac986436a61fa893fe265459
Author: Raluca Stan <ralucastn@gmail.com>
Date: Fri Dec 19 13:41:18 2025 +0200
Update AI backend skill to always be invoked before writing tests (#62531)
* Update backend skill to always invoke before writing tests
* Improve Claude.md and add a unit test template in the skill
* add changelog
diff --git a/.ai/skills/woocommerce-backend-dev/SKILL.md b/.ai/skills/woocommerce-backend-dev/SKILL.md
index 0b08593223..d5f8dfab07 100644
--- a/.ai/skills/woocommerce-backend-dev/SKILL.md
+++ b/.ai/skills/woocommerce-backend-dev/SKILL.md
@@ -1,12 +1,21 @@
---
name: woocommerce-backend-dev
-description: Add or modify WooCommerce backend PHP code following project conventions. Use when creating new classes, methods, hooks, or modifying existing backend code.
+description: Add or modify WooCommerce backend PHP code following project conventions. Use when creating new classes, methods, hooks, or modifying existing backend code. **MUST be invoked before writing any PHP unit tests.**
---
# WooCommerce Backend Development
This skill provides guidance for developing WooCommerce backend PHP code according to project standards and conventions.
+## When to Use This Skill
+
+**ALWAYS invoke this skill before:**
+
+- Writing new PHP unit tests (`*Test.php` files)
+- Creating new PHP classes
+- Modifying existing backend PHP code
+- Adding hooks or filters
+
## Instructions
Follow WooCommerce project conventions when adding or modifying backend PHP code:
diff --git a/.ai/skills/woocommerce-backend-dev/unit-tests.md b/.ai/skills/woocommerce-backend-dev/unit-tests.md
index 475fac9ea2..acb9ffaf60 100644
--- a/.ai/skills/woocommerce-backend-dev/unit-tests.md
+++ b/.ai/skills/woocommerce-backend-dev/unit-tests.md
@@ -2,6 +2,7 @@
## Table of Contents
+- [Complete Test File Template](#complete-test-file-template)
- [Test File Naming and Location](#test-file-naming-and-location)
- [System Under Test Variable](#system-under-test-variable)
- [Test Method Documentation](#test-method-documentation)
@@ -11,6 +12,80 @@
- [Mocking the WooCommerce Logger](#mocking-the-woocommerce-logger)
- [General Testing Best Practices](#general-testing-best-practices)
+## Complete Test File Template
+
+Use this template when creating new test files. It shows all conventions applied together:
+
+```php
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\Admin;
+
+use Automattic\WooCommerce\Internal\Admin\OrderProcessor;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for the OrderProcessor class.
+ */
+class OrderProcessorTest extends WC_Unit_Test_Case {
+
+ /**
+ * The System Under Test.
+ *
+ * @var OrderProcessor
+ */
+ private $sut;
+
+ /**
+ * Set up test fixtures.
+ */
+ public function setUp(): void {
+ parent::setUp();
+ $this->sut = new OrderProcessor();
+ }
+
+ /**
+ * Tear down test fixtures.
+ */
+ public function tearDown(): void {
+ parent::tearDown();
+ }
+
+ /**
+ * @testdox Should return true when order is valid.
+ */
+ public function test_returns_true_for_valid_order(): void {
+ $order = wc_create_order();
+
+ $result = $this->sut->is_valid( $order );
+
+ $this->assertTrue( $result, 'Valid orders should return true' );
+ }
+
+ /**
+ * @testdox Should throw exception when order ID is negative.
+ */
+ public function test_throws_exception_for_negative_order_id(): void {
+ $this->expectException( \InvalidArgumentException::class );
+
+ $this->sut->process( -1 );
+ }
+}
+```
+
+### Key Elements
+
+| Element | Requirement |
+| ------- | ----------- |
+| `declare( strict_types = 1 )` | Required at file start |
+| Namespace | Match source location: `Automattic\WooCommerce\Tests\{path}` |
+| Base class | Extend `WC_Unit_Test_Case` |
+| SUT variable | Use `$sut` with docblock "The System Under Test." |
+| Test docblock | Use `@testdox` with sentence ending in `.` |
+| Return type | Use `void` for test methods |
+| Assertion messages | Include helpful context for failures |
+
## Test File Naming and Location
| Source | Test | Pattern |
diff --git a/CLAUDE.md b/CLAUDE.md
index da5572f894..025de66d87 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -27,7 +27,7 @@ WooCommerce is a WordPress e-commerce plugin organized as a monorepo with:
The `.ai/skills/` directory contains procedural HOW-TO instructions:
-- **`woocommerce-backend-dev`** - Backend PHP conventions (classes, methods, hooks, DI, testing)
+- **`woocommerce-backend-dev`** - Backend PHP conventions and unit tests. **Invoke before writing any PHP test files.**
- **`woocommerce-dev-cycle`** - Testing and linting workflows (PHP, JS, markdown)
- **`woocommerce-copy-guidelines`** - UI text standards (sentence case rules)
- **`woocommerce-code-review`** - Code review standards and critical violations to flag
diff --git a/plugins/woocommerce/changelog/update-skill-for-backend b/plugins/woocommerce/changelog/update-skill-for-backend
new file mode 100644
index 0000000000..11ad7d522c
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-skill-for-backend
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Update AI backend skill to always be invoked before writing tests