2017-05-25 12 views
0

私はテストに問題があります。TableGateway ZF2/ZF3を使ったクラスのテストを書く

データベーステーブルのすべての行をカウントする関数をクラス内に作成しました。データベースにアクセスするには、Zend Frameworks TableGateway classを使用します。私の問題は、私は関数のテストを書く方法を知らないということです。

これを調べる1つの方法は、機能が非常に単純でテストを必要としないことですが、どのように機能させるかを知っているといいでしょう。

AbstractTableGatewayに内部変数$ adapterを設定できる関数はありません。保護された変数$ adapterをどのように設定できますか?

public function testCount() 
{ 
    $sql = "SELECT COUNT(*) AS Count FROM DbTable"; 

    $result = $this->prophesize(Result::class); 
    $result->current()->willReturn(["Count" => 10]); 

    $statement = $this->prophesize(Statement::class); 
    $statement->execute()->willReturn($result); 

    $adapter = $this->prophesize(Adapter::class); 
    $adapter->query($sql)->willReturn($statement); 

    $this->tableGateway->adapter = $adapter; 

    $this->assertSame(10, $this->DbTable->count()); 
} 

答えて

1

TableGateway機能ため

機能

public function count() 
{ 
    $sql = "SELECT COUNT(*) AS Count FROM ". $this->tableGateway->getTable(); 
    $statement = $this->tableGateway->adapter->query($sql); 
    $result = $statement->execute()->current(); 
    return $result['Count']; 
} 

テストは、コンストラクタからそのアダプタを取得します。どんな工場でも$this->tableGatewayプロパティにTableGatewayインスタンスを作成する場合は、模擬アダプタ(またはこれが統合テスト用の場合は実際のアダプタ)をそのコンストラクタに渡す必要があります。

関連する問題