Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.

Commit 4122733

Browse files
committed
Add docs and examples
1 parent 47ebca2 commit 4122733

File tree

6 files changed

+105
-11
lines changed

6 files changed

+105
-11
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
# ci4-module-tests
22
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()`.

src/composer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "mgatner/ci-module-tests",
3+
"description": "Testing scaffold for PHPUnit tests in CodeIgniter 4",
4+
"keywords": [
5+
"codeigniter",
6+
"codeigniter4",
7+
"phpunit",
8+
"tests",
9+
"modules",
10+
"coverage"
11+
],
12+
"homepage": "https://github.com/mgatner/ci-module-tests",
13+
"license": "MIT",
14+
"authors": [
15+
{
16+
"name": "Matthew Gatner",
17+
"email": "mgatner@tattersoftware.com",
18+
"homepage": "https://tattersoftware.com",
19+
"role": "Developer"
20+
}
21+
],
22+
"repositories": [
23+
{
24+
"type": "vcs",
25+
"url": "https://github.com/codeigniter4/CodeIgniter4"
26+
}
27+
],
28+
"require": {
29+
"php" : ">=7.2"
30+
},
31+
"require-dev": {
32+
"phpunit/phpunit" : "^7.0",
33+
"mockery/mockery": "^1.0",
34+
"codeigniter4/codeigniter4": "dev-develop"
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"CIModuleTests\\Support\\": "tests/_support"
39+
}
40+
},
41+
"scripts": {
42+
"test": "phpunit",
43+
"post-update-cmd": [
44+
"composer dump-autoload"
45+
]
46+
}
47+
}

src/phpunit.xml.dist

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
</logging>
3131

3232
<php>
33-
<env name="app.baseURL" value="http://example.com"/>
34-
<env name="CI_ENVIRONMENT" value="testing"/>
35-
<!-- <env name="database.tests.DBDriver" value="MySQLi"/>-->
36-
<!-- <env name="database.tests.database" value="mythauth"/>-->
37-
<!-- <env name="database.tests.username" value="root"/>-->
38-
<env name="database.tests.DBDriver" value="SQLite3"/>
39-
<env name="database.tests.database" value=":memory:"/>
40-
<!-- <env name="database.tests.DBPrefix" value="handlers_"/>-->
33+
<env name="app.baseURL" value="http://example.com"/>
34+
<env name="CI_ENVIRONMENT" value="testing"/>
35+
<!-- <env name="database.tests.hostname" value="localhost"/> -->
36+
<!-- <env name="database.tests.database" value="tests"/> -->
37+
<!-- <env name="database.tests.username" value="tests_user"/> -->
38+
<!-- <env name="database.tests.password" value=""/> -->
39+
<!-- <env name="database.tests.DBDriver" value="MySQLi"/> -->
40+
<!-- <env name="database.tests.DBPrefix" value="tests_"/> -->
41+
<env name="database.tests.database" value=":memory:"/>
42+
<env name="database.tests.DBDriver" value="SQLite3"/>
4143
</php>
4244
</phpunit>

src/tests/_support/DatabaseTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DatabaseTestCase extends \CodeIgniter\Test\CIDatabaseTestCase
1414
*
1515
* @var string
1616
*/
17-
protected $seed = 'ExampleSeeder';
17+
protected $seed = 'CIModuleTests\Support\Database\Seeds\ExampleSeeder';
1818

1919
/**
2020
* The path to where we can find the test Seeds directory.

src/tests/database/ExampleDatabaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public function setUp(): void
88

99
}
1010

11-
public function simpleDatabaseTest()
11+
public function testDatabaseSimple()
1212
{
1313
$model = new \CIModuleTests\Support\Models\ExampleModel();
1414

src/tests/unit/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public function setUp(): void
88

99
}
1010

11-
public function simpleTest()
11+
public function testSimple()
1212
{
1313
$test = defined('APPPATH');
1414
$this->assertTrue($test);

0 commit comments

Comments
 (0)