简体   繁体   English

如何为执行存储过程的 php function 编写单元测试?

[英]How to write unit test for a php function that executes stored procedure?

public function get(): array
    {

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

How do I write a unit test for such functions which are executing stored procedure while returning in PHP?如何为在 PHP 返回时执行存储过程的此类函数编写单元测试? Is there any example for it in PHPUnit ? PHPUnit中是否有任何示例?

If you test FUNCTION, NOT PROCEDURE - create db mock如果您测试FUNCTION,则不是程序- 创建数据库模拟

$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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM