# 單元測試(Unit testing)
適當的測試有幫于更好的編寫軟件。如果你設置了適當的測試用例,可以消除大多數功能性的錯誤,并且更好地維護你的軟件。
## 整合 PHPunit 到 phalcon(Integrating PHPunit with phalcon)
如果你還沒有安裝好 phpunit,可以使用以下 composer 命令:
~~~
composer require phpunit/phpunit:^5.0
~~~
或者手動添加 composer.json:
~~~
{
"require-dev": {
"phpunit/phpunit": "^5.0"
}
}
~~~
phpunit 安裝后將會在你的根目錄創建一個名為 ‘tests’ 的目錄:
~~~
app/
public/
tests/
~~~
接下來,我們需要用一個 ‘helper’ 文件來引導單元測試程序。
## PHPunit 輔助文件(The PHPunit helper file)
需要使用 helper 文件來引導運行測試程序。我們預先準備好一個示例文件,將該文件放到 tests/ 目錄下并命名為 TestHelper.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);
~~~
你需要從自己的 library 類庫中測試組件,將它們添加到 autoloader 加載器或在主程序中使用 autoloader 加載器。
為了更好地幫助你構建單元測試,我們寫了一些抽象的類庫,你可以使用這些抽象類來引導單元測試。 這些文件在 @[https://github.com/phalcon/incubator](https://github.com/phalcon/incubator).
你可以添加 incubator 依賴庫:
~~~
composer require phalcon/incubator
~~~
或手動添加到 composer.json:
~~~
{
"require": {
"phalcon/incubator": "^3.0"
}
}
~~~
你也可以使用鏈接克隆倉庫。
## PHPunit.xml 文件(PHPunit.xml file)
現在,創建一個 phpunit 文件:
~~~
<?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/ 目錄運行所有測試。
## 簡單的單元測試(Sample unit test)
要運行任何單元測試,你要事先定義好。autoloader 加載器將確保正確的文件被加載進來,所以你需要做的是創建文件然后 phpunit 運行測試。
該示例不包含配置文件,但大多數測試用例都需要配置文件,你可以將它添加到 DI 得到 UnitTestCase 文件。
首先在 /tests 目錄創建一個 UnitTestCase.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’,即創建一個文件名為 testsTestUnitTest.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’ 得到以下輸出:
~~~
$ phpunit
PHPUnit 3.7.23 by Sebastian Bergmann.
Configuration read from /private/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'
/private/var/www/tests/Test/UnitTest.php:25
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
~~~
現在,你可以開始構建單元測試了。你可以在這里查看一份很好的指南(如果你不熟悉PHPUnit,我們也推薦閱讀PHPUnit文檔)
[http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/](http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/)
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl