|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Filesystem\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\Filesystem\Filesystem; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test class for Filesystem. |
| 18 | + */ |
| 19 | +class FilesystemTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + public function testCopyCreatesNewFile() |
| 22 | + { |
| 23 | + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
| 24 | + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 25 | + |
| 26 | + file_put_contents($sourceFilePath, 'SOURCE FILE'); |
| 27 | + |
| 28 | + $filesystem = new Filesystem(); |
| 29 | + $filesystem->copy($sourceFilePath, $targetFilePath); |
| 30 | + |
| 31 | + $this->assertFileExists($targetFilePath); |
| 32 | + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); |
| 33 | + |
| 34 | + unlink($sourceFilePath); |
| 35 | + unlink($targetFilePath); |
| 36 | + } |
| 37 | + |
| 38 | + public function testCopyOverridesExistingFileIfModified() |
| 39 | + { |
| 40 | + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
| 41 | + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 42 | + |
| 43 | + file_put_contents($sourceFilePath, 'SOURCE FILE'); |
| 44 | + file_put_contents($targetFilePath, 'TARGET FILE'); |
| 45 | + touch($targetFilePath, time() - 1000); |
| 46 | + |
| 47 | + $filesystem = new Filesystem(); |
| 48 | + $filesystem->copy($sourceFilePath, $targetFilePath); |
| 49 | + |
| 50 | + $this->assertFileExists($targetFilePath); |
| 51 | + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); |
| 52 | + |
| 53 | + unlink($sourceFilePath); |
| 54 | + unlink($targetFilePath); |
| 55 | + } |
| 56 | + |
| 57 | + public function testCopyDoesNotOverrideExistingFileByDefault() |
| 58 | + { |
| 59 | + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
| 60 | + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 61 | + |
| 62 | + file_put_contents($sourceFilePath, 'SOURCE FILE'); |
| 63 | + file_put_contents($targetFilePath, 'TARGET FILE'); |
| 64 | + $modificationTime = time() - 1000; |
| 65 | + touch($sourceFilePath, $modificationTime); |
| 66 | + touch($targetFilePath, $modificationTime); |
| 67 | + |
| 68 | + $filesystem = new Filesystem(); |
| 69 | + $filesystem->copy($sourceFilePath, $targetFilePath); |
| 70 | + |
| 71 | + $this->assertFileExists($targetFilePath); |
| 72 | + $this->assertEquals('TARGET FILE', file_get_contents($targetFilePath)); |
| 73 | + |
| 74 | + unlink($sourceFilePath); |
| 75 | + unlink($targetFilePath); |
| 76 | + } |
| 77 | + |
| 78 | + public function testCopyOverridesExistingFileIfForced() |
| 79 | + { |
| 80 | + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
| 81 | + $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 82 | + |
| 83 | + file_put_contents($sourceFilePath, 'SOURCE FILE'); |
| 84 | + file_put_contents($targetFilePath, 'TARGET FILE'); |
| 85 | + $modificationTime = time() - 1000; |
| 86 | + touch($sourceFilePath, $modificationTime); |
| 87 | + touch($targetFilePath, $modificationTime); |
| 88 | + |
| 89 | + $filesystem = new Filesystem(); |
| 90 | + $filesystem->copy($sourceFilePath, $targetFilePath, true); |
| 91 | + |
| 92 | + $this->assertFileExists($targetFilePath); |
| 93 | + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); |
| 94 | + |
| 95 | + unlink($sourceFilePath); |
| 96 | + unlink($targetFilePath); |
| 97 | + } |
| 98 | + |
| 99 | + public function testCopyCreatesTargetDirectoryIfItDoesNotExist() |
| 100 | + { |
| 101 | + $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
| 102 | + $targetFileDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 103 | + $targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file'; |
| 104 | + |
| 105 | + file_put_contents($sourceFilePath, 'SOURCE FILE'); |
| 106 | + |
| 107 | + $filesystem = new Filesystem(); |
| 108 | + $filesystem->copy($sourceFilePath, $targetFilePath); |
| 109 | + |
| 110 | + $this->assertTrue(is_dir($targetFileDirectory)); |
| 111 | + $this->assertFileExists($targetFilePath); |
| 112 | + $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath)); |
| 113 | + |
| 114 | + unlink($sourceFilePath); |
| 115 | + unlink($targetFilePath); |
| 116 | + rmdir($targetFileDirectory); |
| 117 | + } |
| 118 | +} |
0 commit comments