Product SiteDocumentation Site

1.5. PHPUnit tests

MantisBT has a suite of PHPUnit tests, which can be found in the /tests directory. It is organized in 3 distinct Test Suites, defined in the phpunit.xml file:

Warning

The unit tests are currently only covering a small part of the code base, focusing mainly on the REST and SOAP APIs. The MantisBT Core functions are for the most part only tested indirectly through the APIs.
You are encouraged to improve the test suite, by adding new tests for the patches you are submitting. Please remember that your changes must not break existing tests.
New Tests should be stored in the subdirectory corresponding to the Test Suite they belong to; the file should be named XxxxxTest.php, where Xxxxx is the Test's name. The Test.php suffix allows the script to be automatically picked up by PHPUnit when running the Test Suite. Without this suffix, the new script will have to be declared manually with a <file> tag in the phpunit.xml file's relevant <testsuite> section.

1.5.1. Initial Setup

This section describes the initial configuration required to run the unit tests.
PHPUnit
The software is normally installed via Composer when the MantisBT repository is initialized with composer install, which ensures that you have the appropriate version to run the unit tests.
It is also possible to use another instance of PHPUnit, in which case you should refer to the composer.json file to find out which version to use.
MantisBT
MantisBT must be installed and operational on your computer, per the admin guide. Additionally, the following seed data must be created to ensure that all test cases can be executed
  • 1 Project
  • 3 Project Versions: Old (released, obsolete), Active (released, not obsolete) and Future (not released, not obsolete)
  • 2 Tags
  • Anonymous user account (protected, enabled and with VIEWER access level)
  • 1 REST API Token for the Administrator account used to run the tests

Warning

If the MantisBT instance is to be used for other purposes than unit testing, it is strongly recommended to create a dedicated project for the unit tests, to avoid any side-effects as the tests will generate a large number of various types of database records.
Configuration
The following variables must be set in your config_inc.php file to ensure all PHPUnit tests are executed. Without this, some tests will be skipped
$g_allow_no_category = ON;
$g_due_date_update_threshold = DEVELOPER;
$g_due_date_view_threshold = DEVELOPER;
$g_enable_product_build = ON;
$g_enable_project_documentation = ON;
$g_time_tracking_enabled = ON;
$g_time_tracking_enabled = ON;
$g_allow_anonymous_login = ON;
$g_anonymous_account = 'username'; # of the anonymous account created above
PHPUnit Bootstrap file
The tests are configured using a bootstrap.php file, which must be adjusted to run the tests on your local installation. The tests/boostrap.php.sample file contains the settings you will need to adjust to run all the tests.
At the minimum, you should set the following variables
  • MANTIS_TESTSUITE_USERNAME, MANTIS_TESTSUITE_PASSWORD and MANTIS_TESTSUITE_PROJECT_ID
  • For the SOAP Tests: MANTIS_TESTSUITE_SOAP_ENABLED and MANTIS_TESTSUITE_SOAP_HOST
  • For the REST API Tests: MANTIS_TESTSUITE_REST_ENABLED, MANTIS_TESTSUITE_REST_HOST and MANTIS_TESTSUITE_API_TOKEN
SOAP extension
The PHP SOAP extension is required to run the SOAP tests.