0
有方法來模擬方法和返回值取決於方法參數?我需要這個來模擬容器並獲得服務。我嘗試這樣做:對象和返回對象值的模擬方法取決於方法參數
$container = $this
->getMockBuilder(Container::class)
->getMock();
$container
->expects($this->any())
->method('get')
->with('logger')
->willReturn($this->loggerMock)//this is logger object
;
$container->expects($this->any())
->method('get')
->with('database')
->will($this->returnValue(self::$pdo));//database object
$this->dataProviderFactory = new DataProviderFactory($container);
而當我把這個:的print_r($容器 - >獲取( '記錄'));應該有Logger對象。
但這不起作用。我得到的錯誤如下:
預期失敗的方法名是相等時,用回調函數作用於傳遞給在運行時方法的參數調用零次或多次
Parameter 0 for invocation Symfony\Component\DependencyInjection\Container::get('logger', 1) does not match expected value.
Failed asserting that two strings are equal.
Expected :'database'
Actual :'logger'
這無關你的問題,但你確定你需要你的工廠要依賴整個容器上?可能會更好地注入它需要的服務,如記錄器和數據庫連接。它使測試更容易一些。 – Cerad
現在我不能這樣做你建議,因爲 由於我開發的應用程序的結構 –