【问题标题】:Laravel Storage Fake - delete faked created filesLaravel Storage Fake - 删除伪造的创建文件
【发布时间】:2021-12-24 18:44:14
【问题描述】:

我对 Laravel 的 Storake Faker 有疑问。我很难让我的代码正常工作。现在可以了,但我不是很了解 Storake Faker 的原理。

您可以在下面看到我的工作代码的简短版本:setUp 函数创建一个 Storake 假文件和一个文件。然后将文件的路径存储到数据库中。

之后,它调用存储库中的一个函数,该函数检查路径是否存在。我不允许向您展示该代码,但无论如何都没有关系,因为它正在工作;)

use WithoutMiddleware;
use DatabaseTransactions;

protected $file;

protected function setUp(): void
{
    parent::setUp();

    $path = 'protocols';

    Storage::fake($path);

    $this->file = UploadedFile::fake()->createWithContent(
        'protocol.html',
        'Import gestartet am: 03.09.2019 10:54:13'
    )->store($path);

    factory(Protocol::class)->create([
        'partner_id' => 1,
        'user_id' => 1,
        'path' => $this->file
    ]);
}

/** @test */
public function path_exists()
{
    //Called function returns true

    $path_exists = functionCall('protocols/Test.pdf');
    $this->assertTrue($path_exists);

    $path_exists = functionCall('protocols/Test2.pdf');
    $this->assertTrue($path_exists);

    $path_exists = functionCall('protocols/Test3.pdf');
    $this->assertTrue($path_exists);
}

/** @test */
public function path_does_not_exist()
{
    //Called function throws an Exception

    $this->expectException(Exception::class);
    $this->expectExceptionMessage('file_not_found');
    $path_exists = functionCall('protocols/XY.pdf');

    $this->expectException(Exception::class);
    $this->expectExceptionMessage('file_not_found');
    $path_exists = functionCall('protocols/XY2.pdf');
}

protected function tearDown(): void
{
    Storage::delete($this->file);

    parent::tearDown();
}

嗯,到目前为止一切顺利。但是在我的测试之后,我必须手动删除创建的文件,就像你在 tearDown 方法中看到的那样。

我们来回答我的问题。我读了很多帖子,存储伪造者不会自行删除创建的文件。但为什么不呢?我的意思是,当我必须在测试后手动删除文件时,我还要创建一个 Storage fake。我真的不明白。

还是我理解有误,可以让 Storage Faker 自动删除文件?

编辑:: 解决方案:

use WithoutMiddleware;
use DatabaseTransactions;

protected $file;

protected function setUp(): void
{
    parent::setUp();

    $path = 'local';

    Storage::fake($path);

    $this->file = UploadedFile::fake()->createWithContent(
        'protocol.html',
        'Import gestartet am: 03.09.2019 10:54:13'
    )->store($path);

    factory(Protocol::class)->create([
        'partner_id' => 1,
        'user_id' => 1,
        'path' => $this->file
    ]);
}

/** @test */
public function path_exists()
{
    //Called function returns true

    $path_exists = functionCall('protocols/Test.pdf');
    $this->assertTrue($path_exists);

    $path_exists = functionCall('protocols/Test2.pdf');
    $this->assertTrue($path_exists);

    $path_exists = functionCall('protocols/Test3.pdf');
    $this->assertTrue($path_exists);
}

/** @test */
public function path_does_not_exist()
{
    //Called function throws an Exception

    $this->expectException(Exception::class);
    $this->expectExceptionMessage('file_not_found');
    $path_exists = functionCall('protocols/XY.pdf');

    $this->expectException(Exception::class);
    $this->expectExceptionMessage('file_not_found');
    $path_exists = functionCall('protocols/XY2.pdf');
}

我将磁盘从“协议”更改为“本地”。 'protocols' 只是磁盘 'local' 中的一个路径,而不是磁盘本身。

在这个小改动之后,我可以删除 tearDown 函数,因为现在它会删除测试后创建的文件

【问题讨论】:

    标签: laravel storage faker


    【解决方案1】:

    调用Storage::fake($path); 确实会删除该指定路径中的所有文件。 但是它不会在您完成测试后删除文件。但它可以确保在您重新运行它的第二次没有留下任何文件。

    【讨论】:

    • 我稍微编辑了我的帖子。现在您可以看到我有两个测试函数,分别称为path_exists()path_does_not_exist()。我在这些函数中有一些函数调用。你现在能不能让你的答案更清楚我应该把Storage::fake($path);放在哪里,它会清除创建的文件。我不确定我是否理解你的回答。我试图在每次函数调用之后都放它,但它没有清除路径。
    【解决方案2】:

    好的,我发现了失败。我是多么愚蠢:D

    我伪造了“协议”

    $path = 'protocols';
    Storage::fake($path);
    

    但我的磁盘不是“协议”(它只是本地路径),它是“本地”。所以我将这些行改为:

    $path = 'local';
    Storage::fake($path);
    

    我删除了 tearDown 方法。现在它起作用了。它会自行删除所有创建的文件,并且测试仍然是绿色的。

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2020-06-05
      • 2020-05-24
      • 1970-01-01
      • 2023-04-11
      • 2016-05-31
      • 2012-01-29
      • 2017-02-21
      • 2016-09-30
      相关资源
      最近更新 更多