|
1 | 1 | # ci4-module-tests |
2 | 2 | Testing scaffold for PHPUnit tests in CodeIgniter 4 |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +**CIModuleTests** is intended to be integrated into third-party modules for CodeIgniter 4. |
| 7 | +It provides a scaffold and basic examples of how to perform module testing dependent on the |
| 8 | +framework without being integrated into a specific project. |
| 9 | + |
| 10 | +For more on Testing in CodeIgniter 4 visit the |
| 11 | +[User Guide](https://codeigniter4.github.io/CodeIgniter4/testing/). |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +1. Clone this repo and merge the **tests** folder and **phpunit.xml.dist** and |
| 16 | +**composer.json**<sup>1</sup> files from **src/** into the root of your module. |
| 17 | +2. From your package root run `composer install` to install all the required support packages. |
| 18 | +3. Run `composer test` to initiate the tests. |
| 19 | + |
| 20 | +<sup>1</sup> Note: Unless you are starting fresh you likely will already have your own version of |
| 21 | +**composer.json**, in which case you will need to be sure to merge the following settings |
| 22 | +for **CIModuleTests**: |
| 23 | +* `repositories` needs an entry for `https://github.com/codeigniter4/CodeIgniter4` |
| 24 | +* `require-dev` needs the CodeIgniter 4 repo, PHPUnit, and Mockery |
| 25 | +* `autoload-dev` must supply the PSR4 namespace for the test supports |
| 26 | + |
| 27 | +See the provided [composer.json](src/composer.json) for examples. |
| 28 | + |
| 29 | +## Creating Tests |
| 30 | + |
| 31 | +All tests go in the **tests/** directory. There are two generic subfolders for you, |
| 32 | +**unit** and **database** but feel free to make your own. Tests must extend the appropriate |
| 33 | +test case: |
| 34 | +* For basic tests extend `CodeIgniter\Test\CIDatabaseTestCase` |
| 35 | +* For database tests extend `CIModuleTests\Support\DatabaseTestCase` |
| 36 | + |
| 37 | +Tests are individual methods within each file. Method names must start with the word "test": |
| 38 | +`testUserSync()` `testOutputColor()` `testFooBar()` |
| 39 | + |
| 40 | +## Database Tests |
| 41 | + |
| 42 | +If you are using database tests that require a live database connect you will need to edit |
| 43 | +**phpunit.xml.dist**, uncomment the database configuration lines and add your connection |
| 44 | +details. Example directories and files are provided for test Seeds and Models, which you |
| 45 | +can modify or replace with your own. Also be sure to modify |
| 46 | +**tests/_support/DatabaseTestCase.php** to point to your seed and include any additional |
| 47 | +steps in `setUp()`. |
0 commit comments