Php/phpunit/モックオブジェクト/テスト対象にprotectedメソッドが含まれている場合
提供: 初心者エンジニアの簡易メモ
- PointProtected.php
class PointProtected
{
private $_point = 0;
public function add($point)
{
$this->_point += $point;
return $this;
}
public function getPoint()
{
$this->checkException();
return $this->_point;
}
private function checkException()
{
if ($this->_point < 0) {
throw new Exception();
}
}
}
- PointMockProtectedTest.php
require_once __DIR__ . '/../../application/PointProtected.php';
class PointMockProtectedTest extends PHPUnit_Framework_TestCase
{
public function testGetPoint()
{
// Point クラスのスタブを作成します
$stub = $this->getMock('PointProtected', array('checkException'));
// protectedのcheckExceptionを通さないようスタブの設定を行います
$stub->expects($this->any())
->method('checkException')
->will($this->returnValue(null));
$stub->add(-10);
$this->assertThat($stub->getPoint(), $this->equalTo(-10));
}
}
