<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [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文檔。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看