From 5f1e1189d4028a40dc5d0b409058e79be60ffdb7 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 16 Nov 2020 16:14:47 +0100 Subject: [PATCH] Adds possibility of assert content on filesystem assertExist (#35231) --- src/Illuminate/Filesystem/FilesystemAdapter.php | 13 ++++++++++++- tests/Filesystem/FilesystemAdapterTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index fb3ffdc39255..1c33b9892676 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -52,9 +52,10 @@ 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); @@ -62,6 +63,16 @@ public function assertExists($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; diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 6f4d396ed120..8e2cac163ce0 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -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() @@ -290,6 +295,11 @@ public function testPutFile() $this->assertFileExists($filePath); $filesystemAdapter->assertExists($storagePath); + + $filesystemAdapter->assertExists( + $storagePath, + 'uploaded file content' + ); } public function testPutFileWithAbsoluteFilePath() @@ -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' + ); } }