|
1 | 1 | # ci4-module-tests
|
2 |
| -Testing scaffold for PHPUnit tests in CodeIgniter 4 |
| 2 | + |
| 3 | +PHPUnit testing scaffold for CodeIgniter 4 modules |
3 | 4 |
|
4 | 5 | ## Overview
|
5 | 6 |
|
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. |
| 7 | +Not a module itself but a testing scaffold for CodeIgniter 4 modules, |
| 8 | +**ci4-module-tests** makes it easy to setup PHPUnit tests in your modules. |
9 | 9 |
|
10 |
| -For more on Testing in CodeIgniter 4 visit the |
11 |
| -[User Guide](https://codeigniter4.github.io/CodeIgniter4/testing/). |
| 10 | +To read more on Unit Testing in CodeIgniter 4 visit the |
| 11 | +[User Guide](https://codeigniter4.github.io/userguide/testing/index.html). |
12 | 12 |
|
13 | 13 | ## Installation
|
14 | 14 |
|
15 |
| -### New Projects |
16 |
| - |
17 |
| -1. Clone this repo and merge the all files from **src/** into the root of your module. |
18 |
| -2. From your package root run `composer install` to install all the required support packages. |
19 |
| -3. Start your module code in **src/** and add your namespace to **composer.json**'s `autoload` |
20 |
| -4. Run `composer test` to initiate the tests. |
21 |
| - |
22 |
| -### Existing Projects |
23 |
| - |
24 |
| -Unless you are starting fresh you likely will already have your own versions of some of the |
25 |
| -package files in **src/**. In this case you will need to be sure to merge some necessary |
26 |
| -settings for **CIModuleTests**. In **composer.json**: |
27 |
| -* `repositories` needs an entry for `https://github.com/codeigniter4/CodeIgniter4` |
28 |
| -* `require-dev` needs the CodeIgniter 4 repo, PHPUnit, and Mockery |
29 |
| -* `autoload-dev` must supply the PSR4 namespace for the test supports |
30 |
| -See the provided [composer.json](src/composer.json) for examples. |
31 |
| - |
32 |
| -Also review **src/.gitignore** for helpful additions so you don't track test results. |
| 15 | +Clone or download this repo and place **src/tests** and **src/phpunit.xml.dist** in your |
| 16 | +project root. Add the following lines to **composer.json**: |
| 17 | +``` |
| 18 | + "repositories": [ |
| 19 | + { |
| 20 | + "type": "vcs", |
| 21 | + "url": "https://github.com/codeigniter4/CodeIgniter4" |
| 22 | + } |
| 23 | + ], |
| 24 | + "minimum-stability": "dev", |
| 25 | + "require-dev": { |
| 26 | + "phpunit/phpunit" : "^7.0", |
| 27 | + "mockery/mockery": "^1.0", |
| 28 | + "codeigniter4/codeigniter4": "dev-develop" |
| 29 | + }, |
| 30 | + "autoload-dev": { |
| 31 | + "psr-4": { |
| 32 | + "CIModuleTests\\Support\\": "tests/_support" |
| 33 | + } |
| 34 | + }, |
| 35 | + "scripts": { |
| 36 | + "test": "phpunit", |
| 37 | + "post-update-cmd": [ |
| 38 | + "composer dump-autoload" |
| 39 | + ] |
| 40 | + } |
| 41 | +``` |
33 | 42 |
|
34 |
| -### phpunit.xml |
| 43 | +If you are using version tracking you should exclude test results by adding this to |
| 44 | +**.gitignore**: |
| 45 | +``` |
| 46 | +vendor/ |
| 47 | +build/ |
| 48 | +phpunit*.xml |
| 49 | +phpunit |
| 50 | +composer.lock |
| 51 | +``` |
35 | 52 |
|
36 |
| -**src/** includes a ready-to-use PHPUnit template in **phpunit.xml.dist**. You can keep this |
37 |
| -as is but if you plan to add environment info (like database connections) be sure to rename |
38 |
| -it to **phpunit.xml** and prevent it from being tracked via **.gitignore**. |
| 53 | +Examples of **composer.json** and **.gitignore** are located in the [examples/](examples/) |
| 54 | +folder if you need a starting point. |
39 | 55 |
|
40 |
| -## Customizing |
| 56 | +## Updating |
41 | 57 |
|
42 |
| -The **_support** directory comes loaded with easy-to-use examples of test cases and their |
43 |
| -support files, but you will probably want to modify or replace these with your own versions. |
44 |
| -There is no harm in leaving files in place you do not need. |
45 |
| -* **tests/_support/DatabaseTestCase.php** |
46 |
| -* **tests/_support/SessionTestCase.php** |
47 |
| -* **tests/_support/DatabaseTestCase.php** |
48 |
| -* **tests/_support/Database/Migrations/create_test_tables.php** |
49 |
| -* **tests/_support/Database/Seeds/ExampleSeeder.php** |
50 |
| -* **tests/_support/Models/ExampleModel.php** |
| 58 | +As this repo is updated with bugfixes and improvements you will want to update your |
| 59 | +modules that use it. Because files need to be copied in place manually you will have to |
| 60 | +handle updates manually by cloning or downloading this repo and merging changed files |
| 61 | +into your project. "Watch" this repo to be notified of new releases and changes. |
51 | 62 |
|
52 | 63 | ## Creating Tests
|
53 | 64 |
|
54 |
| -All tests go in the **tests/** directory. There are two generic subfolders for you, |
55 |
| -**unit** and **database** but feel free to make your own. Tests must extend the appropriate |
56 |
| -test case: |
57 |
| -* For basic tests extend `CodeIgniter\Test\CIUnitTestCase` |
58 |
| -* For database tests extend `CIModuleTests\Support\DatabaseTestCase` |
59 |
| -* For session tests extend `CIModuleTests\Support\SessionTestCase` |
60 |
| - |
61 |
| -Tests are individual methods within each file. Method names must start with the word "test": |
62 |
| -`testUserSync()` `testOutputColor()` `testFooBar()` |
63 |
| - |
64 |
| -### Database Tests |
| 65 | +See the docs on [Creating Tests](docs/CREATING.md). |
65 | 66 |
|
66 |
| -If you are using database tests that require a live database connect you will need to |
67 |
| -rename **phpunit.xml.dist** to **phpunit.xml**, uncomment the database configuration lines |
68 |
| -and add your connection details. Example directories and files are provided for test Seeds |
69 |
| -and Models, which you can modify or replace with your own. Also be sure to modify |
70 |
| -**tests/_support/DatabaseTestCase.php** to point to your seed and include any additional |
71 |
| -steps in `setUp()`. |
| 67 | +## Running Tests |
72 | 68 |
|
73 |
| -### Session Tests |
74 |
| - |
75 |
| -Similarly there is a pre-configured test case available with a mock session configured to |
76 |
| -make testing sessions easy: **tests/_support/SessionTestCase.php**. |
| 69 | +From your package root run `composer install` (or `composer upgrade`) to install all the |
| 70 | +required support packages, then run `composer test` to initiate the tests. Test results |
| 71 | +and code coverage will be written to standard output and formatted log versions will go |
| 72 | +in **build/logs/**. |
77 | 73 |
|
78 | 74 | ## Code Coverage
|
79 | 75 |
|
80 |
| -**CIModuleTests** comes preconfigured to run code coverage as part of the testing. You will |
81 |
| -need to have a code coverage driver installed to use this feature, such as |
82 |
| -[Xdebug](https://xdebug.org). **CIModuleTests** assumes your source code is in **src/**; |
83 |
| -if your code is somewhere else then modify the following line in **phpunit.xml.dist** : |
84 |
| -``` |
85 |
| -<directory suffix=".php">./src</directory> |
86 |
| -``` |
87 |
| -Other common modifications would be removing the attributes from the whitelist element: |
88 |
| -``` |
89 |
| -<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true"> |
90 |
| -``` |
91 |
| -... and adjusting the output location and format of the coverage reports: |
92 |
| -``` |
93 |
| -<logging> |
94 |
| - <log type="coverage-clover" target="build/logs/clover.xml"/> |
95 |
| - <log type="coverage-html" target="build/logs/html"/> |
96 |
| - <log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/> |
97 |
| -</logging> |
98 |
| -``` |
99 |
| - |
100 |
| -## Updating |
101 |
| - |
102 |
| -As this repo is updated with bugfixes and improvements you will want to update the code |
103 |
| -merged into your modules. Because tests need to be top-level and should not include their |
104 |
| -own repository info you will need to handle updates manually. |
105 |
| - |
106 |
| -If you added **CIModuleTests** to your package via Composer you can access the latest |
107 |
| -version by running `composer update` and merging files from |
108 |
| -**vendor/mgatner/ci-module-tests/src/** into your **tests/** directory. |
| 76 | +See the docs on [Code Coverage](docs/COVERAGE.md). |
0 commit comments