2
0

FileInfoTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Test\Michel\FileSystem;
  3. use Michel\FileSystem\FileInfo;
  4. use Michel\UniTester\TestCase;
  5. use RuntimeException;
  6. use SplFileInfo;
  7. use SplFileObject;
  8. class FileInfoTest extends TestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. // TODO: Implement setUp() method.
  13. }
  14. protected function tearDown(): void
  15. {
  16. // TODO: Implement tearDown() method.
  17. }
  18. protected function execute(): void
  19. {
  20. $this->testGetSplFileInfo();
  21. $this->testGetMimeType();
  22. $this->testGetExtension();
  23. $this->testOpenFile();
  24. $this->testOpenFileInvalidMode();
  25. $this->testDirectory();
  26. $this->testDelete();
  27. $this->testGetMetadata();
  28. $this->testCompareWith();
  29. }
  30. public function testGetSplFileInfo()
  31. {
  32. $file = new FileInfo(__FILE__);
  33. $this->assertInstanceOf(SplFileInfo::class, $file->getSplFileInfo());
  34. }
  35. public function testGetMimeType()
  36. {
  37. $fileInfo = new FileInfo(__FILE__);
  38. $this->assertEquals('text/x-php', $fileInfo->getMimeType());
  39. }
  40. public function testGetExtension()
  41. {
  42. $fileInfo = new FileInfo(__FILE__);
  43. $this->assertEquals('php', $fileInfo->getExtension());
  44. }
  45. public function testOpenFile()
  46. {
  47. $fileInfo = new FileInfo(__FILE__);
  48. $this->assertInstanceOf(SplFileObject::class, $fileInfo->openFile());
  49. }
  50. public function testOpenFileInvalidMode()
  51. {
  52. $this->expectException(RuntimeException::class, function () {
  53. $fileInfo = new FileInfo(__FILE__);
  54. $fileInfo->openFile('invalid');
  55. });
  56. }
  57. public function testDirectory()
  58. {
  59. $this->expectException(RuntimeException::class, function () {
  60. new FileInfo(__DIR__);
  61. });
  62. }
  63. public function testDelete()
  64. {
  65. $tmpFile = tempnam(sys_get_temp_dir(), 'tmp__');
  66. $fileInfo = new FileInfo($tmpFile);
  67. $this->assertTrue($fileInfo->delete());
  68. $this->assertFalse(file_exists($tmpFile));
  69. }
  70. public function testGetMetadata()
  71. {
  72. $filePath = __FILE__;
  73. $fileSize = filesize($filePath);
  74. $mimeType = 'text/x-php';
  75. $extension = 'php';
  76. $basename = basename($filePath);
  77. $lastModified = filemtime($filePath);
  78. $creationDate = filectime($filePath);
  79. $fileInfo = new FileInfo($filePath);
  80. $metadata = $fileInfo->getMetadata();
  81. $this->assertEquals([
  82. 'path' => $filePath,
  83. 'size' => $fileSize,
  84. 'size_in_kb' => round($fileSize / 1024, 2),
  85. 'size_in_mb' => round($fileSize / 1024 / 1024, 2),
  86. 'mime_type' => $mimeType,
  87. 'extension' => $extension,
  88. 'basename' => $basename,
  89. 'last_modified' => date('Y-m-d H:i:s', $lastModified),
  90. 'creation_date' => date('Y-m-d H:i:s', $creationDate)
  91. ], $metadata);
  92. }
  93. public function testCompareWith()
  94. {
  95. $filePath1 = __FILE__;
  96. $filePath2 = __FILE__;
  97. $fileInfo1 = new FileInfo($filePath1);
  98. $fileInfo2 = new FileInfo($filePath2);
  99. $result = $fileInfo1->compareWith($fileInfo2);
  100. $this->assertTrue($result);
  101. $filePath1 = __DIR__.'/resources/syncsource/file1.txt';
  102. $filePath2 = __DIR__.'/resources/syncsource/file2.txt';
  103. $fileInfo1 = new FileInfo($filePath1);
  104. $fileInfo2 = new FileInfo($filePath2);
  105. $result = $fileInfo1->compareWith($fileInfo2);
  106. $this->assertFalse($result);
  107. }
  108. }