麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

對于PHPUnit測試私有屬性和方法的功能分析

2020-03-22 20:31:51
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHPUnit測試私有屬性和方法功能,結合實例形式較為詳細的分析了使用PHPUnit針對私有屬性和方法進行測試的相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了PHPUnit測試私有屬性和方法功能。分享給大家供大家參考,具體如下:

一、測試類中的私有方法:

html' target='_blank'>class Sample private $a = 0; private function run() echo $a;}

上面只是簡單的寫了一個類包含,一個私有變量和一個私有方法。對于protected和private方法,由于無法像是用public方法一樣直接調用,所以在使用phpunit進行單測的時候,多有不便,特別是當一個類中,對外只提供少量接口,內部使用了大量private方法的情況。

對于protected方法,建議使用繼承的方式進行測試,在此就不再贅述。而對于private方法的測試,建議使用php的反射機制來進行。話不多說,上代碼:

class testSample() $method = new ReflectionMethod( Sample , run  $method- setAccessible(true); //將run方法從private變成類似于public的權限 $method- invoke(new Sample()); //調用run方法}

如果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 hard-coded dependencies

No, not those privates. If you need help with those, this book might help.

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:

 ?phpclass Foo private $bar = baz  public function doSomething() return $this- bar = $this- doSomethingPrivate(); private function doSomethingPrivate() return blah ? 

Before we explore how protected and private attributes and methods can be tested directly, lets have a look at how they can be tested indirectly.

The following test calls the testDoSomething() method which in turn calls thedoSomethingPrivate() method:

 ?phpclass 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 assumes that 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() method. Convenience wrappers such asPHPUnit_Framework_TestCase::assertAttributeEquals() exist to express assertions onprotected and private attributes:

 ?phpclass FooTest extends PHPUnit_Framework_TestCase public function testPrivateAttribute() $this- assertAttributeEquals( baz , /* expected value */ bar , /* attribute name */ new Foo /* object */? 

PHP 5.3.2 introduces the ReflectionMethod::setAccessible() method to allow the invocation of protected and private methods through the Reflection API:

 ?phpclass 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)? 

In the test above we directly test testDoSomethingPrivate(). When it fails we immediately know where to look for the root cause.

I agree with Dave Thomas and Andy Hunt, who write in their book Pragmatic Unit Testing :

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 is hidden behind private or protected access, 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 !

相關推薦:

PHP和redis實現悲觀鎖機制的解析

php中可變函數的使用總結

以上就是對于PHPUnit測試私有屬性和方法的功能分析的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美大屁股精品毛片视频 | 成人毛片视频在线观看 | 一级毛片在线看 | 男女无遮挡羞羞视频 | 性爱视频免费 | 免费a级毛片大学生免费观看 | 午夜视频在线看 | 久久草在线观看视频 | free japan xxxxhdsex69| 亚洲成年人免费网站 | www.精品在线 | 欧美一级美片在线观看免费 | 一级电影中文字幕 | 欧美成人精品一区二区 | 日韩av在线播放一区 | 91精品老司机 | 成人羞羞在线观看网站 | 久久人添人人爽人人爽人人片av | 国产一级小视频 | 99精品无人区乱码在线观看 | 日日夜av | 久久久久久久久久亚洲 | 国产精品欧美久久久久一区二区 | 免费亚洲视频在线观看 | 成人免费在线观看视频 | 久久久久久久免费看 | 一级黄色片武则天 | 超碰人人做人人爱 | 亚洲一区二区中文字幕在线观看 | 性欧美日本| 国产成人精品一区二区仙踪林 | 午夜精品福利视频 | 性aaa| 免费视频aaa| 丁香天堂网| h视频免费看 | 一级做a爱片性色毛片 | 欧美一级做一级爱a做片性 91在线视频观看 | 成人福利电影在线观看 | 天天干导航 | av国产免费 |