淺談如何提高PHP代碼質(zhì)量之端到端集成測試
概述
在這里,我們可以使用為行為驅(qū)動開發(fā)構(gòu)建的工具——官方 PHP 的 Cucumber 實現(xiàn)——Behat。我們可以通過運行以下代碼來安裝它:
$ php composer.phar require --dev behat/behat
增加一個目標到 build.xml(在本文的第一部分中描述了 Phing 設(shè)置)
<target name="behat"> <exec executable="bin/behat" passthru="true" checkreturn="true" /> </target>… <target name="run" depends="phpcs,phpcpd,phan,phpspec,behat" />
然后,你應該為文件 features/price.feature 的測試創(chuàng)建一個規(guī)范。
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 return some result
這個測試場景非常容易閱讀,并且應該給你一個關(guān)于該特性應該如何工作的良好印象。不幸的是,計算機通常并不真正理解人類語言,所以現(xiàn)在是為每一步編寫代碼的時候了。
你可以通過運行 ./bin/behat-init 來生成它的代碼模板。它應該會創(chuàng)建一個這樣的類:
//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() { } }
然后你可以執(zhí)行:
$ bin/behat --dry-run --append-snippets
Behat 將自動為場景中定義的每個步驟創(chuàng)建函數(shù)。
現(xiàn)在你可以通過填充函數(shù)的主體來開始實現(xiàn)真正的檢查:
// features/bootstrap/FeatureContext.php <?php use 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 運行所有的測試。你應該得到以下結(jié)果:
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 準備了一份很好的報告,說明我們的應用程序做了什么,結(jié)果是什么。下一次,當項目經(jīng)理詢問你在測試中涉及到哪些場景時,你可以給他一個 Behat 輸出!
1、測試的結(jié)構(gòu)
每個測試都包括:
- 對該場景的一些準備,用“Given”部分表示
- “When”部分所涵蓋的一些動作
- 一些檢查被標記為“Then”部分
每個部分都可以包含多個與“And”關(guān)鍵字連接的步驟:
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 控制器運行你的測試步驟。你還可以創(chuàng)建“domain”上下文,它將只使用 PHP API 調(diào)用來運行你的業(yè)務邏輯。通過這種方式,你可以單獨地測試業(yè)務邏輯集成,從端到端應用程序測試。
關(guān)于如何在 Behat 建立許多上下文的更多信息,請參考http://behat.org/en/latest/userguide/context.html的文檔。
3、如何使用Behat
正如一開始所提到的,你可以使用 Behat 進行集成測試。通常情況下,你的代碼依賴于一些外部的第三方系統(tǒng)。當我們在第 2 部分中編寫單元測試時,我們總是假設(shè)外部依賴關(guān)系像預期的那樣工作。使用 Behat,你可以編寫測試場景,它將自動運行你的代碼,并檢查它是否正確地使用真實場景的服務。
最重要的是,Behat 對于測試系統(tǒng)使用的復雜的端到端場景非常有用。它允許你隱藏在一個可讀性的名字后面運行測試步驟所需的復雜代碼,并編寫一個人人都能理解的場景。
總結(jié)
從以上的文章中,你已經(jīng)學習了如何在你的項目中設(shè)置六個有用的工具:
- PHing 用于運行你的構(gòu)建
- PHPCS 用于自動檢查代碼格式
- PHPCPD 用于檢測重復代碼的
- Phan 用于高級靜態(tài)代碼分析
- PHPSpec 用于單元測試
- Behat 用于端到端和集成測試
現(xiàn)在,你可以向 git 提交鉤子添加 ./bin/phing,并設(shè)置持續(xù)集成來運行每個提交的測試。
是不是突然之間,沒有什么能阻止你寫出高質(zhì)量的 PHP 代碼!
以上就是淺談如何提高PHP代碼質(zhì)量之端到端集成測試的詳細內(nèi)容,更多關(guān)于如何提高PHP代碼質(zhì)量之端到端集成測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php Smarty date_format [格式化時間日期]
php Smarty date_format [格式化時間日期] ,需要的朋友可以參考下。2010-03-03PHP調(diào)用Linux的命令行執(zhí)行文件壓縮命令
一開始,我和普通青年一樣,想到用PHP內(nèi)置的 ZipArchive糾結(jié)的是環(huán)境上沒安裝zip擴展,想采用用PHP調(diào)用Linux的命令行 ,執(zhí)行壓縮命令,感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01PHP簡單實現(xiàn)定時監(jiān)控nginx日志文件功能示例
這篇文章主要介紹了PHP簡單實現(xiàn)定時監(jiān)控nginx日志文件功能,涉及php定時讀取nginx服務器日志以及基于curl的數(shù)據(jù)傳輸相關(guān)操作技巧,需要的朋友可以參考下2018-06-06PHP中利用sleep函數(shù)實現(xiàn)定時執(zhí)行功能實現(xiàn)代碼
在PHP中,有一個sleep函數(shù),大概意思是程序執(zhí)行遇到sleep函數(shù)時暫停N秒后繼續(xù)往下執(zhí)行。如sleep(10)意思就是程序自上往下執(zhí)行,遇到sleep(10)語句后暫停十秒,然后繼續(xù)往下執(zhí)行2016-08-08PHP常見字符串處理函數(shù)用法示例【轉(zhuǎn)換,轉(zhuǎn)義,截取,比較,查找,反轉(zhuǎn),切割】
這篇文章主要介紹了PHP常見字符串處理函數(shù)用法,結(jié)合實例形式分析了php針對字符串的大小寫轉(zhuǎn)換、轉(zhuǎn)義、截取、比較、查找、反轉(zhuǎn)、切割等操作,需要的朋友可以參考下2016-12-12php str_replace替換指定次數(shù)的方法詳解
本篇文章主要介紹了php str_replace替換指定次數(shù)的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05PHP實現(xiàn)支持GET,POST,Multipart/form-data的HTTP請求類
這篇文章主要介紹了PHP實現(xiàn)支持GET,POST,Multipart/form-data的HTTP請求類,包括了連接與處理方式及相關(guān)的技巧,需要的朋友可以參考下2014-09-09