|
| 1 | +# Creating Tests |
| 2 | + |
| 3 | +## Resources |
| 4 | +* [CodeIgniter 4 User Guide on Testing](https://codeigniter4.github.io/userguide/testing/index.html) |
| 5 | +* [PHPUnit docs](https://phpunit.readthedocs.io/en/8.3/index.html) |
| 6 | +* [Mockery docs](http://docs.mockery.io/en/latest/) |
| 7 | + |
| 8 | +## Test Cases |
| 9 | + |
| 10 | +Every test needs a *test case*, or class that your tests extend. CodeIgniter 4 |
| 11 | +provides a few that you may use directly: |
| 12 | +* `CodeIgniter\Test\CIUnitTestCase` - for basic tests with no other service needs |
| 13 | +* `CodeIgniter\Test\CIDatabaseTestCase` - for tests that need database access |
| 14 | + |
| 15 | +**ModuleTests** also provides some examples: |
| 16 | +* `ModuleTests\Support\DatabaseTestCase` - for database tests, pre-configured for migrations, seeds, and models from **tests/_support** |
| 17 | +* `ModuleTests\Support\SessionTestCase` - for session tests, pre-configured with a mock session driver |
| 18 | + |
| 19 | +Most of the time you will want to write your own test cases to hold functions and services |
| 20 | +common to your test suites. |
| 21 | + |
| 22 | +## Tests |
| 23 | + |
| 24 | +All tests go in the **tests/** directory. **ModuleTests** provides two generic |
| 25 | +subfolders for you, **unit** and **database** - but feel free to make your own. Each test file |
| 26 | +is a class that extends a **Test Case** (see above) and contains methods for the individual |
| 27 | +tests. These method names must start with the word "test" and should have descriptive names |
| 28 | +for precisely what they are testing: `testUserCanModifyFile()` `testOutputColorMatchesInput()` |
| 29 | +`testIsLoggedInFailsWithInvalidUser()` |
| 30 | + |
| 31 | +Writing tests is an art, and there are many resources available to help learn how. Review |
| 32 | +the links above and always pay attention to your [Code Coverage](docs/COVERAGE.md). |
| 33 | + |
| 34 | +### Database Tests |
| 35 | + |
| 36 | +**ModuleTests** provides examples for migrating, seeding, and testing against a mock |
| 37 | +or live<sup>1</sup> database. The example files can be modified or replaced with your own: |
| 38 | +* **tests/_support/Database/Migrations/create_test_tables.php** |
| 39 | +* **tests/_support/Database/Seeds/ExampleSeeder.php** |
| 40 | +* **tests/_support/Models/ExampleModel.php** |
| 41 | + |
| 42 | +Be sure to modify the test case (or create your own) to point to your seed and migrations |
| 43 | +and include any additional steps to be run before tests in the `setUp()` method: |
| 44 | +* **tests/_support/DatabaseTestCase.php** |
| 45 | + |
| 46 | +<sup>1</sup> Note: If you are using database tests that require a live database connection you will need |
| 47 | +to rename **phpunit.xml.dist** to **phpunit.xml**, uncomment the database configuration |
| 48 | +lines and add your connection details. Prevent **phpunit.xml** from being tracked in your |
| 49 | +repo by adding it to **.gitignore**. |
| 50 | + |
| 51 | +### Session Tests |
| 52 | + |
| 53 | +Similar to database testing, **ModuleTests** provides a test case pre-configured |
| 54 | +with the [mock session class](https://codeigniter4.github.io/userguide/testing/overview.html#mocking-services) |
| 55 | +to make testing sessions easier: |
| 56 | +* **tests/_support/SessionTestCase.php** |
0 commit comments