🪅

protected functionのテスト方法

2023/02/21に公開

継承クラスを仮作成して解決

Hoge.php
use Tests/TestCase;

class Hoge
{
	protected function hoge(){
		return 'hoge';
	}
}

Test.php
class Test extends Testcase
{
	private HogeHoge $hoge;
	protected function setUp(): void {
		app()->bind(HogeHoge::class);
		$this->hoge = app(HogeHoge::class);
	}
	
	public function test(){
		$result = $this->hoge->hoge();
		self::assertEquals('hoge', $result);
	}
} 

class HogeHoge extends Hoge
{
	public function hoge(){
		return parent::hoge();
	}
}

公式Doc ヘルパ

https://readouble.com/laravel/9.x/ja/helpers.html

Discussion