Skip to content

Commit

Permalink
[8.x] Run clearstatcache after deleting file and asserting `Storage…
Browse files Browse the repository at this point in the history
…` using exists/missing (#40257)

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Jan 5, 2022
1 parent ab29010 commit 8c293f9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ public function delete($paths)

foreach ($paths as $path) {
try {
if (! @unlink($path)) {
if (@unlink($path)) {
clearstatcache(false, $path);
} else {
$success = false;
}
} catch (ErrorException $e) {
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function __construct(FilesystemInterface $driver)
*/
public function assertExists($path, $content = null)
{
clearstatcache();

$paths = Arr::wrap($path);

foreach ($paths as $path) {
Expand Down Expand Up @@ -101,6 +103,8 @@ public function assertExists($path, $content = null)
*/
public function assertMissing($path)
{
clearstatcache();

$paths = Arr::wrap($path);

foreach ($paths as $path) {
Expand Down
73 changes: 73 additions & 0 deletions tests/Integration/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Illuminate\Tests\Integration\Filesystem;

use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Process\Process;

/**
* @requires OS Linux|Darwin
*/
class FilesystemTest extends TestCase
{
protected $stubFile;

protected function setUp(): void
{
$this->afterApplicationCreated(function () {
File::put($file = storage_path('app/public/StardewTaylor.png'), File::get(__DIR__.'/Fixtures/StardewTaylor.png'));
$this->stubFile = $file;
});

$this->beforeApplicationDestroyed(function () {
if (File::exists($this->stubFile)) {
File::delete($this->stubFile);
}
});

parent::setUp();
}

public function testItCanDeleteViaFilesystemShouldUpdatesFileExists()
{
$this->assertTrue(File::exists($this->stubFile));
$this->assertTrue(File::isFile($this->stubFile));

File::delete($this->stubFile);

$this->assertFalse(File::exists($this->stubFile));
}

public function testItCanDeleteViaFilesystemRequiresManualClearStatCacheOnFileExistsFromDifferentProcess()
{
$this->assertTrue(File::exists($this->stubFile));
$this->assertTrue(File::isFile($this->stubFile));

Process::fromShellCommandline("rm {$this->stubFile}")->run();

clearstatcache(true, $this->stubFile);
$this->assertFalse(File::exists($this->stubFile));
}

public function testItCanDeleteViaFilesystemShouldUpdatesIsFile()
{
$this->assertTrue(File::exists($this->stubFile));
$this->assertTrue(File::isFile($this->stubFile));

File::delete($this->stubFile);

$this->assertFalse(File::isFile($this->stubFile));
}

public function testItCanDeleteViaFilesystemRequiresManualClearStatCacheOnIsFileFromDifferentProcess()
{
$this->assertTrue(File::exists($this->stubFile));
$this->assertTrue(File::isFile($this->stubFile));

Process::fromShellCommandline("rm {$this->stubFile}")->run();

clearstatcache(true, $this->stubFile);
$this->assertFalse(File::isFile($this->stubFile));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions tests/Integration/Filesystem/StorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Illuminate\Tests\Integration\Filesystem;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Process\Process;

/**
* @requires OS Linux|Darwin
*/
class StorageTest extends TestCase
{
protected $stubFile;

protected function setUp(): void
{
$this->afterApplicationCreated(function () {
File::put($file = storage_path('app/public/StardewTaylor.png'), File::get(__DIR__.'/Fixtures/StardewTaylor.png'));
$this->stubFile = $file;
});

$this->beforeApplicationDestroyed(function () {
if (File::exists($this->stubFile)) {
File::delete($this->stubFile);
}
});

parent::setUp();
}

public function testItCanDeleteViaStorage()
{
Storage::disk('public')->assertExists('StardewTaylor.png');
$this->assertTrue(Storage::disk('public')->exists('StardewTaylor.png'));

Storage::disk('public')->delete('StardewTaylor.png');

Storage::disk('public')->assertMissing('StardewTaylor.png');
$this->assertFalse(Storage::disk('public')->exists('StardewTaylor.png'));
}

public function testItCanDeleteViaFilesystemShouldUpdatesStorage()
{
Storage::disk('public')->assertExists('StardewTaylor.png');
$this->assertTrue(Storage::disk('public')->exists('StardewTaylor.png'));

File::delete($this->stubFile);

Storage::disk('public')->assertMissing('StardewTaylor.png');
$this->assertFalse(Storage::disk('public')->exists('StardewTaylor.png'));
}

public function testItCanDeleteViaFilesystemRequiresManualClearStatCacheOnStorageFromDifferentProcess()
{
Storage::disk('public')->assertExists('StardewTaylor.png');
$this->assertTrue(Storage::disk('public')->exists('StardewTaylor.png'));

Process::fromShellCommandline("rm {$this->stubFile}")->run();

clearstatcache(true, $this->stubFile);
Storage::disk('public')->assertMissing('StardewTaylor.png');
$this->assertFalse(Storage::disk('public')->exists('StardewTaylor.png'));
}
}

0 comments on commit 8c293f9

Please sign in to comment.