Skip to content

Commit 821a25a

Browse files
authored
Merge pull request #4 from rcsofttech85/tests
get mime type info
2 parents a49403d + 2b9c8d2 commit 821a25a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"php": ">8.2",
2020
"ext-sodium": "*",
2121
"ext-ctype": "*",
22-
"ext-zip": "*"
22+
"ext-zip": "*",
23+
"ext-fileinfo": "*"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": "^10",
2627
"squizlabs/php_codesniffer": "^3.7",
27-
"symfony/dotenv": "^6.3",
28-
"ext-fileinfo": "*"
28+
"symfony/dotenv": "^6.3"
2929
}
3030
}

src/FileHandler.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace rcsofttech85\FileHandler;
44

5+
use finfo;
56
use Generator;
67
use rcsofttech85\FileHandler\Exception\FileHandlerException;
78
use ZipArchive;
@@ -69,6 +70,24 @@ public function compress(string $filename, string $zipFilename): void
6970
$zip->close();
7071
}
7172

73+
/**
74+
* @throws FileHandlerException
75+
*/
76+
public function getMimeType(string $filename): string
77+
{
78+
if (!file_exists($filename)) {
79+
throw new FileHandlerException('file does not exist.');
80+
}
81+
82+
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
83+
$mimeType = $fileInfo->file($filename);
84+
if (!$mimeType) {
85+
throw new FileHandlerException('unknown mime type');
86+
}
87+
88+
return $mimeType;
89+
}
90+
7291
/**
7392
* @throws FileHandlerException
7493
*/

tests/unit/FileHandlerTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace unit;
44

5-
use finfo;
65
use PHPUnit\Framework\Attributes\DataProvider;
76
use PHPUnit\Framework\Attributes\Test;
87
use PHPUnit\Framework\Attributes\TestDox;
@@ -135,6 +134,7 @@ public function studioIsFoundForExactNameMatch(string $keyword)
135134
$this->assertTrue($isStudioFound);
136135
}
137136

137+
138138
#[Test]
139139
public function successfulCompression()
140140
{
@@ -143,13 +143,22 @@ public function successfulCompression()
143143

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

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

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

152+
#[Test]
153+
public function getMimeTypeFunctionReturnsCorrectInfo()
154+
{
155+
$csvFile = $this->fileHandler->getMimeType("movie.csv");
156+
$zipFile = $this->fileHandler->getMimeType("compressed.zip");
157+
158+
$this->assertEquals("text/csv", $csvFile);
159+
$this->assertEquals('application/zip', $zipFile);
160+
}
161+
153162
#[Test]
154163
public function successfulDecompression()
155164
{

0 commit comments

Comments
 (0)