【问题标题】:How to write unit test for a php function that executes stored procedure?如何为执行存储过程的php函数编写单元测试?
【发布时间】:2021-06-06 13:54:10
【问题描述】:
public function get(): array
    {

        $val = [
            '@a' =>  $this->c,
            '@b'   => (int)$this->issucess
        ];
        try {
            return $this->db->Execute($this->spName, $val);
        } catch (\Exception $e) {
           
        }
    }

如何为这些在 PHP 中返回时执行存储过程的函数编写单元测试? PHPUnit 中是否有任何示例?

【问题讨论】:

    标签: php unit-testing testing stored-procedures phpunit


    【解决方案1】:

    如果您测试 FUNCTION,NOT PROCEDURE - 创建 db mock

    $testParams = [
      '@a' => 'value',
      '@b' => 'value'
    ];
    //stub procedure results
    $testProcedureResult = [];
    $dbMock = Mockery::mock(Database::class);
    $dbMock->method('Execute')
      ->once()
      ->with('procedure', $testParams)
      ->willReturn($testProcedureResult);
    
    $service = new YourTestedClass($dbMock);
    //other service configuration..
    $this->assertSame($testProcedureResult, $service->get());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2017-02-15
      • 2016-09-25
      相关资源
      最近更新 更多