對於PHPUnit測試私有屬性和方法的功能分析
這篇文章主要介紹了PHPUnit測試私有屬性和方法功能,結合實例形式較為詳細的分析了使用PHPUnit針對私有屬性和方法進行測試的相關操作技巧與注意事項,需要的朋友可以參考下
本文實例講述了PHPUnit測試私有屬性和方法功能。分享給大家供大家參考,具體如下:
一、測試類別中的私有方法:
##
class Sample { private $a = 0; private function run() { echo $a; } }
class testSample() { $method = new ReflectionMethod('Sample', 'run'); $method->setAccessible(true); //将run方法从private变成类似于public的权限 $method->invoke(new Sample()); //调用run方法 }
private static function run() { echo 'run is a private static function'; }
那麼invoke函數還可以這麼寫:
$method->invoke(null); //只有静态方法可以不必传类的实例化
#如果run還需要傳參,例如:
private function run($x, $y) { return $x + $y; }
那麼,測試程式碼可以改為:
$method->invokeArgs(new Sample(), array(1, 2)); //array中依次写入要传的参数。执行结果返回3
【注意】:利用反射的方法測試私有方法雖好,但setAccessible函數是php5.3.2版本以後才支援的(>=5.3.2)
二、私有屬性的get/set#說完了私有方法,再來看看私有屬性,依舊拿Sample類別作為例子,想要取得或設定Sample類別中的私有屬性$a的值可以用以下方法:
public function testPrivateProperty() { $reflectedClass = new ReflectionClass('Sample'); $reflectedProperty = $reflectedClass->getProperty('a'); $reflectedProperty->setAccessible(true); $reflectedProperty->getValue(); //获取$a的值 $reflectedProperty->setValue(123); //给$a赋值:$a = 123; }
上述方法對靜態屬性仍然有效。
到此,是不是瞬間感覺測試私有方法或屬性變得很容易了。
附:PHPunit 測試私有方法(英文原文)This article is part of a series on testing untestable code:
- Testing private methods
- Testing code that uses singletons
- Stubbing static methods
- #Stubbing static methods
One question I get over and over again when talking about Unit Testing is this:
"How do I test the private attributes and methods of my objects?"
Lets assume we have a class Foo:
<?php class Foo { private $bar = 'baz'; public function doSomething() { return $this->bar = $this->doSomethingPrivate(); } private function doSomethingPrivate() { return 'blah'; } } ?>
<?php class FooTest extends PHPUnit_Framework_TestCase { /** * @covers Foo::doSomething * @covers Foo::doSomethingPrivate */ public function testDoSomething() { $foo = new Foo; $this->assertEquals('blah', $foo->doSomething()); } } ?>
The test above
#
<?php class FooTest extends PHPUnit_Framework_TestCase { public function testPrivateAttribute() { $this->assertAttributeEquals( 'baz', /* expected value */ 'bar', /* attribute name */ new Foo /* object */ ); } } ?>
##1 testDoSomething() only works correctly whentestDoSomethingPrivate() works correctly. This means that we have indirectly testedtestDoSomethingPrivate(). The problem with this approach is that when the test fails we do not know directly where the root cause for the failure is. It could be in eithertestDoSomething() or testDoSomethingPrivate(). This makes the test less valuable.
PHPUnit supports reading protected and private attributes through thePHPUnit_Framework_Assert::readAttribute quals() exist to express assertions onprotected and private attributes:
<?php class FooTest extends PHPUnit_Framework_TestCase { /** * @covers Foo::doSomethingPrivate */ public function testPrivateMethod() { $method = new ReflectionMethod( 'Foo', 'doSomethingPrivate' ); $method->setAccessible(TRUE); $this->assertEquals( 'blah', $method->invoke(new Foo) ); } } ?>
PHP 5.3.2 introduces the ReflectionMethod::setAccessible() method to allow the invocation of protected and private methods through the Reflmethod to allow the invocation of protected and private methods through the Reflmethod to allow the invocation of protected and private methods through the Reflmethod to allow the invocation of protected and private methods through the Reflmethod to allow the invocation of protected and private methods through the Reflmethod to allow the invocation of protected and private methods through the Reflmethod to allow API:
rrreee#I agree with Dave Thomas and Andy Hunt, who write in their book "Pragmatic Unit Testing":
In the test above we directly test testDoSomethingPrivate(). When it fails we immediately know where to look for the root cause.
"In general, you don't want to break any encapsulation for the sake of testing ( or as Mom used to say, "don't expose your privates!"). Most of the time, you should be able to test a class by exercising its public methods. If there is significant functionality that private methods. , that might be a warning sign that there's another class in there struggling to get out."
###So: Just because the testing of protected and private attributes and methods is possible does not mean that this is a "good thing".######以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網! ######相關推薦:###
#
以上是對於PHPUnit測試私有屬性和方法的功能分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

番茄小說是一款非常熱門的小說閱讀軟體,我們在番茄小說中經常會有新的小說和漫畫可以去閱讀,每一本小說和漫畫都很有意思,很多小伙伴也想著要去寫小說來賺取賺取零用錢,在把自己想要寫的小說內容編輯成文字,那麼我們要怎麼樣在這裡面去寫小說呢?小伙伴們都不知道,那就讓我們一起到本站本站中花點時間來看寫小說的方法介紹。分享番茄小說寫小說方法教學 1、先在手機上打開番茄免費小說app,點擊個人中心——作家中心 2、跳到番茄作家助手頁面——點擊創建新書在小說的結

七彩虹主機板在中國國內市場享有較高的知名度和市場佔有率,但是有些七彩虹主機板的用戶還不清楚怎麼進入bios進行設定呢?針對這一情況,小編專門為大家帶來了兩種進入七彩虹主機板bios的方法,快來試試吧!方法一:使用u盤啟動快捷鍵直接進入u盤裝系統七彩虹主機板一鍵啟動u盤的快捷鍵是ESC或F11,首先使用黑鯊裝機大師製作一個黑鯊U盤啟動盤,然後開啟電腦,當看到開機畫面的時候,連續按下鍵盤上的ESC或F11鍵以後將會進入到一個啟動項順序選擇的窗口,將遊標移到顯示“USB”的地方,然

而後悔莫及、人們常常會因為一些原因不小心刪除某些聯絡人、微信作為一款廣泛使用的社群軟體。幫助用戶解決這個問題,本文將介紹如何透過簡單的方法找回被刪除的聯絡人。 1.了解微信聯絡人刪除機制這為我們找回被刪除的聯絡人提供了可能性、微信中的聯絡人刪除機制是將其從通訊錄中移除,但並未完全刪除。 2.使用微信內建「通訊錄恢復」功能微信提供了「通訊錄恢復」節省時間和精力,使用者可以透過此功能快速找回先前刪除的聯絡人,功能。 3.進入微信設定頁面點選右下角,開啟微信應用程式「我」再點選右上角設定圖示、進入設定頁面,,

furmark怎麼看?1、在主介面中設定“運行模式”和“顯示模式”,還能調整“測試模式”,點擊“開始”按鈕。 2.等待片刻後,就會看到測試結果,包含了顯示卡各種參數。 furmark怎麼算合格? 1.用furmark烤機,半小時左右看一下結果,基本上在85度左右徘徊,峰值87度,室溫19度。大號機箱,5個機殼風扇口,前置兩個,上置兩個,後置一個,不過只裝了一個風扇。所有配件都沒有超頻。 2.一般情況下,顯示卡的正常溫度應在「30-85℃」之間。 3.就算是大夏天周圍環境溫度過高,正常溫度也是「50-85℃

手機遊戲成為了人們生活中不可或缺的一部分,隨著科技的發展。它以其可愛的龍蛋形象和有趣的孵化過程吸引了眾多玩家的關注,而其中一款備受矚目的遊戲就是手機版龍蛋。幫助玩家們在遊戲中更好地培養和成長自己的小龍,本文將向大家介紹手機版龍蛋的孵化方法。 1.選擇合適的龍蛋種類玩家需要仔細選擇自己喜歡並且適合自己的龍蛋種類,根據遊戲中提供的不同種類的龍蛋屬性和能力。 2.提升孵化機的等級玩家需要透過完成任務和收集道具來提升孵化機的等級,孵化機的等級決定了孵化速度和孵化成功率。 3.收集孵化所需的資源玩家需要在遊戲中

字體大小的設定成為了重要的個人化需求,隨著手機成為人們日常生活的重要工具。以滿足不同使用者的需求、本文將介紹如何透過簡單的操作,提升手機使用體驗,調整手機字體大小。為什麼需要調整手機字體大小-調整字體大小可以使文字更清晰易讀-適合不同年齡段用戶的閱讀需求-方便視力不佳的用戶使用手機系統自帶字體大小設置功能-如何進入系統設置界面-在在設定介面中找到並進入"顯示"選項-找到"字體大小"選項並進行調整第三方應用調整字體大小-下載並安裝支援字體大小調整的應用程式-開啟應用程式並進入相關設定介面-根據個人

在現今社會,手機已經成為我們生活中不可或缺的一部分。而微信作為我們日常溝通、工作、生活的重要工具,更是經常被使用。然而,在處理不同事務時可能需要分開兩個微信帳號,這就要求手機能夠支援同時登入兩個微信帳號。華為手機作為國內知名品牌,很多人使用,那麼華為手機開啟兩個微信帳號的方法是怎麼樣的呢?下面就來揭秘一下這個方法。首先,要在華為手機上同時使用兩個微信帳號,最簡

新派幻想仙俠MMORPG《誅仙2》「無為測試」即將於4月23日開啟,在原著千年後的誅仙大陸,會發生怎樣的全新仙俠冒險故事?六境仙俠大世界,全職修仙學府,自由自在的修仙生活,仙界中的萬般妙趣都在等待著仙友們親自前往探索! 「無為測試」預先下載現已開啟,仙友們可前往官網下載,開服前無法登入遊戲伺服器,啟動碼可在預先下載安裝完成後使用。 《誅仙2》「無為測試」開放時間:4月23日10:00——5月6日23:59誅仙正統續作全新仙俠冒險篇章《誅仙2》以《誅仙》小說為藍圖,在繼承原著世界觀的基礎上,將遊戲背景設
