💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 概况 编写适当的测试可以帮助编写更好的软件。如果您设置了正确的测试用例,则可以消除大多数功能错误并更好地维护您的软件。 ## 将PHPUnit与Phalcon集成 如果您还没有安装phpunit,可以使用以下composer命令来完成: ```bash composer require phpunit/phpunit:^5.0 ``` 或者通过手动将其添加到 `composer.json`: ```json <br />{ "require-dev": { "phpunit/phpunit": "^5.0" } } ``` 安装PHPUnit后,在项目根目录中创建一个名为 `tests` 的目录: app/ public/ tests/ 接下来,我们需要一个'helper'文件来引导应用程序进行单元测试。 ## PHPUnit帮助文件 需要一个帮助文件来引导应用程序以运行测试。我们准备了一个示例文件。将文件作为 `TestHelper.php` 放在 `tests/` 目录中。 ```php <?php use Phalcon\Di; use Phalcon\Di\FactoryDefault; use Phalcon\Loader; ini_set("display_errors", 1); error_reporting(E_ALL); define("ROOT_PATH", __DIR__); set_include_path( ROOT_PATH . PATH_SEPARATOR . get_include_path() ); // Required for phalcon/incubator include __DIR__ . "/../vendor/autoload.php"; // Use the application autoloader to autoload the classes // Autoload the dependencies found in composer $loader = new Loader(); $loader->registerDirs( [ ROOT_PATH, ] ); $loader->register(); $di = new FactoryDefault(); Di::reset(); // Add any needed services to the DI here Di::setDefault($di); ``` 如果您需要测试自己库中的任何组件,请将它们添加到自动加载器或使用主应用程序中的自动加载器。 为了帮助您构建单元测试,我们制作了一些抽象类,您可以使用它们来引导单元测试本身。这些文件存在于[Phalcon Incubator](https://github.com/phalcon/incubator)中。 您可以通过将Incubator库添加为依赖项来使用它: ```bash composer require phalcon/incubator ``` 或者通过手动将其添加到 `composer.json`: ```json { "require": { "phalcon/incubator": "^3.0" } } ``` 您还可以使用上面的repo链接克隆存储库。 ## `phpunit.xml` 文件 现在,创建一个 `phpunit.xml` 文件,如下所示: ```xml <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="./TestHelper.php" backupGlobals="false" backupStaticAttributes="false" verbose="true" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="true"> <testsuite name="Phalcon - Testsuite"> <directory>./</directory> </testsuite> </phpunit> ``` 修改 `phpunit.xml` 以满足您的需求并将其保存在 `tests` 中。这将在 `tests` 目录下运行任何测试。 ## 单元测试样例 要运行任何单元测试,您需要定义它们。自动加载器将确保加载正确的文件,因此您需要做的就是创建文件,phpunit将为您运行测试。 此示例不包含配置文件,但大多数测试用例确实需要一个。您可以将其添加到 `DI` 以获取 `UnitTestCase` 文件。 首先在 `tests` 目录中创建一个名为 `UnitTestCase.php` 的基本单元测试: ```php <?php use Phalcon\Di; use Phalcon\Test\UnitTestCase as PhalconTestCase; abstract class UnitTestCase extends PhalconTestCase { /** * @var bool */ private $_loaded = false; public function setUp() { parent::setUp(); // Load any additional services that might be required during testing $di = Di::getDefault(); // Get any DI components here. If you have a config, be sure to pass it to the parent $this->setDi($di); $this->_loaded = true; } /** * Check if the test case is setup properly * * @throws \PHPUnit_Framework_IncompleteTestError; */ public function __destruct() { if (!$this->_loaded) { throw new \PHPUnit_Framework_IncompleteTestError( "Please run parent::setUp()." ); } } } ``` 在命名空间中分离单元测试总是一个好主意。对于此测试,我们将创建命名空间“Test”。因此,创建一个名为 `tests\Test\UnitTest.php` 的文件: ```php <?php namespace Test; /** * Class UnitTest */ class UnitTest extends \UnitTestCase { public function testTestCase() { $this->assertEquals( "works", "works", "This is OK" ); $this->assertEquals( "works", "works1", "This will fail" ); } } ``` 现在,当您从`tests`目录中的命令行执行`phpunit`时,您将获得以下输出: ```bash $ phpunit PHPUnit 3.7.23 by Sebastian Bergmann. Configuration read from /var/www/tests/phpunit.xml Time: 3 ms, Memory: 3.25Mb There was 1 failure: 1) Test\UnitTest::testTestCase This will fail Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'works' +'works1' /var/www/tests/Test/UnitTest.php:25 FAILURES! Tests: 1, Assertions: 2, Failures: 1. ``` 现在您可以开始构建单元测试。你可以在这里查看一个[好的指南](http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/)。如果您不熟悉PHPUnit,我们还建议您阅读PHPUnit文档。