From 6ac54866723d8ba5561d39e182207dbc79b1466f Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Fri, 6 Apr 2012 17:48:54 +0100 Subject: [PATCH] [Filesystem] Added unit tests for copy method. --- .../Filesystem/Tests/FilesystemTest.php | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/Tests/FilesystemTest.php diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php new file mode 100644 index 0000000000..dff5b9800b --- /dev/null +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -0,0 +1,118 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Tests; + +use Symfony\Component\Filesystem\Filesystem; + +/** + * Test class for Filesystem. + */ +class FilesystemTest extends \PHPUnit_Framework_TestCase +{ + public function testCopyCreatesNewFile() + { + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + + $filesystem = new Filesystem(); + $filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertFileExists($targetFilePath); + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); + + unlink($sourceFilePath); + unlink($targetFilePath); + } + + public function testCopyOverridesExistingFileIfModified() + { + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + file_put_contents($targetFilePath, 'TARGET FILE'); + touch($targetFilePath, time() - 1000); + + $filesystem = new Filesystem(); + $filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertFileExists($targetFilePath); + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); + + unlink($sourceFilePath); + unlink($targetFilePath); + } + + public function testCopyDoesNotOverrideExistingFileByDefault() + { + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + file_put_contents($targetFilePath, 'TARGET FILE'); + $modificationTime = time() - 1000; + touch($sourceFilePath, $modificationTime); + touch($targetFilePath, $modificationTime); + + $filesystem = new Filesystem(); + $filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertFileExists($targetFilePath); + $this->assertEquals('TARGET FILE', file_get_contents($targetFilePath)); + + unlink($sourceFilePath); + unlink($targetFilePath); + } + + public function testCopyOverridesExistingFileIfForced() + { + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + file_put_contents($targetFilePath, 'TARGET FILE'); + $modificationTime = time() - 1000; + touch($sourceFilePath, $modificationTime); + touch($targetFilePath, $modificationTime); + + $filesystem = new Filesystem(); + $filesystem->copy($sourceFilePath, $targetFilePath, true); + + $this->assertFileExists($targetFilePath); + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); + + unlink($sourceFilePath); + unlink($targetFilePath); + } + + public function testCopyCreatesTargetDirectoryIfItDoesNotExist() + { + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFileDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); + $targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + + $filesystem = new Filesystem(); + $filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertTrue(is_dir($targetFileDirectory)); + $this->assertFileExists($targetFilePath); + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); + + unlink($sourceFilePath); + unlink($targetFilePath); + rmdir($targetFileDirectory); + } +}