2017-08-04 22 views
1

PHPUnitでreturnValueMap()を使用すると単体テストの初心者で、わかりません。私は今...PHPUnit - Mockedクラスのメソッドを見つけられないMockのReturnValueMap

は、テスト中のこのコードを考えてみましょう日間グーグルでてきました。

public function __construct(EntityManager $entityManager, AuditLog $auditLog) { 
    $this->entityManager = $entityManager; 
    $this->auditLog = $auditLog; 
} 

public function updateSomeId($oldId, $newId) 
{ 
    $repositories = ['repo1', 'repo2', 'repo3']; 

    foreach ($repositories as $repository) { 
     try { 
      $this->entityManager->getRepository($repository) 
       ->updateSomeId($oldId, $newId); 

     } catch (RepositoryException $e) { 
      throw new SomeOtherException($e->getMessage(), $e->getCode(), $e); 
     } 
    } 
} 

ユニットテストコード。

... code removed for brevity 

    $repoMaintenance = new RepoMaintenance(
     $this->getEntityManagerMock(), 
     $this->getAuditLogMock() 
    ); 

    $this->assertTrue($repoMaintenance->updateSomeId(
     $this->oldId, 
     $this->newId 
    )); 

/** 
* @return EntityManager 
*/ 
private function getEntityManagerMock() 
{ 
    $entityManagerMock = $this->getMockBuilder(EntityManager::class) 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $entityManagerMock 
     ->method('getRepository') 
     ->willReturn($this->returnValueMap($this->getEntityManagerReturnValueMap())); 

    return $entityManagerMock; 
} 

/** 
* @return array 
*/ 
private function getEntityManagerReturnValueMap() 
{ 
    return [ 
     ['repo1', $this->getRepo1Mock()], 
     ['repo2', $this->getRepo2Mock()], 
     ['repo3', $this->getRepo3Mock()], 
    ]; 
} 

/** 
* @return Repo1 
*/ 
private function getRepo1Mock() 
{ 
    return $this->getMockBuilder(Repo1::class) 
     ->disableOriginalConstructor() 
     ->getMock(); 
} 

... Code removed for brevity 

ユニットテストを実行すると、次の致命的なエラーが返されます。

PHP Fatal error: Call to undefined method PHPUnit_Framework_MockObject_Stub_ReturnValueMap::updateSomeId()

私は以前に何の問題が公共のコンテキスト内のメソッドにアクセスしていないと、戻り値マップでモックを使用しました。違いは、私はS1234内のprivateアクセスに設定されている__construct()の変数を疑似しようとしていることです。

私には何が欠けていますか?問題(私はめったに推測しません)は、メンバーの私的アクセスレベルが嘲笑されていることです。

このコードを単体テストする方法はありますか?私はいつでもデータベースにヒットしたくないので、これがコールを嘲笑する理由です。

答えて

2

willReturn($this->returnValueMap...

の代わりに will($this->returnValueMap...が必要です。
関連する問題