yidas/codeigniter-phpunit

CodeIgniter 3 PHPUnit 测试扩展库

1.1.0 2019-02-23 06:56 UTC

This package is auto-updated.

Last update: 2024-08-23 18:35:38 UTC


README

CodeIgniter PHPUnit 测试


CodeIgniter 3 PHPUnit 测试扩展库

Latest Stable Version License

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

特性

  • Codeigniter 3 框架中的 PHPUnit 测试

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

概述

需求

此库需要以下内容

  • PHP 5.3.0+
  • CodeIgniter 3.0.0+

安装

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

composer require yidas/codeigniter-phpunit

目录结构

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

配置

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

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

对于此 phpunit.xml 模板,测试案例目录是 application/test,确保您会在其中创建每个测试案例。

用法

在此库的 application 目录中运行 phpunit 从vendor

$ ./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框架来编写测试案例。

例如,为测试Codeigniter配置组件编写测试案例 application/tests/CodeigniterTest.php

<?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)