• Stars
    star
    581
  • Rank 76,901 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An easier way to use PHPUnit with CodeIgniter 3.x.

ci-phpunit-test for CodeIgniter 3.x

Latest Stable Version Total Downloads Latest Unstable Version License

Scrutinizer Code Quality Coverage Status Build Status



An easier way to use PHPUnit with CodeIgniter 3.x.

  • You don't have to modify CodeIgniter core files at all.
  • You can write controller tests easily.
  • Nothing is untestable, maybe.
  • Well documented.

Screenshot: Running tests on NetBeans 8.1

Requirements

  • PHP 7.3 or later
    • If you use Monkey Patching, you must use PHP-Parser 4.2 or later as a Composer dependency.
  • CodeIgniter 3.x
    • If you want to upgrade to CodeIgniter4, see #361.
  • PHPUnit 9.3 or later
    • If you want to use PHPUnit 9.2 or earlier, please use ci-phpunit-test 2.x.

Optional

  • NetBeans
    • Go to Project Properties > Testing > PHPUnit, check Use Custom Test Suite checkbox, and select application/tests/_ci_phpunit_test/TestSuiteProvider.php.

Change Log

See Change Log.

Folder Structure

codeigniter/
├── application/
│   └── tests/
│        ├── _ci_phpunit_test/ ... don't touch! files ci-phpunit-test uses
│        ├── Bootstrap.php     ... bootstrap file for PHPUnit
│        ├── DbTestCase.php    ... DbTestCase class
│        ├── TestCase.php      ... TestCase class
│        ├── controllers/      ... put your controller tests
│        ├── libraries/        ... put your library tests
│        ├── mocks/
│        │   └── libraries/    ... mock libraries
│        ├── models/           ... put your model tests
│        └── phpunit.xml       ... config file for PHPUnit
└── vendor/

Installation

Manual Installation

  1. Download latest ci-phpunit-test from https://github.com/kenjis/ci-phpunit-test/releases.
  2. Unzip and copy application/tests folder into your application folder in CodeIgniter project.

That's it.

Composer Installation

$ cd /path/to/codeigniter/
$ composer require kenjis/ci-phpunit-test:^3.0 --dev

And run install.php:

$ php vendor/kenjis/ci-phpunit-test/install.php --from-composer
  • The above command always overwrites existing files.
  • You must run it at CodeIgniter project root folder.
  • You can specify your application and public folder with option arguments, if you use custom folder paths.
$ php vendor/kenjis/ci-phpunit-test/install.php -a <application_dir> -p <public_dir> -t <unittest_dir>

So the default would be:

$ php vendor/kenjis/ci-phpunit-test/install.php -a application -p public -t application/tests

Upgrading

Manual Upgrading

  1. Download latest ci-phpunit-test from https://github.com/kenjis/ci-phpunit-test/releases.
  2. Unzip and replace application/tests/_ci_phpunit_test folder.
  3. Read Change Log.

Composer Upgrading

$ cd /path/to/codeigniter/
$ composer update kenjis/ci-phpunit-test

Read Change Log.

If you want to remove application/test/_ci_phpunit_test/

If you're upgrading from a previous version of ci-phpunit-test that created an application/test/_ci_phpunit_test directory and now want to directly use ci-phpunit-test from Composer, you have a couple of additional steps:

  1. Delete the old test library directory:
    $ rm -rf /path/to/codeigniter/application/tests/_ci_phpunit_test
  2. Edit the application/tests/Bootstrap.php file. At the bottom of the "set the main path constants" section, add the following:
    define('CI_PHPUNIT_TESTPATH', implode(
        DIRECTORY_SEPARATOR,
        [dirname(APPPATH), 'vendor', 'kenjis', 'ci-phpunit-test', 'application', 'tests', '_ci_phpunit_test']
    ).DIRECTORY_SEPARATOR);
    And replace any references to __DIR__ . '/_ci_phpunit_test/ or TESTPATH . '_ci_phpunit_test with CI_PHPUNIT_TESTPATH . '. (So, for example, __DIR__ . '/_ci_phpunit_test/CIPHPUnitTest.php' would become CI_PHPUNIT_TESTPATH . '/CIPHPUnitTest.php'.)

How to Run Tests

You need to install PHPUnit before running tests.

If you use Composer:

$ composer require phpunit/phpunit --dev

Run All Tests

$ cd /path/to/codeigniter/
$ vendor/bin/phpunit -c application/tests/
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 00:00.102, Memory: 12.00 MB

OK (3 tests, 3 assertions)

Generating code coverage report in Clover XML format ... done [00:00.002]

Generating code coverage report in HTML format ... done [00:00.012]

To generate coverage report, Xdebug is needed.

Run a Single Test

If you want to run a single test case file:

$ vendor/bin/phpunit -c application/tests/ application/tests/models/Category_model_test.php

How to Write Tests

As an example, a test case class for Inventory_model would be as follows:

<?php

class Inventory_model_test extends TestCase
{
    public function setUp(): void
    {
        $this->resetInstance();
        $this->CI->load->model('Inventory_model');
        $this->obj = $this->CI->Inventory_model;
    }

    public function test_get_category_list()
    {
        $expected = [
            1 => 'Book',
            2 => 'CD',
            3 => 'DVD',
        ];
        $list = $this->obj->get_category_list();
        foreach ($list as $category) {
            $this->assertEquals($expected[$category->id], $category->name);
        }
    }

    public function test_get_category_name()
    {
        $actual = $this->obj->get_category_name(1);
        $expected = 'Book';
        $this->assertEquals($expected, $actual);
    }
}

As an example, a test case class for Welcome controller would be as follows:

<?php

class Welcome_test extends TestCase
{
    public function test_index()
    {
        $output = $this->request('GET', 'welcome/index');
        $this->assertStringContainsString(
            '<title>Welcome to CodeIgniter</title>', $output
        );
    }
}

See How to Write Tests for details.

Function/Class Reference

See Function and Class Reference.

Tips

See Tips.

Stand-alone Packages

Some features of ci-phpunit-test are available in the following standalone packages.

Related Projects for CodeIgniter 3.x

More Repositories

1

php-framework-benchmark

PHP Framework Benchmark
PHP
1,038
star
2

codeigniter-composer-installer

Installs the offical CodeIgniter 3 with secure folder structure via Composer
PHP
378
star
3

codeigniter-ss-twig

A Simple and Secure Twig integration for CodeIgniter 3.x and 4.x
PHP
161
star
4

php-orm-benchmark

PHP ORM Benchmark
PHP
144
star
5

codeigniter-cli

Cli for CodeIgniter 3.0
PHP
93
star
6

ci3-to-4-upgrade-helper

CodeIgniter 3 to 4 Upgrade Helper
PHP
64
star
7

codeigniter-deployer

A Deployment Tool for CodeIgniter 3.0
PHP
62
star
8

fuelphp1st

電子書籍『はじめてのフレームワークとしての FuelPHP』(初版) サポートサイト
PHP
36
star
9

ci-app-for-ci-phpunit-test

CodeIgniter Test Application for ci-phpunit-test
PHP
30
star
10

user-agent-parser-benchmarks

PHP User Agent Parser Benchmarks
PHP
29
star
11

codeigniter-tettei-apps

『CodeIgniter徹底入門』のサンプルアプリケーション(CodeIgniter v3.1版)
PHP
28
star
12

codeigniter-doctrine

A simple Doctrine integration for CodeIgniter 3.x
PHP
23
star
13

ci4-qrcode

Sample of CodeIgniter 3 QR Code upgraded to CodeIgniter4 using ci3-to-4-upgrade-helper
PHP
22
star
14

codeigniter3-filename-checker

CodeIgniter3 Filename Checker
PHP
22
star
15

ci4-attribute-routes

CodeIgniter4 Attribute Routes. You can set Routes in Controllers as PHP8 Attributes.
PHP
21
star
16

vagrant-fuelphp-centos6

FuelPHP Vagrant Development setup (CentOS6)
Ruby
21
star
17

vagrant-centos6-php

Vagrant CentOS 6.5 PHP 5.5 Development Environment
HTML
20
star
18

ci4-app-template

CodeIgniter4 Application Template - This template changes the CI4 default config more secure.
PHP
19
star
19

codeigniter3-namespaced-controller

CodeIgniter3 Namespaced Controller
HTML
17
star
20

ci4-viewi-demo

CodeIgniter4 Viewi Demo
PHP
15
star
21

perfect-php-mini-blog

書籍『パーフェクト PHP』のミニブログアプリケーションです。
PHP
15
star
22

codeigniter4-matome

CodeIgniter 4 まとめ
Shell
14
star
23

codeigniter-widgets

CodeIgniter Widget (View Partial) Sample
PHP
14
star
24

fuelphp-tools

Tools for FuelPHP 1.x
PHP
13
star
25

ci4-composer-installer

You can install previous versions of CodeIgniter 4 app starter.
PHP
13
star
26

sample-contact-form

FuelPHP で作成した「コンタクトフォーム」のサンプルです。
PHP
13
star
27

codeigniter4-multiple-apps-sample

Running Multiple Applications with one CodeIgniter Installation
PHP
11
star
28

codeigniter-simple-module

CodeIgniter Simple Module
PHP
11
star
29

ci-hmvc-ci-phpunit-test

CodeIgniter HMVC and ci-phpunit-test
PHP
7
star
30

vagrant-centos7-php

Vagrant CentOS 7.1 PHP Development Environment
Shell
7
star
31

php-csp-nonce-source

CSP (Content Security Policy) nonce-source for PHP
PHP
6
star
32

fuelphp-advent-calendar-2012

FuelPHP Advent Calendar 2012 電子書籍作成用
PHP
6
star
33

ci4-shield-test

CodeIgniter4 Shield Test
PHP
6
star
34

ci4-file-upload

CodeIgniter 4 File Upload
PHP
5
star
35

fibonacci-benchmark

Fibonacci Benchmark
Shell
5
star
36

phpunit-helper

Helpers for PHPUnit. Easy mock creation and easy private property/method testing.
PHP
5
star
37

epub-converter

EPUB to KOBO EPUB converter
PHP
5
star
38

monkey-patch

Standalone package of ci-phpunit-test's Monkey Patching.
PHP
5
star
39

ci4-validation-tutorial

CodeIgniter 4 Validation Tutorial
PHP
5
star
40

codeigniter-twig-samples

CodeIgniter Twig Samples
PHP
5
star
41

fuelphp1st-2nd

『はじめてのフレームワークとしてのFuelPHP 第2版(改訂版)』サポートサイト
PHP
5
star
42

ci4-news

CodeIgniter 4 News Tutorial
PHP
5
star
43

ci4-tettei-apps

『CodeIgniter徹底入門』のサンプルアプリケーション (CodeIgniter v4.x版)
PHP
4
star
44

ci3-like-captcha

CodeIgniter3-like Captcha
PHP
4
star
45

AutoMySQLBackup

Fork of https://sourceforge.net/projects/automysqlbackup/
Shell
4
star
46

fuel-simplerauth

FuelPHP SimplerAuth Package
PHP
4
star
47

ci4-for-000webhost

CodeIgniter 4 for 000webhost.com
PHP
4
star
48

ci3-to-4-news

Sample of CodeIgniter3 News Tutorial upgraded to CodeIgniter4 using ci3-to-4-upgrade-helper
PHP
3
star
49

ci4-online-games-store

Sample of CodeIgniter3 Online Game Store upgraded to CodeIgniter4 using ci3-to-4-upgrade-helper
PHP
3
star
50

ci3-news

CodeIgniter 3 News Tutorial
PHP
3
star
51

imadoki-codeigniter-phpunit

イマドキのCodeIgniterでPHPUnit入門
PHP
3
star
52

OreOrePHP

A simple and fast PHP 5.4+ "glue" framework with minimal learning cost.
PHP
3
star
53

ci4-modules-test

CodeIgniter4 Code Modules Test
PHP
3
star
54

fuelphp1st-2nd-contact-form

『はじめてのフレームワークとしてのFuelPHP 第2版(改訂版)』のコンタクトフォーム
PHP
3
star
55

crowdin-file-manager

PHP
2
star
56

NagoyaPHP.Vol17

「非三連数 2017.8.5」の解答例
PHP
2
star
57

my-ciunit

CIUnit for CodeIgniter 2.0. This is a migration of the unmaintained my-ciunit bitbucket repo.
PHP
2
star
58

ci4-twig-sample

CodeIgniter 4 Twig Sample
PHP
2
star
59

Kenjis.Contact

BEAR.Sunday Sample Contact Form
PHP
2
star
60

FuelIgniter

PHP
2
star
61

Nagoya.php.vol.9-DouKaku

PHP
2
star
62

ci4-viewi-tour-of-heroes

CodeIgniter4 Viewi Tour of Heroes app
PHP
2
star
63

secure-validator

Secure Validator is a library for input validation.
PHP
1
star
64

codeigniter-restserver-with-ci-phpunit-test

PHP
1
star
65

toggl-time-entry-pusher

PHP
1
star
66

php-code-benchmarks

PHP Code Benchmarks
PHP
1
star
67

fuelphp-1.x-composer

FuelPHP 1.x Composer Setup
PHP
1
star
68

csv-maker-for-bookcyber

売りたい書籍のCSVファイル作成 for 電脳書房
PHP
1
star
69

Nagoya.php.vol.7-DouKaku

第27回オフラインリアルタイムどう書く「分岐と行き止まり」のPHPによる解答例
PHP
1
star
70

Nagoya.php.vol6-DouKaku

第21回オフラインリアルタイムどう書く「レッスンは何曜日?」のPHPによる解答例
PHP
1
star
71

CodeIgniter_2.1.0_Tutorial

CodeIgniter 2.1.0 and its Official Tutorial Source Code
PHP
1
star
72

ci4-model-update-danger

PHP
1
star
73

NagoyaPHP.Vol12

「バス代 〜 横へな 2013.4.6」の解答例
PHP
1
star
74

ci4-qb-batch-sample

CodeIgniter 4 QueryBuilder Batch Sample
PHP
1
star
75

ci4-session-in-plain-php

Using CodeIgniter4 Session in Plain PHP
PHP
1
star
76

fuelphp-advent-calendar-2013

FuelPHP Advent Calendar 2013 電子書籍作成用
PHP
1
star