Coder Social home page Coder Social logo

a382695908 / ci-phpunit-test Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kenjis/ci-phpunit-test

0.0 1.0 0.0 1.49 MB

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

Home Page: http://kenjis.github.io/ci-phpunit-test/

License: MIT License

PHP 99.77% Shell 0.23%

ci-phpunit-test's Introduction

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 5.4.0 or later (5.6 or later is recommended)
  • CodeIgniter 3.x
  • PHPUnit 4.3 or later (4.8 or later is recommended)
    • If you use PHPUnit 6.0, please use ci-phpunit-test v0.14.0 or later.
    • You can download old version of phpunit.phar from https://phar.phpunit.de/.

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 --dev

And run install.php:

$ php vendor/kenjis/ci-phpunit-test/install.php
  • The above command always overwrites exisiting files.
  • You must run it at CodeIgniter project root folder.

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
$ php vendor/kenjis/ci-phpunit-test/update.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()
    {
        $this->obj = $this->newModel('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->assertContains(
            '<title>Welcome to CodeIgniter</title>', $output
        );
    }
}

See How to Write Tests for details.

Function/Class Reference

See Function and Class Reference.

Tips

See Tips.

ci-phpunit-test's People

Contributors

bellflower2015 avatar hoersten avatar kenjis avatar luckydonald avatar rochefort avatar rodolfosilva avatar souki-tn avatar trungdq88 avatar ytetsuro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.