Skip to content

Commit 97eade2

Browse files
Merge pull request #1655 from lameze/MDL-88495
MDL-88495 [docs] Document phpunit snapshot, restore and upgrade commands
2 parents 4f374cf + 2afb69a commit 97eade2

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

  • general/development/tools/phpunit

general/development/tools/phpunit/index.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,110 @@ define('TEST_LDAPLIB_BIND_PW', '*');
145145
define('TEST_LDAPLIB_DOMAIN', 'dc=yourcomputer,dc=local');
146146
```
147147

148+
## Maintaining the test environment {/* #maintaining-the-test-environment */}
149+
150+
{/* <!-- cspell:ignore dataroot, filedir, dbtype, mysnapshot --> */}
151+
152+
Once the environment has been initialised, the `admin/tool/phpunit/cli/util.php` script can manage it without a full re-initialisation via `init.php`. Run it with `--help` to see all of the available options:
153+
154+
```
155+
php public/admin/tool/phpunit/cli/util.php --help
156+
```
157+
158+
### Upgrading the environment after a change {/* #upgrading-the-environment-after-a-change */}
159+
160+
<Since version="5.3" issueNumber="MDL-88495" />
161+
162+
When you add, remove, or change a plugin (including bumping a plugin's version number), the test environment no longer matches the code and PHPUnit will refuse to run:
163+
164+
```
165+
Moodle PHPUnit environment was initialised for different version, please use:
166+
php public/admin/tool/phpunit/cli/init.php
167+
or php public/admin/tool/phpunit/cli/util.php --upgrade
168+
```
169+
170+
Instead of re-initialising the whole environment, you can upgrade just the installed plugins in place, which is significantly faster:
171+
172+
```
173+
php public/admin/tool/phpunit/cli/util.php --upgrade
174+
```
175+
176+
This runs the standard non-core upgrade against the existing test database — installing new plugins, running their upgrade steps, and applying plugin default settings — in the same way that completing an upgrade from the notifications page does on a normal site.
177+
178+
### Snapshots {/* #snapshots */}
179+
180+
<Since version="5.3" issueNumber="MDL-88495" />
181+
182+
Initialising the test environment from scratch is slow because it installs the full Moodle database. In continuous integration, where the same vanilla install is rebuilt for every job, this can dominate the run time. Snapshots let you capture an initialised environment once and restore it almost instantly on later runs.
183+
184+
A snapshot is a single `.zip` archive containing the test database structure and data together with the `filedir`. Snapshots are stored under `<phpunit_dataroot>/snapshots/` and are keyed by database type and Moodle version, so a snapshot taken for one version is not used against another.
185+
186+
#### Creating a snapshot {/* #creating-a-snapshot */}
187+
188+
Initialise the environment as usual, then create a snapshot:
189+
190+
```
191+
php public/admin/tool/phpunit/cli/util.php --snapshot
192+
```
193+
194+
By default the snapshot is named `<dbtype>-snapshot-<version>` (for example `pgsql-snapshot-2026072200`). You can give it a custom name with `--snapshot=NAME`:
195+
196+
```
197+
php public/admin/tool/phpunit/cli/util.php --snapshot=mysnapshot
198+
```
199+
200+
#### Listing snapshots {/* #listing-snapshots */}
201+
202+
```
203+
php public/admin/tool/phpunit/cli/util.php --list
204+
```
205+
206+
The names printed are exactly the values to pass to `--restore`:
207+
208+
```
209+
Available snapshots (use the full name shown with --restore=NAME):
210+
- pgsql-mysnapshot-2026072200
211+
- pgsql-snapshot-2026072200
212+
```
213+
214+
#### Restoring a snapshot {/* #restoring-a-snapshot */}
215+
216+
The database must be empty before restoring, so drop it first:
217+
218+
```
219+
php public/admin/tool/phpunit/cli/util.php --drop
220+
php public/admin/tool/phpunit/cli/util.php --restore=pgsql-snapshot-2026072200
221+
```
222+
223+
`--restore` rebuilds the database and `filedir` from the snapshot, which is much faster than a full `init.php`. Pass the exact name shown by `--list` (without the `.zip` extension). Snapshots are preserved by `--drop`, so you can drop and restore repeatedly.
224+
225+
#### Example CI workflow {/* #example-ci-workflow */}
226+
227+
The snapshot archive is intended to be cached by your CI system (GitHub Actions, GitLab CI, Jenkins, and so on). How the archive is stored between jobs is out of scope for Moodle and depends on your CI provider.
228+
229+
1. On a vanilla Moodle, initialise the environment and take a snapshot, then cache the archive from `<phpunit_dataroot>/snapshots/` keyed by the Moodle version:
230+
231+
```
232+
php public/admin/tool/phpunit/cli/init.php
233+
php public/admin/tool/phpunit/cli/util.php --snapshot
234+
```
235+
236+
2. On subsequent runs, restore the cached snapshot instead of re-installing:
237+
238+
```
239+
php public/admin/tool/phpunit/cli/util.php --restore=<name>
240+
```
241+
242+
3. Install the plugin under test, then upgrade the environment in place:
243+
244+
```
245+
php public/admin/tool/phpunit/cli/util.php --upgrade
246+
```
247+
248+
4. Run the tests.
249+
250+
If a new Moodle version is released the cached snapshot no longer matches, and the workflow should fall back to a fresh `init.php` before taking a new snapshot.
251+
148252
## Test execution {/* #test-execution */}
149253

150254
To execute all test suites from main configuration file execute the `vendor/bin/phpunit` script from your `$CFG->dirroot` directory.

0 commit comments

Comments
 (0)