好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHPUnit測試私有屬性和方法功能示例

PHPUnit測試私有屬性和方法功能示例

熱門標簽:智能外呼電銷系統 寶安400電話辦理 電銷機器人-快迭智能 合肥外呼系統app 哈爾濱400電話辦理到易號網 拉薩打電話機器人 沈陽人工智能電銷機器人公司 高識別電銷機器人 h5 地圖標注

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

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

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:

?php
class 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:

?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 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:

?php
class 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:

?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)
    );
  }
}
?>

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".

參考文獻:

http://php.net/manual/en/class.reflectionmethod.php

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結》、《php字符串(string)用法總結》、《PHP數組(Array)操作技巧大全》、《PHP運算與運算符用法總結》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》及《php優秀開發框架總結》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHPUnit 單元測試安裝與使用入門教程
  • ThinkPHP5與單元測試PHPUnit使用詳解
  • PHPUnit + Laravel單元測試常用技能
  • PHP使用phpunit進行單元測試示例
  • 使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法
  • php中PHPUnit框架實例用法

標簽:巴中 泰州 林芝 威海 梅州 山東 成都 張家口

巨人網絡通訊聲明:本文標題《PHPUnit測試私有屬性和方法功能示例》,本文關鍵詞  PHPUnit,測試,私有,屬性,和,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHPUnit測試私有屬性和方法功能示例》相關的同類信息!
  • 本頁收集關于PHPUnit測試私有屬性和方法功能示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 这么多年在线观看免费完整版| 欧美天天在线| 欧美最猛黑人XXXⅩ性色情| 男女做爰爽到高潮的片色情文章 | 小男生喷出精子视频| 999久久久国产| 一二三四免费播放视频| 肉汁横流辣文一女多男| 大大香大香煮伊在2021一二三| 免费一级毛片久久久人妖| 久久久噜噜噜久久久午夜| 久久久久国产精品| 在车里被弄了H野战高H漫画| 窝窝女人体国产午夜视频| 护士被强摁做开腿呻吟| 两性午夜欧美高清做性| 人人爱人人射| 色窝窝无码一区二区三区在线观看| 欧美经典三级中文字幕电影高清完整版| 日韩综合区| 天天操天天操天天操| 欧美精品日韩一区二区三区| 丰满大肥婆肥奶大屁股| 国产?浪潮AV一区二区| 黄网有哪些| 经典黄色片| 色戒6段床戏是几分几秒| 欧美坐脸| 亚洲精品午夜福利在线观看| 国产小嫩模特一级毛片| 美女视频黄频大全免费| 亚洲日本激情综合在线观看| 小说主角夜麟| 久久国产精品精品国产色综合| 使劲插视频| 字母圈调教室论坛| 美女的尿囗秘?正面图| 丝袜连裤袜欧美激情日韩| 我的变态室友(H)三攻一受| free×性护士vidos欧美| 久热精品视频在线|