nueip/codeigniter-phpunit

CodeIgniter 3 PHPUnit 测试扩展库

1.3.0 2022-03-10 08:13 UTC

This package is auto-updated.

Last update: 2024-09-10 13:56:19 UTC


README

CodeIgniter PHPUnit 测试


CodeIgniter 3 PHPUnit 测试扩展库

Latest Stable Version License

这个 RESTful API 扩展被收集到 nueip/codeigniter-pack,这是一个完整的 Codeigniter 框架解决方案。

特性

  • Codeigniter 3 框架中的 PHPUnit 测试

  • 通过 Composer 轻松安装到您的 Codeigniter 项目中

概要

需求

此库需要以下内容

  • PHP 5.3.0+
  • CodeIgniter 3.0.0+

安装

在您的 Codeigniter 项目中的 \application 文件夹下运行 Composer

composer require nueip/codeigniter-phpunit

目录结构

codeigniter/
└── application/
    ├── tests/          Test cases
    ├── vendor/         Vendor included nueip/codeigniter-phpunit
    └── phpunit.xml     PHPUnit XML

配置

根据 目录结构,在 application 目录下创建和配置 phpunit.xml

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="vendor/nueip/codeigniter-phpunit/bootstrap.php">
  <testsuites>
    <testsuite name="TestSuite">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

对于此 phpunit.xml 模板,测试用例目录为 application/test,确保您会创建每个测试用例。

用法

在此库的 application 目录下运行来自 vendor 的 phpunit

$ ./vendor/bin/phpunit

或者使用绝对路径命令

$ /var/www/html/codeigniter3/application/vendor/bin/phpunit -c /var/www/html/codeigniter3/application/phpunit.xml
$ phpunit -c /var/www/html/codeigniter3/application/phpunit.xml

然后结果将如下

PHPUnit 5.7.27 by Sebastian Bergmann and contributors.



Time: 40 ms, Memory: 2.75MB

No tests executed!

测试用例

使用此扩展库,您可以编写加载 Codeigniter 框架的测试用例。

例如,编写一个测试用例 application/tests/CodeigniterTest.php 以测试 Codeigniter 配置组件

<?php

use PHPUnit\Framework\TestCase;

final class CodeigniterTest extends TestCase
{
    function __construct() 
    {
        parent::__construct();
        // Load CI application
        $this->CI = & get_instance();
    }
    
    public function testConfigItem()
    {
        $indexPage = $this->CI->config->item('index_page');
        
        $this->assertSame('index.php', $indexPage);
    }
}

然后通过运行 PHPUnit 测试,您将得到结果 OK (1 test, 1 assertion)