laravelstoragefaker

Laravel Storage Fake - delete faked created files


I have question about the Storake Faker for Laravel. I had a hard time to get my code to work. Now it works, but I don't really understand the principle of the Storake Faker.

You can see a short version of my working code below: The setUp function creates a Storake fake and a file. The path of the file is then stored to Database.

After that it calls a function in a Repository, which checks if the path exists. I'm not allowed to show you that code, but anyway it doesn't matter, because it's working ;)

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

Well, so far so good. But after my test I have to delete the created File manually, like you can see in the tearDown method.

Here we come to my question. I read many posts, that the Storage faker does not delete the created files by itself. But why not? I mean, for what else do I create a Storage fake, when I have to delete the files after the test manually. I really dont get it.

Or did I understand something wrong and it is possible to get the files automatically deleted by the Storage Faker?

EDIT:: Solution:

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');
}

I changed the disk from 'protocols' to 'local'. 'protocols' is just a path in the disk 'local', but not the disk itself.

After this small change, I could delete the tearDown function, because now it deletes the created files after the tests


Solution

  • Okay, I found the failure. How stupid I was :D

    I've made a fake of 'protocols'

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

    But my disk is not 'protocols' (it is just a path in local), it is 'local'. So I changed these lines to:

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

    And I deleted the tearDown method. Now it works. It delets all created files by itself and the Tests are still green.