[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文檔。
- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持