Skip to content

Commit

Permalink
Adds possibility of assert content on filesystem assertExist (#35231)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 16, 2020
1 parent 470c0a4 commit 5f1e118
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Filesystem/FilesystemAdapter.php
Expand Up @@ -52,16 +52,27 @@ public function __construct(FilesystemInterface $driver)
* Assert that the given file exists.
*
* @param string|array $path
* @param string|null $content
* @return $this
*/
public function assertExists($path)
public function assertExists($path, $content = null)
{
$paths = Arr::wrap($path);

foreach ($paths as $path) {
PHPUnit::assertTrue(
$this->exists($path), "Unable to find a file at path [{$path}]."
);

if (! is_null($content)) {
$actual = $this->get($path);

PHPUnit::assertSame(
$content,
$actual,
"File [{$path}] was found, but content [{$actual}] does not match [{$content}]."
);
}
}

return $this;
Expand Down
15 changes: 15 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Expand Up @@ -262,6 +262,11 @@ public function testPutFileAs()
$filesystemAdapter->assertExists($storagePath);

$this->assertSame('uploaded file content', $filesystemAdapter->read($storagePath));

$filesystemAdapter->assertExists(
$storagePath,
'uploaded file content'
);
}

public function testPutFileAsWithAbsoluteFilePath()
Expand Down Expand Up @@ -290,6 +295,11 @@ public function testPutFile()
$this->assertFileExists($filePath);

$filesystemAdapter->assertExists($storagePath);

$filesystemAdapter->assertExists(
$storagePath,
'uploaded file content'
);
}

public function testPutFileWithAbsoluteFilePath()
Expand All @@ -303,5 +313,10 @@ public function testPutFileWithAbsoluteFilePath()
$this->assertSame(44, strlen($storagePath)); // random 40 characters + ".txt"

$filesystemAdapter->assertExists($storagePath);

$filesystemAdapter->assertExists(
$storagePath,
'uploaded file content'
);
}
}

0 comments on commit 5f1e118

Please sign in to comment.