`

How to use PHPUnit (CIUnit) with CodeIgniter 2.1.0

    博客分类:
  • php
 
阅读更多

转载自: http://d.hatena.ne.jp/Kenji_s/20120117/1326763908

 

How to use PHPUnit (CIUnit) with CodeIgniter 2.1.0Add Star

STOP SOPA

This article explains how to install and use PHPUnit/CIUnit with CodeIgniter 2.1.0.

What is CIUnit?

CIUnit is a bridge between your CodeIgniter application and PHPUnit.

But the official release supports CodeIgniter 1.7.2. So I use the fork of CIUnit (my-ciunit).

Install PHPUnit

$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover components.ez.no
$ sudo pear channel-discover pear.symfony-project.com
$ sudo pear install phpunit/PHPUnit

Install CodeIgniter

$ wget http://downloads.codeigniter.com/reactor/CodeIgniter_2.1.0.zip
$ unzip CodeIgniter_2.1.0.zip

Install CIUnit

Download my-ciunit

The default branch of my-ciunit is now for CodeIgniter 2.1.0.

$ wget https://bitbucket.org/kenjis/my-ciunit/get/default.zip
$ unzip default.zip

If you use CodeIgniter 2.0.3, get "CI 2.0.3" branch.

$ wget https://bitbucket.org/kenjis/my-ciunit/get/CI%202.0.3.zip
$ unzip "CI 2.0.3.zip"
Install my-ciunit by shell script

my-ciunit has a installer shell script.

How to use:

$ tools/install.sh /path/to/CodeIgniter/ [database_name [database_user [database_password [database_host]]]]

Note: The database name for testing must end with "_test".

For example:

$ cd kenjis-my-ciunit-*
$ tools/install.sh ../CodeIgniter_2.1.0/ ciunit_test root password

This script creates database config file for testing, "application/config/testing/database.php".

Install my-ciunit manually

Copy application folder and tests folder in my-ciunit to CodeIgniter top diretory.

$ cd kenjis-my-ciunit-*
$ cp -R application /path/to/CodeIgniter_2.1.0/
$ cp -R tests /path/to/CodeIgniter_2.1.0/

And create database config file for testing, "application/config/testing/database.php".

Note: The database name for testing must end with "_test".

If you use MY_Loader, MY_Output, MY_Session

Change the parent classes to MY_* in:

  • application/third_party/CIUnit/core/CIU_*.php
  • application/third_party/CIUnit/libraries/CIU_*.php

Directory structure of CIUnit

CodeIgniter/
  application/
    third_party/
      CIUnit/ ... CIUnit itself
  tests/
    controllers/ ... tests of controller
    fixtures/    ... fixtures
    helpers/     ... tests of helper
    libs/        ... tests of library
    models/      ... tests of model
    system/      ... tests of system (PHP)

Prepare Database for Testing

The database name for testing must end with "_test". This is the specification of CIUnit.

Create "ciunit_test" database, and create "phone_carrier" table for a sample model test code in CIUnit.

CREATE TABLE IF NOT EXISTS `phone_carrier` (
  `name` varchar(255) NOT NULL,
  `txt_address` varchar(255) NOT NULL,
  `txt_message_length` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Prepare Model for sample test code

CIUnit has a sample test code for "Phone_carrier_model" model, but has no "Phone_carrier_model" model code.

Create the model.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Phone_carrier_model extends CI_Model {
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function getCarriers(array $attributes)
	{
		foreach ($attributes as $field)
		{
			$this->db->select($field)->from('phone_carrier');
			$query = $this->db->get();
			foreach ($query->result_array() as $row)
			{
				$data[] = array($field, $row[$field]);
			}
		}
		
		return $data;
	}
	
}

/* End of file phone_carrier_model.php */
/* Location: ./application/models/phone_carrier_model.php */

Run tests

Before run phpunit, move to tests folder.

$ cd tests/

To run all tests,

$ phpunit

To run tests of model, specify a folder,

$ phpunit models

To run a specific test file,

$ phpunit models/PhoneCarrierModelTest.php

If all tests have passed, you'll see green OK below:

f:id:Kenji_s:20120117095833p:image


Next: Database Testing of CodeIgniter Application with PHPUnit (CIUnit) - A Day in Serenity @ Kenji

分享到:
评论

相关推荐

    PHPUnit Essentials

    Get started with PHPUnit and learn how to write and test code using advanced technologies Overview Learn how to install PHPUnit as well as how to write and execute tests Understand the advanced ...

    ci-phpunit-test, 一种简单的使用 PHPUnit 3.x 语言的方法.zip

    ci-phpunit-test, 一种简单的使用 PHPUnit 3.x 语言的方法 ci-phpunit-test用于 CodeIgniter 3 。x 在 CodeIgniter 3. x. 中使用PHPUnit的简便方法你根本不需要修改CodeIgniter核心文件。你可以轻松编

    imadoki-codeigniter-phpunit:Imadoki的CodeIgniter入门PHPUnit

    Imadoki的CodeIgniter入门PHPUnit 的源代码。

    ci-phpunit-test:将PHPUnit与CodeIgniter 3.x结合使用的更简单方法

    适用于CodeIgniter 3.x的ci-phpunit-test 将PHPUnit与 3.x结合使用的更简单方法。 您根本不需要修改CodeIgniter核心文件。 您可以轻松编写控制器测试。 也许没有什么是不可测试的。 有据可查。要求PHP 7.3或更高版本...

    phpunit附属lib

    如果你在用PHPUNIT会报 include(PHPUnit_Extensions_Story_TestCase.php): failed to open stream: No such file or directory include(PHPUnit_Extensions_Database_TestCase.php): failed to open stream: No such...

    Etsy的PHPUnit扩展phpunit-extensions.zip

    phpunit-extensions 是 Etsy 的 PHPUnit 扩展。 标签:phpunit

    phpunit 4.2 最新版

    phpunit 3 67 最新版 PHPUnit是一个轻量级的PHP测试框架 它是在PHP5下面对JUnit3系列版本的完整移植 是xUnit测试框架家族的一员 它们都基于模式先锋Kent Beck的设计 单元测试是几个现代敏捷开发方法的基础 使得...

    Easy Laravel5 A Hands On Introduction Usinga Real-World Project

    We’ll conclude the chapter with an introduction to PHPUnit, showing you how to create and execute your first automated Laravel test! Chapter2.ManagingYourProjectControllers,Layout,Views,and ...

    PHPunit手册中文版

    本版次适用于3.2版的PHPUnit。当然,大多数示例应该也可用于2.0-3.1版的PHPUnit。本书后面的“用于PHP 4的PHPUnit”(附录B - 译注)部分涉及了适用于PHP 4的旧版PHPUnit,它们已不再积极开发。 读者需要很好的理解...

    ci-app-for-ci-phpunit-test:ci-phpunit-test的CodeIgniter测试应用程序

    ci-phpunit-test的CodeIgniter测试应用程序此CodeIgniter应用程序用于。要求PHP 7.3或更高版本如何进行测试 $ git clone -b 3.x https://github.com/kenjis/ci-app-for-ci-phpunit-test.git$ cd ci-app-for-ci-...

    phpunit配置及使用

    phpunit配置及使用

    phpunit_中文手册说明

    phpunit_中文手册说明 根据目录进行的编排!

    PHPUnit单元测试的配置及使用

    PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用

    phpunit5.7

    PHPUnit 5.7 是之前旧的 稳定 版本。 它 稳定 于 2016年12月02日。 PHPUnit 5.7 支持于 PHP 5.6, PHP 7.0 和 PHP 7.1, PHPUnit 5 将于2018年02月02日结束维护支持。

    PHPUnit

    NULL 博文链接:https://tangzhifei.iteye.com/blog/437196

    phpunit4.8.35

    phpunit.phar phpunit.cmd的打包文件

    phpunit-asynchronicity:用于断言与PHPUnit异步发生的事情的库

    用法使用PHPUnit use Asynchronicity \ PHPUnit \ Asynchronicity ;use PHPUnit \ Framework \ Assert ;use PHPUnit \ Framework \ TestCase ;final class ProcessTest extends TestCase{ use Asynchronicity ; /** ...

    PHPUnit袖珍指南.doc

    PHPUnit袖珍指南.doc PHPUnit 单元测试 从环境配置到 段元详细介绍

    phpunit5.7官方文档PDF中文

    PHPUnit手册Sebastian Bergmann 此版本对应于PHPUnit 5.7。最后更新于2017-05026。 版权 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015 Sebastian Bergmann

    phpunit-book.pdf

    该文档为 phpunit自动换测试文档。如果大家有什么问题希望大家回复。

Global site tag (gtag.js) - Google Analytics