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

不言
發布: 2023-03-31 20:00:02
原創
1359 人瀏覽過

這篇文章主要介紹了PHPUnit測試私有屬性和方法功能,結合實例形式較為詳細的分析了使用PHPUnit針對私有屬性和方法進行測試的相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了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 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 = &#39;baz&#39;;
  public function doSomething()
  {
    return $this->bar = $this->doSomethingPrivate();
  }
  private function doSomethingPrivate()
  {
    return &#39;blah&#39;;
  }
}
?>
登入後複製

Before we explore how protected and private attributes and methods can be tested directly, lets have a look at how the can be testy can be ed 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(&#39;blah&#39;, $foo->doSomething());
  }
}
?>
登入後複製

The test above

#

<?php
class FooTest extends PHPUnit_Framework_TestCase
{
  public function testPrivateAttribute()
  {
    $this->assertAttributeEquals(
     &#39;baz&#39;, /* expected value */
     &#39;bar&#39;, /* 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(
     &#39;Foo&#39;, &#39;doSomethingPrivate&#39;
    );
    $method->setAccessible(TRUE);
    $this->assertEquals(
     &#39;blah&#39;, $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


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 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中文網! ######相關推薦:###

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

#php中可變函數的使用總結

#

以上是對於PHPUnit測試私有屬性和方法的功能分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!