kenjis/ci-phpunit-test
Composer 安装命令:
composer require kenjis/ci-phpunit-test
包简介
An easier way to use PHPUnit with CodeIgniter 3.x
README 文档
README
Warning
Upgrading to CodeIgniter4 is strongly recommended.
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.
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.
- Go to Project Properties > Testing > PHPUnit, check Use Custom Test Suite checkbox, and select
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
- Download latest
ci-phpunit-testfrom https://github.com/kenjis/ci-phpunit-test/releases. - Unzip and copy
application/testsfolder into yourapplicationfolder 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
applicationandpublicfolder 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
- But some paths may be not correct, in that case, please fix them in tests/Bootstrap.php.
Upgrading
Manual Upgrading
- Download latest
ci-phpunit-testfrom https://github.com/kenjis/ci-phpunit-test/releases. - Unzip and replace
application/tests/_ci_phpunit_testfolder. - 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:
- Delete the old test library directory:
$ rm -rf /path/to/codeigniter/application/tests/_ci_phpunit_test - Edit the
application/tests/Bootstrap.phpfile. 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/orTESTPATH . '_ci_phpunit_testwithCI_PHPUNIT_TESTPATH . '. (So, for example,__DIR__ . '/_ci_phpunit_test/CIPHPUnitTest.php'would becomeCI_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
kenjis/ci-phpunit-test 适用场景与选型建议
kenjis/ci-phpunit-test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.23M 次下载、GitHub Stars 达 585, 最近一次更新时间为 2015 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「test」 「unit testing」 「codeigniter」 「monkey patch」 「codeigniter3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kenjis/ci-phpunit-test 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kenjis/ci-phpunit-test 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kenjis/ci-phpunit-test 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
Symfony bundle for unigen test generator
A cli tool which generates unit tests.
PHPUnit Testing extensions for HMTL and CSS insures valid HTML and CSS via v.Nu validator
统计信息
- 总下载量: 1.23M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 596
- 点击次数: 37
- 依赖项目数: 3
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-10
