{"slug":"craft-pest","title":"craft-pest","summary":"Testing Craft CMS 5 plugins and modules with Pest: test isolation, database safety, and the markhuot/craft-pest-core harness. ALWAYS load when writing, running, fixing, or reviewing tests for a Craft plugin or module, and whenever a suite touches a real Craft install. Covers opt-","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-26T20:25:58.034825Z","repo":{"url":"https://github.com/michtio/craftcms-claude-skills","stars":90,"forks":11,"license":"MIT","updatedAt":"2026-11-26T19:56:57Z"},"bodyHtml":"<hr>\n<h2>name: craft-pest\ndescription: \"Testing Craft CMS 5 plugins and modules with Pest: test isolation, database safety, and the markhuot/craft-pest-core harness. ALWAYS load when writing, running, fixing, or reviewing tests for a Craft plugin or module, and whenever a suite touches a real Craft install. Covers opt-in rollback, tests/Pest.php + tests/bootstrap.php wiring, phpunit.xml.dist </h2>\n<h1>Testing Craft CMS Plugins with Pest</h1>\n<h2>Craft Version of This Project</h2>\n<p>!<code>sh ${CLAUDE_SKILL_DIR}/scripts/craft-version.sh</code></p>\n<p>If no Craft version line appears above, establish the version before any version-specific advice: <code>craftcms/cms</code> in <code>composer.lock</code> for a site, the lowest version the <code>craftcms/cms</code> constraint in <code>composer.json</code> allows for a plugin, or ask. Then check it against the version tags in this skill (<code>[5.10+]</code>, <code>[changed 5.12]</code>, <code>[seomatic 5.1+]</code>).</p>\n<p>Reference for testing Craft CMS 5 plugins and modules with Pest, primarily via <code>markhuot/craft-pest-core</code>.</p>\n<p>The dominant failure mode in Craft plugin testing is not a wrong assertion — it's a suite that <strong>writes to a database it shouldn't</strong>, or that <strong>passes only because of ambient state</strong> on the developer's install. Both are silent. Both look like a green suite. This skill leads with isolation for that reason: get the harness right first, then write tests.</p>\n<p><strong>Verified against <code>markhuot/craft-pest-core</code> 3.2.2 and <code>craftcms/cms</code> 5.10.12 (August 2026).</strong> Where a claim names a class or method, it was read in that package's source. craft-pest's own README and docs are not authoritative on these points — several of the behaviors below are unstated there.</p>\n<h2>Companion Skills — Load When Needed</h2>\n<ul>\n<li><strong><code>craftcms</code></strong> — Plugin/module architecture, elements, controllers, events, project config. Load when the code under test is being written or changed, not just exercised.</li>\n<li><strong><code>craft-php-guidelines</code></strong> — PHP standards for the test files themselves (PHPDocs, naming, ECS).</li>\n<li><strong><code>ddev</code></strong> — Every command runs through DDEV. Load for the correct invocation of a plugin's own suite inside a host project (<code>ddev exec --dir …</code>).</li>\n</ul>\n<h2>Documentation</h2>\n<ul>\n<li>Craft Pest: <a href=\"https://craft-pest.com\">https://craft-pest.com</a></li>\n<li>Pest PHP: <a href=\"https://pestphp.com/docs/installation\">https://pestphp.com/docs/installation</a></li>\n<li>Codeception (Craft's native harness): <a href=\"https://craftcms.com/docs/5.x/extend/testing.html\">https://craftcms.com/docs/5.x/extend/testing.html</a></li>\n</ul>\n<p>Use <code>WebFetch</code> for specific pages, but prefer reading <code>vendor/markhuot/craft-pest-core/src/</code> when the question is \"what does it actually do.\"</p>\n<h2>The Two Non-Negotiables</h2>\n<p>Everything else in this skill is technique. These two are the ones that cause data loss.</p>\n<h3>1. Rollback is opt-in — <code>TestCase</code> alone commits everything</h3>\n<p><code>markhuot\\craftpest\\test\\TestCase</code> boots Craft and mixes in ~15 traits (<code>ActingAs</code>, <code>RequestBuilders</code>, <code>DatabaseAssertions</code>, <code>Queues</code>, …). <strong><code>RefreshesDatabase</code> is not one of them.</strong> Only that trait opens a transaction (<code>setUpRefreshesDatabase()</code> → <code>beginTransaction()</code>) and rolls it back on teardown.</p>\n<p>So a <code>tests/Pest.php</code> that binds only <code>TestCase</code> produces a suite where every factory call, every <code>saveElement()</code>, every service write <strong>commits permanently</strong> to whatever database Craft booted against. The tests pass. The database fills up.</p>\n<pre><code>// tests/Pest.php — bind BOTH\nuses(\n    \\markhuot\\craftpest\\test\\TestCase::class,\n    \\markhuot\\craftpest\\test\\RefreshesDatabase::class,\n)-&gt;in(__DIR__);\n</code></pre>\n<p>If a suite genuinely needs committed data (rare — usually a sign the test should be restructured), scope the exception to that one file rather than dropping the trait globally.</p>\n<h3>2. The env override is CWD-bound — never run a plugin suite from a shared project root</h3>\n<p><code>InstallsCraft::loadPhpunitXmlEnvironmentVariables()</code> (a Pest <code>HandlesArguments</code> plugin, so it runs before Craft boots) looks for exactly two paths:</p>\n<pre><code>getcwd().'/phpunit.xml'\ngetcwd().'/phpunit.xml.dist'\n</code></pre>\n<p>It does <strong>not</strong> parse a <code>--configuration=</code> CLI flag. There is no fallback, no search upward, no argument inspection.</p>\n<p>The consequence is the dangerous part. This invocation looks like it isolates the plugin's suite:</p>\n<pre><code># UNSAFE for craft-pest-core suites\nddev craft pest -- --configuration=vendor/acme/my-plugin/phpunit.xml.dist\n</code></pre>\n<p>PHPUnit reads that config for test discovery, so tests are found and run — but <code>getcwd()</code> is the <em>project</em> root, so the <strong>plugin's <code>&lt;env&gt;</code> DB pins are never loaded</strong>. Craft boots against the live development database and, if <code>RefreshesDatabase</code> is also missing, writes to it permanently. That combination is how a suite silently creates thousands of orphaned elements in a shared install.</p>\n<p><strong>Rule: run a plugin's suite from the plugin's own root.</strong></p>\n<pre><code># From the plugin directory\nvendor/bin/pest\ncomposer test\n\n# From a host project, targeting the plugin's own root\nddev exec --dir /var/www/html/vendor/acme/my-plugin vendor/bin/pest\n</code></pre>\n<p>Treat the shared-root <code>--configuration=</code> invocation as unsafe for any craft-pest-core suite, including in CI. See the <code>ddev</code> skill for the container-side invocation.</p>\n<h2>Isolation Checklist</h2>\n<p>Run this against any plugin suite you inherit, write, or review. Each line has failed in practice.</p>\n<table>\n<thead>\n<tr>\n<th>Check</th>\n<th>Where</th>\n<th>Failure if missing</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>RefreshesDatabase</code> bound alongside <code>TestCase</code></td>\n<td><code>tests/Pest.php</code></td>\n<td>Every write commits permanently</td>\n</tr>\n<tr>\n<td><code>CRAFT_DB_DATABASE</code> pinned before Craft boots</td>\n<td><code>tests/bootstrap.php</code></td>\n<td>Suite runs against the dev database</td>\n</tr>\n<tr>\n<td>Fail-closed DB guard that <strong>throws</strong> (never <code>exit(1)</code>)</td>\n<td><code>tests/bootstrap.php</code></td>\n<td><code>exit(1)</code> hands the shell 0 under Pest — guard fails open on CI</td>\n</tr>\n<tr>\n<td><code>CRAFT_ENVIRONMENT</code> pinned uniquely per plugin</td>\n<td><code>phpunit.xml.dist</code></td>\n<td>Suites share server-scoped <code>GET_LOCK</code> names — <code>BusyResourceException</code>/deadlocks even across separate databases</td>\n</tr>\n<tr>\n<td><code>date_default_timezone_set('UTC')</code> <strong>after</strong> app creation</td>\n<td><code>tests/bootstrap.php</code></td>\n<td>Datetimes shift by the install's UTC offset</td>\n</tr>\n<tr>\n<td>Same pins present as <code>&lt;env&gt;</code> entries</td>\n<td><code>phpunit.xml.dist</code></td>\n<td>Correct-invocation path has no pins</td>\n</tr>\n<tr>\n<td>DB name + table prefix forced; coordinates <code>default=\"true\"</code></td>\n<td><code>phpunit.xml.dist</code></td>\n<td>A forced local hostname breaks CI runners</td>\n</tr>\n<tr>\n<td><code>Install::safeUp()</code> guarded with <code>createIndexIfMissing()</code> / <code>Db::findForeignKey()</code></td>\n<td><code>src/migrations/Install.php</code></td>\n<td>Duplicate keys accumulate to MySQL's 64-per-table cap</td>\n</tr>\n<tr>\n<td>Sites created per-test and deleted in <code>afterEach()</code></td>\n<td>tests, <code>tests/Pest.php</code></td>\n<td>Durable sites mutate the shared test database</td>\n</tr>\n<tr>\n<td>Suite invoked from the plugin's own root</td>\n<td><code>composer test</code>, CI, DDEV</td>\n<td><code>&lt;env&gt;</code> pins silently ignored</td>\n</tr>\n<tr>\n<td>Plugin under test explicitly installed</td>\n<td><code>tests/bootstrap.php</code></td>\n<td>Works only on an install that already has it</td>\n</tr>\n<tr>\n<td>Edition pinned explicitly</td>\n<td><code>beforeEach()</code></td>\n<td>Passes on Pro, fails on Solo/Team</td>\n</tr>\n<tr>\n<td>Count assertions scoped to test-created rows</td>\n<td>each test</td>\n<td>Passes on a seeded install, fails when clean</td>\n</tr>\n<tr>\n<td>Audit/event sinks muted on <strong>every</strong> surface</td>\n<td>shared helper</td>\n<td>Tests write real audit rows</td>\n</tr>\n<tr>\n<td>Queue replaced with a stub</td>\n<td>shared helper</td>\n<td>Tests drain or grow a real backlog</td>\n</tr>\n<tr>\n<td>A Pest job actually runs in CI</td>\n<td><code>.github/workflows/</code></td>\n<td>The suite decays unnoticed</td>\n</tr>\n</tbody>\n</table>\n<h2>Reference Files</h2>\n<p>Read the reference file(s) your task needs — each costs input tokens on every turn.</p>\n<p><strong>Task examples:</strong></p>\n<ul>\n<li>\"Set up Pest for a new plugin\" → <code>isolation.md</code> (bootstrap + phpunit.xml) then <code>patterns.md</code></li>\n<li>\"Tests are writing to my dev database / created thousands of entries\" → <code>isolation.md</code></li>\n<li>\"Suite passes locally but fails on CI or against a fresh test DB\" → <code>isolation.md</code> (Ambient state) + <code>shared-state.md</code></li>\n<li>\"Write a test for a controller action / element factory / queue job\" → <code>patterns.md</code></li>\n<li>\"Test dynamically-registered permissions\" → <code>craft-state.md</code> (Permission-tree memoization)</li>\n<li>\"Test something that requires a logged-in user\" → <code>craft-state.md</code> (Simulating a login)</li>\n<li>\"Tests wrote real audit rows / fired real webhooks\" → <code>craft-state.md</code> (Muting event surfaces)</li>\n<li>\"Raw SQL fixture isn't treated as expired\" → <code>craft-state.md</code> (Fixture timestamps)</li>\n<li>\"Tests pollute a shared playground install\" → <code>shared-state.md</code></li>\n<li>\"<code>Install.php</code> changes aren't reaching the test database\" → <code>shared-state.md</code> (Schema drift)</li>\n<li>\"Expiry/date assertions fail intermittently, or datetimes come back hours off\" → <code>isolation.md</code> (Pin the process timezone)</li>\n<li>\"Suite connects fine locally but can't reach the database on CI\" → <code>isolation.md</code> (Force the database name, default everything else) + <code>ci.md</code></li>\n<li>\"Install fails with too many keys / duplicate indexes piling up\" → <code>shared-state.md</code> (Install migrations must be idempotent)</li>\n<li>\"Set up a multi-site test / my test site's queries ignore siteId\" → <code>craft-state.md</code> (Site fixtures) + the <code>craftcms</code> skill's <code>architecture.md</code></li>\n<li>\"My fixture-cleanup sweep isn't deleting anything / fixtures leak into a shared install\" → <code>craft-state.md</code> (Prefix-matching sweeps)</li>\n<li>\"Test passes alone but fails in the suite / service returns stale data\" → <code>craft-state.md</code> (Service caches go stale when craft-pest swaps components)</li>\n<li>\"Wire tests into CI\" → <code>ci.md</code></li>\n<li>\"Make the bootstrap refuse to run against the wrong database\" → <code>isolation.md</code> (fail-closed guard — throw, never <code>exit(1)</code>)</li>\n<li>\"Two suites deadlock when run in parallel / <code>BusyResourceException</code> on a fresh empty database\" → <code>isolation.md</code> (pin <code>CRAFT_ENVIRONMENT</code>)</li>\n<li>\"Does this suite even run in CI?\" → <code>ci.md</code> (sweep all workflows, not just <code>tests.yaml</code>)</li>\n<li>\"<code>--filter</code> run fails on cookieValidationKey but the full suite passes\" → <code>isolation.md</code> (--filter subsets)</li>\n<li>\"Refactor a large untested controller/service\" → <code>patterns.md</code> (Pin behaviour before refactoring)</li>\n<li>\"The fix requires changing an existing test / is my new test actually testing anything?\" → <code>patterns.md</code> (A test can encode the bug; Prove a fix by breaking it)</li>\n<li>\"<code>TestCaseAlreadyInUse</code> from my uses() rules / per-directory TestCases\" → <code>patterns.md</code> (uses() rules)</li>\n<li>\"CP controller test fails on sites, URLs, or asset directories\" → <code>craft-state.md</code> (CP-surface controller tests)</li>\n</ul>\n<table>\n<thead>\n<tr>\n<th>Reference</th>\n<th>Scope</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>references/isolation.md</code></td>\n<td>Database isolation: <code>tests/bootstrap.php</code>, process timezone, <code>phpunit.xml.dist</code> (force the DB name, default the connection coordinates), <code>RefreshesDatabase</code>, <code>InstallsCraft</code> boot vs plugin install, invocation paths, ambient-state assumptions (editions, counts, pre-existing fixtures)</td>\n</tr>\n<tr>\n<td><code>references/craft-state.md</code></td>\n<td>Craft internals that bite in tests: permission-tree memoization, login/session gates, UTC fixture timestamps, muting audit/event surfaces, per-test site fixtures, component swapping and stale service caches, queue stubs, project-config writes</td>\n</tr>\n<tr>\n<td><code>references/patterns.md</code></td>\n<td>Writing the tests: Pest vs Codeception, pure unit tests, what a green suite does not prove, factories, HTTP, queue, database assertions, multi-site, mocking Craft services, console commands, events, file/test conventions</td>\n</tr>\n<tr>\n<td><code>references/shared-state.md</code></td>\n<td>Suites that run against a shared or long-lived install: restore-what-you-found, <code>Install.php</code> vs migration drift in the test DB, idempotent <code>Install</code> migrations, self-seeding, request-IP fixtures</td>\n</tr>\n<tr>\n<td><code>references/ci.md</code></td>\n<td>CI wiring: <code>check-cs</code> not <code>fix-cs</code>, a real Pest job, invocation from the plugin root, fresh-database verification</td>\n</tr>\n</tbody>\n</table>\n<h2>Further Pitfalls (detail in <code>patterns.md</code>)</h2>\n<ul>\n<li><strong>Codeception vs Pest</strong>: use Pest + craft-pest-core for new plugin work, Codeception only for Craft core or an existing Codeception suite; pure-logic tests extend <code>PHPUnit\\Framework\\TestCase</code> without booting Craft. See <code>patterns.md</code> (Two harnesses; Pure unit tests).</li>\n<li><strong>A green suite is not proof</strong>: service-layer tests never see the HTTP layer, a seeded dev install is not authoritative, a console harness is not a browser, one long-lived process is not a sequence of requests, and a suite not in CI rots. See <code>patterns.md</code> (What a green suite does not prove).</li>\n</ul>\n","files":[{"path":"references/ci.md","sizeBytes":6071,"isText":true},{"path":"references/craft-state.md","sizeBytes":19885,"isText":true},{"path":"references/isolation.md","sizeBytes":21246,"isText":true},{"path":"references/patterns.md","sizeBytes":18173,"isText":true},{"path":"references/shared-state.md","sizeBytes":10477,"isText":true},{"path":"scripts/craft-version.sh","sizeBytes":7799,"isText":true},{"path":"SKILL.md","sizeBytes":12887,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-26T20:26:10.70458Z","sha256":"7D150B8B81A2F5FA7837CAE3AE66EF508C4E68E9F0D92ABD5EE0442D074F4D3E","sizeBytes":39948},"review":null,"source":{"repositoryUrl":"https://github.com/michtio/craftcms-claude-skills","path":"skills/craft-pest","license":"MIT","commit":"93e135df3d734ebdf36ec4b2f352e1a0daa03c85","subtreeSha":"107EC1D415D7A2F098926838010D5BD27475C93318AC86A6B34AA520E5807352","lastSyncedAt":"2026-11-26T20:25:56.895968Z"},"reviewedAt":"2026-11-26T20:26:35.600256Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/michtio/craftcms-claude-skills/tree/main/skills/craft-pest"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install michtio-craftcms-claude-skills@llmmart"},{"target":"git","command":"git clone https://github.com/michtio/craftcms-claude-skills.git"}]}