假装Laravel存储磁盘的例外

发布于 2025-01-23 04:17:23 字数 1155 浏览 0 评论 0原文

您好,我试图失败以模拟异常或问题,目的是测试处理该错误的代码,测试快乐案例,成功上传它很容易,但是我找不到有关它们的例外情况,

这是段落在所讨论的代码中,只需将文件上传到存储磁盘,

private function code_that_runs_on_the_app(string $file, string $filename)
{
    $disk = Storage::disk('a_disk_used_for_this');

    if ($disk->putFileAs('/', $file, $filename)) {
        unlink($file);
    } else {
        // Handle the error case, like some write permission error 
    }
}

这就是“失败”的测试,因为它实际上上传了文件,我需要在putfileas上伪造错误,或者可能是一个例外或无效响应

public function it_tests_some_part_of_my_code()
{
    $disk = Storage::fake('a_disk_used_for_this');

    // Here comes some code that triggers the code needed

    // Because it supposedly failed this should be empty
    $this->assertEmpty($disk->allFiles());
}

。文档发现我可以使用类似的东西

Storage::shouldReceive('putFileAs')->once()->andThrow(new \Exception('Testing'));
$disk = Storage::fake('a_disk_used_for_this');
...

,但即使在那一步中也会失败。

您知道一种伪造的方法,例如超时错误,连接错误,权限错误等吗?

提前致谢

Hello I'm trying unsuccessfully to emulate an exception or a problem with the purpose of testing the code that handles that error, testing the happy case, successful upload it's easy but the exception cases I couldn't find information about them

This is a snippet of the code in question, just uploading a file to a storage disk

private function code_that_runs_on_the_app(string $file, string $filename)
{
    $disk = Storage::disk('a_disk_used_for_this');

    if ($disk->putFileAs('/', $file, $filename)) {
        unlink($file);
    } else {
        // Handle the error case, like some write permission error 
    }
}

This is the test that "fails" because it actually upload the file, I need to fake an error on the putFileAs could be an exception or a null response

public function it_tests_some_part_of_my_code()
{
    $disk = Storage::fake('a_disk_used_for_this');

    // Here comes some code that triggers the code needed

    // Because it supposedly failed this should be empty
    $this->assertEmpty($disk->allFiles());
}

Reading about that on the documentation found that I could use something like

Storage::shouldReceive('putFileAs')->once()->andThrow(new \Exception('Testing'));
$disk = Storage::fake('a_disk_used_for_this');
...

But it fails even on that step.

Do you know a way to fake like a timeout error, a connection error, permissions error, etc?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2025-01-30 04:17:23

我不确定什么对您不起作用,但我认为这会对您有所帮助:

public function it_tests_some_part_of_my_code()
{
    Storage::shouldReceive('putFileAs')
        ->once()
        ->andThrow(new Exception($errorMessage = 'Testing'));

    Storage::shouldReceive('disk')
        ->once()
        ->andReturnNull();

    $this->expectException(Exception::class);
    $this->expectExceptionMessage($errorMessage);


    // Call the class/method that is going to trigger this exception
}

I am not sure what is not working for you, but I think this will help you:

public function it_tests_some_part_of_my_code()
{
    Storage::shouldReceive('putFileAs')
        ->once()
        ->andThrow(new Exception($errorMessage = 'Testing'));

    Storage::shouldReceive('disk')
        ->once()
        ->andReturnNull();

    $this->expectException(Exception::class);
    $this->expectExceptionMessage($errorMessage);


    // Call the class/method that is going to trigger this exception
}
紅太極 2025-01-30 04:17:23

要测试超时错误,您可以受到。就个人而言,没有尝试过,但是您可以尝试将时间限制设置为非常低的值。

要测试连接错误,试图复制到某些随机不存在的服务器可能会起作用。只需将设置在何处将参数复制到https://a/

以测试许可,您可以在测试中使用mkdir()然后使用CHMODchown设置其权限。

需要明确的是,我没有尝试过任何这些,但是他们应该努力提出错误/例外。

to test a timeout error, you can get inspired by PHP copy/fopen etc. times out. Personally, haven't tried, but you can try out setting the time limit to very low value.

To test a connection error, trying to copy to some random-not-existing server might work. Simply set the where to copy parameter to https://a/

To test permission, you can create a directory within a test using mkdir() and then set its permissions using chmod or chown.

To be clear, I have not tried any of these, but they should work to throw the errors/exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文