Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"php": ">8.2",
"ext-sodium": "*",
"ext-ctype": "*",
"ext-zip": "*"
"ext-zip": "*",
"ext-fileinfo": "*"
},
"require-dev": {
"phpunit/phpunit": "^10",
"squizlabs/php_codesniffer": "^3.7",
"symfony/dotenv": "^6.3",
"ext-fileinfo": "*"
"symfony/dotenv": "^6.3"
}
}
19 changes: 19 additions & 0 deletions src/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace rcsofttech85\FileHandler;

use finfo;
use Generator;
use rcsofttech85\FileHandler\Exception\FileHandlerException;
use ZipArchive;
Expand Down Expand Up @@ -69,6 +70,24 @@ public function compress(string $filename, string $zipFilename): void
$zip->close();
}

/**
* @throws FileHandlerException
*/
public function getMimeType(string $filename): string
{
if (!file_exists($filename)) {
throw new FileHandlerException('file does not exist.');
}

$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $fileInfo->file($filename);
if (!$mimeType) {
throw new FileHandlerException('unknown mime type');
}

return $mimeType;
}

/**
* @throws FileHandlerException
*/
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace unit;

use finfo;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
Expand Down Expand Up @@ -135,6 +134,7 @@ public function studioIsFoundForExactNameMatch(string $keyword)
$this->assertTrue($isStudioFound);
}


#[Test]
public function successfulCompression()
{
Expand All @@ -143,13 +143,22 @@ public function successfulCompression()

$this->fileHandler->compress($testFile, $compressedZipFilename);

$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $fileInfo->file($compressedZipFilename);
$mimeType = $this->fileHandler->getMimeType($compressedZipFilename);

$this->assertFileExists($compressedZipFilename);
$this->assertEquals('application/zip', $mimeType);
}

#[Test]
public function getMimeTypeFunctionReturnsCorrectInfo()
{
$csvFile = $this->fileHandler->getMimeType("movie.csv");
$zipFile = $this->fileHandler->getMimeType("compressed.zip");

$this->assertEquals("text/csv", $csvFile);
$this->assertEquals('application/zip', $zipFile);
}

#[Test]
public function successfulDecompression()
{
Expand Down