在本系列的最后一部分,是時候設定端到端 / 集成測驗環境,并確保我們已經準備好檢查我們作業的質量,
在本系列的前幾部分中,我們建立了一個構建工具,一些靜態代碼分析器,并開始撰寫單元測驗,
為了使我們的測驗堆疊更完整,有一些測驗可以檢查你的代碼是否在真實環境中運行,以及它是否能在更復雜的業務場景中運行良好,
在這里,我們可以使用為行為驅動開發構建的工具——官方 PHP 的 Cucumber 實作——Behat,我們可以通過運行以下代碼來安裝它:
$ php composer.phar require --dev behat/behat
增加一個目標到 build.xml(在本文的第一部分中描述了 Phing 設定)
<target name="behat"> <exec executable="bin/behat" passthru="true" checkreturn="true"/></target>…<target name="run" depends="phpcs,phpcpd,phan,phpspec,behat"/>
然后,你應該為檔案 features/price.feature 的測驗創建一個規范,
Feature: Price Comparison In order to compare prices As a customer I need to break the currency barrier Scenario: Compare EUR and PLN Given I use nbp.pl comparator When I compare “100EUR” and “100PLN” Then It should r
eturn some result
這個測驗場景非常容易閱讀,并且應該給你一個關于該特性應該如何作業的良好印象,不幸的是,計算機通常并不真正理解人類語言,所以現在是為每一步撰寫代碼的時候了,
你可以通過運行 ./bin/behat-init 來生成它的代碼模板,它應該會創建一個這樣的類:
1 //features/bootstrap/FeatureContext.php use Behat\Behat\Context\SnippetAcceptingContext;use Behat\Gherkin\Node\PyStringNode;use Behat\Gherkin\Node\TableNode;class FeatureContext implements SnippetAcceptingContext{ /** * Initializes context. */ public function __construct() { }}
然后你可以執行:
1 $ bin/behat --dry-run --append-snippets
Behat 將自動為場景中定義的每個步驟創建函式,
現在你可以通過填充函式的主體來開始實作真正的檢查:
// features/bootstrap/FeatureContext.php <?phpuse Behat\Behat\Context\Context;use Domain\Price;use Domain\PriceComparator;use Infrastructure\NBPPriceConverter; /*** Defines application features from the specific context.*/class FeatureContext implements Context{ /** @var PriceComparator */ private $priceComparator; /** @var int */ private $result; /** * Initializes context. * * Every scenario gets its own context instance. * You can also pass arbitrary arguments to the * context constructor through behat.yml. */ public function __construct() { } /** * @Given I use nbp.pl comparator */ public function iUseNbpPlComparator() { $this->priceComparator = new PriceComparator(new NBPPriceConverter()); } /** * @When I compare :price1 and :price2 */ public function iCompareAnd($price1, $price2) { preg_match('/(\d+)([A-Z]+)/', $price1, $match1); preg_match('/(\d+)([A-Z]+)/', $price2, $match2); $price1 = new Price($match1[1], $match1[2]); $price2 = new Price($match2[1], $match2[2]); $this->result = $this->priceComparator->compare($price1, $price2); } /** * @Then It should return some result */ public function itShouldReturnSomeResult() { if (!is_int($this->result)) { throw new \DomainException('Returned value is not integer'); } }}
最后,使用 ./bin/phing 運行所有的測驗,你應該得到以下結果:
Buildfile: /home/maciej/workspace/php-testing/build.xmlMyProject > phpcs: MyProject > phpcpd: phpcpd 4.0.0 by Sebastian Bergmann.0.00% duplicated lines out of 103 total lines of code. Time: 17 ms, Memory: 4.00MB MyProject > phan: MyProject > phpspec: / skipped: 0% / pending: 0% / passed: 100% / failed: 0% / broken: 0% / 3 examples2 specs3 examples (3 passed)15ms MyProject > behat: Feature: Price Comparison In order to compare prices As a customer I need to break the currency barrier Scenario: Compare EUR and PLN # features/price.feature:6 Given I use nbp.pl comparator # FeatureContext::iUseNbpPlComparator() When I compare "100EUR" and "100PLN" # FeatureContext::iCompareAnd() Then It should return some result # FeatureContext::itShouldReturnSomeResult()1 scenario (1 passed)3 steps (3 passed)0m0.01s (9.13Mb) MyProject > run: BUILD FINISHED Total time: 1.1000 second
正如你所看到的,Behat 準備了一份很好的報告,說明我們的應用程式做了什么,結果是什么,下一次,當專案經理詢問你在測驗中涉及到哪些場景時,你可以給他一個 Behat 輸出!
1 測驗的結構
每個測驗都包括:
- 對該場景的一些準備,用“Given”部分表示
- “When”部分所涵蓋的一些動作
- 一些檢查被標記為“Then”部分
每個部分都可以包含多個與“And”關鍵字連接的步驟:
Scenario: Compare EUR and PLN Given nbp.pl comparator is available And I use nbp.pl comparator When I compare "100EUR" and "100PLN" And I save the result Then It should return some result And the first amount should be greater
2 背景關系
Behat 允許你為你的測驗定義多個背景關系,這意味著你可以將步驟代碼分割成多個類,并從不同的角度去測驗你的場景,
你可以例如:為 web 背景關系撰寫代碼,它將使用你的應用程式 HTTP 控制器運行你的測驗步驟,你還可以創建“domain”背景關系,它將只使用 PHP API 呼叫來運行你的業務邏輯,通過這種方式,你可以單獨地測驗業務邏輯集成,從端到端應用程式測驗,
關于如何在 Behat 建立許多背景關系的更多資訊,請參考 http://behat.org/en/latest/userguide/context.html 的檔案,
3 我是如何使用 Behat 的?
正如一開始所提到的,你可以使用 Behat 進行集成測驗,通常情況下,你的代碼依賴于一些外部的第三方系統,當我們在第 2 部分中撰寫單元測驗時,我們總是假設外部依賴關系像預期的那樣作業,使用 Behat,你可以撰寫測驗場景,它將自動運行你的代碼,并檢查它是否正確地使用真實場景的服務,
最重要的是,Behat 對于測驗系統使用的復雜的端到端場景非常有用,它允許你隱藏在一個可讀性的名字后面運行測驗步驟所需的復雜代碼,并撰寫一個人人都能理解的場景,
總結
從以上的文章中,你已經學習了如何在你的專案中設定六個有用的工具:
- PHing 用于運行你的構建
- PHPCS 用于自動檢查代碼格式
- PHPCPD 用于檢測重復代碼的
- Phan 用于高級靜態代碼分析
- PHPSpec 用于單元測驗
- Behat 用于端到端和集成測驗
現在,你可以向 git 提交鉤子添加 ./bin/phing,并設定持續集成來運行每個提交的測驗,
是不是突然之間,沒有什么能阻止你寫出高質量的 PHP 代碼!
Well done!
很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要的加群(點擊→)677079770
推薦閱讀:
教你如何提高 PHP 代碼的質量
如何提高 PHP 代碼的質量?第二部分 單元測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100301.html
標籤:PHP
