# Unit Tests

## Setup testing environment

In the theme root within your environment (localWP: "Open site shell" option) run:

```bash
./tests/install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host]
```

* `<db-name>` is the name of the db you want the run the tests in. We suggest creating a separate db form your dev db for testing.
* `<db-user>` Database username
* `<db-pass>` Database password
* `[db-host]` Database url and port. For localWP get the port from the url when opening Adminer.

Note for localWP on linux the command looks like this for localWP:\
`./tests/install-wp-tests.sh local-test root root localhost:10063`

Notes:

* On localWP, make sure you are running from the "Open site shell" menu option, so the environment is loaded
* On localWP, remove `--protocal=tcp` on line #141 of ./tests/install-wp-tests.sh
* On localWP, you can get the database port from the browser url after opening adminer
* You may need to install svn: `sudo apt install subversion`
* If you get an error that looks like: `Could not find /{path}/wordpress-tests-lib /includes/functions.php, have you run tests/install-wp-tests.sh ?` delete the temp folder and run the install again.

## Running the tests

Install phpunit and phpunit-polyfills form the theme root.

```bash
composer require "phpunit/phpunit=7.5.*"
composer require "yoast/phpunit-polyfills"
```

Run `./vendor/bin/phpunit`

The tests need phpunit v7. v8 and above currently don't work.

## Writing tests

* tests are located in the theme ./tests folder
* unit test files need to start with 'unit-test'
* unit test function need to start with 'test\_'
* phpunit documentation <https://phpunit.readthedocs.io/en/7.5>
* assertions: <https://phpunit.readthedocs.io/en/7.5/assertions.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.disciple.tools/local-setup/theme-phpunit-tests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
