Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 MGatner
Copyright (c) 2019 CodeIgniter Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PHPUnit testing scaffold for CodeIgniter 4 modules
## Overview

Not a module itself but a testing scaffold for CodeIgniter 4 modules,
**module-tests** makes it easy to setup PHPUnit tests in your modules.
**ModuleTests** makes it easy to setup PHPUnit tests in your modules.

To read more on Unit Testing in CodeIgniter 4 visit the
[User Guide](https://codeigniter4.github.io/userguide/testing/index.html).
Expand Down Expand Up @@ -53,6 +53,23 @@ composer.lock
Examples of **composer.json** and **.gitignore** are located in the [examples/](examples/)
folder if you need a starting point.

### Paths

A number of framework and testing path are defined as constants during the
[bootstrap process](src/tests/_support/bootstrap.php). These default to the assumed locations
if you have a standard directory structure. If you move directories around you will need to
review these paths and set them appropriately.

* **APPPATH**: `$paths->appDirectory`
* **ROOTPATH**: `APPPATH . '../'`
* **FCPATH**: `ROOTPATH . 'public/'`
* **WRITEPATH**: `$paths->writableDirectory`
* **SYSTEMPATH**: `$paths->systemDirectory`
* **CIPATH**: `SYSTEMPATH . '../`
* **SUPPORTPATH**: `CIPATH . 'tests/_support/`
* **PROJECTSUPPORTPATH**: *bootstrap.php directory*
* **TESTPATH**: `PROJECTSUPPORTPATH . '../`

## Updating

As this repo is updated with bugfixes and improvements you will want to update your
Expand All @@ -74,3 +91,10 @@ in **build/logs/**.
## Code Coverage

See the docs on [Code Coverage](docs/COVERAGE.md).

## Project Testing

**ModuleTests** is designed to be added to your modular library which will be included into
other CodeIgniter 4 projects. If you are looking for a testing scaffold for applications
built using CodeIgniter 4 as their core framework, check out
[Codeigniter4Projects/ProjectTests](https://github.com/codeigniter4projects/project-tests).
8 changes: 4 additions & 4 deletions docs/COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

## Overview

**ci4-module-tests** comes preconfigured to handle code coverage in addition to the unit tests.
**ModuleTests** comes preconfigured to handle code coverage in addition to the unit tests.
You will need to have a code coverage driver installed to use this feature, such as
[Xdebug](https://xdebug.org).

## Setup

**ci4-module-tests** assumes your source code is in **src/**; if your code is somewhere else
**ModuleTests** assumes your source code is in **src/**; if your code is somewhere else
then you will need to modify the following line in your PHPUnit XML file:
```
<directory suffix=".php">./src</directory>
```

## PHPUnit.xml

**ci4-module-tests** includes a ready-to-use PHPUnit template in **phpunit.xml.dist**.
**ModuleTests** includes a ready-to-use PHPUnit template in **phpunit.xml.dist**.
Common practice is to create a local copy of this file as **phpunit.xml** and add any
sensitive or environment info (like database connections). Prevent **phpunit.xml** from
being tracked in your repo by adding it to **.gitignore**.
Expand All @@ -30,7 +30,7 @@ being tracked in your repo by adding it to **.gitignore**.

In addition to the code source mentioned above, PHPUnit can be configured to exclude files
that are not relevant to testing or would otherwise cause the coverage calculations to fail.
**ci4-module-tests** starts with a few files common to CodeIgniter 4 but you may need to add
**ModuleTests** starts with a few files common to CodeIgniter 4 but you may need to add
your own:
```
<exclude>
Expand Down
5 changes: 3 additions & 2 deletions examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
"php" : ">=7.2"
},
"require-dev": {
"phpunit/phpunit" : "^7.0",
"codeigniter4/codeigniter4": "dev-develop",
"mikey179/vfsstream": "1.6.*",
"mockery/mockery": "^1.0",
"codeigniter4/codeigniter4": "dev-develop"
"phpunit/phpunit" : "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class CreateTestTables extends Migration
{
protected $DBGroup = 'tests';

public function up()
{
$fields = [
Expand Down
21 changes: 16 additions & 5 deletions src/tests/_support/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@
define('APPPATH', realpath($paths->appDirectory) . DIRECTORY_SEPARATOR);
define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR);
define('FCPATH', realpath(ROOTPATH . 'public') . DIRECTORY_SEPARATOR);
define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR);
define('WRITEPATH', realpath($paths->writableDirectory) . DIRECTORY_SEPARATOR);
define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR);
define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR);
define('SUPPORTPATH', realpath(CIPATH . 'tests/_support') . DIRECTORY_SEPARATOR);

// Define necessary module test path constants
define('SUPPORTPATH', realpath(__DIR__) . DIRECTORY_SEPARATOR);
define('TESTPATH', realpath(SUPPORTPATH . '../') . DIRECTORY_SEPARATOR);
define('MODULEPATH', realpath(__DIR__ . '/../../') . DIRECTORY_SEPARATOR);
define('COMPOSER_PATH', MODULEPATH . 'vendor/autoload.php');
define('MODULESUPPORTPATH', realpath(__DIR__) . DIRECTORY_SEPARATOR);
define('TESTPATH', realpath(MODULESUPPORTPATH . '../') . DIRECTORY_SEPARATOR);
define('MODULEPATH', realpath(__DIR__ . '/../../') . DIRECTORY_SEPARATOR);
define('COMPOSER_PATH', MODULEPATH . 'vendor/autoload.php');

// Let's see if an app/Common.php file exists
if (file_exists(APPPATH . 'Common.php'))
{
require_once APPPATH . 'Common.php';
}

// Require system/Common.php
require_once SYSTEMPATH . 'Common.php';

// Set environment values that would otherwise stop the framework from functioning during tests.
if (! isset($_SERVER['app.baseURL']))
Expand Down