All Projects → kenjis → Ci Phpunit Test

kenjis / Ci Phpunit Test

Licence: mit
An easier way to use PHPUnit with CodeIgniter 3.x.

Projects that are alternatives of or similar to Ci Phpunit Test

codeigniter-tettei-apps
『CodeIgniter徹底入門』のサンプルアプリケーション(CodeIgniter v3.1版)
Stars: ✭ 26 (-95.14%)
Mutual labels:  codeigniter, phpunit
Paraunit
Run PHPUnit tests in parallel
Stars: ✭ 104 (-80.56%)
Mutual labels:  testing-tools, phpunit
php-test-generator
Generate test cases for existing PHP files
Stars: ✭ 47 (-91.21%)
Mutual labels:  phpunit, testing-tools
php.autotest
autotest for php written in php
Stars: ✭ 19 (-96.45%)
Mutual labels:  phpunit, testing-tools
Phpunit
The PHP Unit Testing framework.
Stars: ✭ 18,103 (+3283.74%)
Mutual labels:  testing-tools, phpunit
Phpunit Snapshot Assertions
A way to test without writing actual test cases
Stars: ✭ 443 (-17.2%)
Mutual labels:  phpunit
Goss
Quick and Easy server testing/validation
Stars: ✭ 4,550 (+750.47%)
Mutual labels:  testing-tools
Pixelmatch
The smallest, simplest and fastest JavaScript pixel-level image comparison library
Stars: ✭ 4,447 (+731.21%)
Mutual labels:  testing-tools
Tcpcopy
An online request replication tool, also a tcp stream replay tool, fit for real testing, performance testing, stability testing, stress testing, load testing, smoke testing, etc
Stars: ✭ 4,028 (+652.9%)
Mutual labels:  testing-tools
Vuetron
A tool for testing and debugging your Vue + Vuex applications. 是一個可以幫助您 Vue.js 的項目測試及偵錯的工具, 也同時支持 Vuex及 Vue-Router.
Stars: ✭ 531 (-0.75%)
Mutual labels:  testing-tools
Karate
Test Automation Made Simple
Stars: ✭ 5,497 (+927.48%)
Mutual labels:  testing-tools
Codeception
Full-stack testing PHP framework
Stars: ✭ 4,401 (+722.62%)
Mutual labels:  phpunit
Tparse
CLI tool for analyzing and summarizing go test output. Pipe friendly. CI/CD friendly.
Stars: ✭ 445 (-16.82%)
Mutual labels:  testing-tools
Dbdeployer
DBdeployer is a tool that deploys MySQL database servers easily.
Stars: ✭ 467 (-12.71%)
Mutual labels:  testing-tools
Knapsack
Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.
Stars: ✭ 430 (-19.63%)
Mutual labels:  testing-tools
Pandera
A light-weight, flexible, and expressive pandas data validation library
Stars: ✭ 506 (-5.42%)
Mutual labels:  testing-tools
Mapos
Sistema de Controle de Ordens de Serviço
Stars: ✭ 421 (-21.31%)
Mutual labels:  codeigniter
Hermione
Browser test runner based on mocha and wdio
Stars: ✭ 454 (-15.14%)
Mutual labels:  testing-tools
Namazu
🐟 鯰: Programmable fuzzy scheduler for testing distributed systems
Stars: ✭ 475 (-11.21%)
Mutual labels:  testing-tools
Mocha
☕️ simple, flexible, fun javascript test framework for node.js & the browser
Stars: ✭ 20,986 (+3822.62%)
Mutual labels:  testing-tools

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

  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.

Installation via Composer

If you like Composer:

$ 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

  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.

Upgrading via Composer

If you like Composer:

$ cd /path/to/codeigniter/
$ composer update kenjis/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'.)

Read Change Log.

How to Run Tests

You have to install PHPUnit before running tests.

Note: You must run phpunit command in application/tests folder.

$ cd /path/to/codeigniter/
$ cd application/tests/
$ phpunit
PHPUnit 4.8.31 by Sebastian Bergmann and contributors.

...

Time: 341 ms, Memory: 5.50Mb

OK (3 tests, 3 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

To generate coverage report, Xdebug is needed.

If you want to run a single test case file:

$ phpunit 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].