Skip to content

Commit 2f01d39

Browse files
authored
code coverage (#50)
test code coverage Signed-off-by: rahul <[email protected]>
1 parent c4156bd commit 2f01d39

15 files changed

+543
-112
lines changed

src/CsvFileHandler.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ public function findAndReplaceInCsv(
7676
string $filename,
7777
string $keyword,
7878
string $replace,
79-
string|null $column = null
79+
string|null $column = null,
80+
string|null $dirName = null
8081
): bool {
8182
$headers = $this->extractHeader($filename);
8283

8384
if (!$headers) {
8485
throw new FileHandlerException('failed to extract header');
8586
}
8687

87-
$tempFilePath = $this->tempFileHandler->createTempFileWithHeaders($headers);
88+
$tempFilePath = $this->tempFileHandler->createTempFileWithHeaders($headers, $dirName);
8889
if (!$tempFilePath) {
89-
return false;
90+
throw new FileHandlerException('could not create temp file');
9091
}
9192

92-
9393
try {
9494
$count = 0;
9595
foreach ($this->getRows($filename) as $row) {
@@ -168,11 +168,7 @@ private function isValidCsvFileFormat(array $row): bool
168168
private function getRows(string $filename, array|false $hideColumns = false, int|false $limit = false): Generator
169169
{
170170
$filename = $this->validateFileName($filename);
171-
$csvFile = fopen($filename, 'r');
172-
if (!$csvFile) {
173-
throw new FileHandlerException('file not found');
174-
}
175-
171+
$csvFile = $this->openFileAndReturnResource($filename, 'r');
176172
$headers = $this->extractHeader($csvFile);
177173
if (!is_array($headers)) {
178174
fclose($csvFile);

src/DependencyInjection/ServiceContainer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public function getContainerBuilder(): ContainerBuilder
1919
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../src/config'));
2020
$loader->load('services.yaml');
2121

22-
$containerBuilder->setParameter('STORED_HASH_FILE', $_ENV['STORED_HASH_FILE']);
22+
foreach ($_ENV as $key => $value) {
23+
$containerBuilder->setParameter($key, $value);
24+
}
25+
2326

2427
return $containerBuilder;
2528
}

src/FileEncryptor.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public function encryptFile(): bool
5454

5555
$output = bin2hex($nonce . $ciphertext);
5656

57-
$file = fopen($this->filename, 'w');
58-
if (!$file) {
59-
return false;
60-
}
57+
$file = $this->openFileAndReturnResource($this->filename);
6158

6259
try {
6360
fwrite($file, $output);
@@ -69,10 +66,10 @@ public function encryptFile(): bool
6966
}
7067

7168
/**
72-
*
69+
* @return bool
7370
* @throws FileEncryptorException
71+
* @throws FileHandlerException
7472
* @throws SodiumException
75-
*
7673
*/
7774
public function decryptFile(): bool
7875
{
@@ -86,10 +83,8 @@ public function decryptFile(): bool
8683
throw new FileEncryptorException('file is not encrypted');
8784
}
8885

89-
$bytes = hex2bin($encryptedData);
90-
if (!$bytes) {
91-
return false;
92-
}
86+
$bytes = $this->convertHexToBin($encryptedData);
87+
9388
$nonce = substr($bytes, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
9489
$ciphertext = substr($bytes, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
9590

@@ -102,10 +97,7 @@ public function decryptFile(): bool
10297
}
10398

10499

105-
$file = fopen($this->filename, 'w');
106-
if (!$file) {
107-
return false;
108-
}
100+
$file = $this->openFileAndReturnResource($this->filename);
109101

110102
try {
111103
fwrite($file, $plaintext);
@@ -115,4 +107,18 @@ public function decryptFile(): bool
115107

116108
return true;
117109
}
110+
111+
/**
112+
* @param string $encryptedData
113+
* @return string
114+
* @throws FileEncryptorException
115+
*/
116+
public function convertHexToBin(string $encryptedData): string
117+
{
118+
$bytes = hex2bin($encryptedData);
119+
if (!$bytes) {
120+
throw new FileEncryptorException('could not convert hex to bin');
121+
}
122+
return $bytes;
123+
}
118124
}

src/FileHandler.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,32 @@ public function write(string $data, ?int $length = null): void
6363
/**
6464
* @throws FileHandlerException
6565
*/
66-
public function compress(string $filename, string $zipFilename): void
66+
public function compress(string $filename, string $zipFilename, int $flag = ZipArchive::CREATE): void
6767
{
6868
$filename = $this->validateFileName($filename);
6969

7070
$zip = new ZipArchive();
7171

72-
if (!$zip->open($zipFilename, ZipArchive::CREATE)) {
73-
throw new FileHandlerException('Failed to create the ZIP archive.');
74-
}
7572

76-
if (!$zip->addFile($filename)) {
77-
throw new FileHandlerException('Failed to add the file to the ZIP archive.');
73+
if (true !== $zip->open($zipFilename, $flag)) {
74+
throw new FileHandlerException('Failed to create the ZIP archive.');
7875
}
7976

80-
77+
$zip->addFile($filename);
8178
$zip->close();
8279
}
8380

8481
/**
8582
* @throws FileHandlerException
8683
*/
87-
public function getMimeType(string $filename): string
84+
public function getMimeType(string $filename): string|false
8885
{
8986
$filename = $this->validateFileName($filename);
9087

88+
9189
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
9290
$mimeType = $fileInfo->file($filename);
93-
if (!$mimeType) {
91+
if ($mimeType === 'application/octet-stream') {
9492
throw new FileHandlerException('unknown mime type');
9593
}
9694

@@ -100,35 +98,29 @@ public function getMimeType(string $filename): string
10098
/**
10199
* @throws FileHandlerException
102100
*/
103-
public function decompress(string $zipFilename, string $extractPath = "./"): void
101+
public function decompress(string $zipFilename, string $extractPath = "./", int $flag = ZipArchive::CREATE): void
104102
{
105103
$zipFilename = $this->validateFileName($zipFilename);
106104

107105
$zip = new ZipArchive();
108106

109-
if (!$zip->open($zipFilename)) {
110-
throw new FileHandlerException('Failed to open the ZIP archive.');
107+
if (true !== $zip->open($zipFilename, $flag)) {
108+
throw new FileHandlerException('Invalid or uninitialized Zip object');
111109
}
112110

111+
113112
if (!$zip->extractTo($extractPath)) {
114113
throw new FileHandlerException('Failed to extract the ZIP archive.');
115114
}
116115

117116
$zip->close();
118117
}
119118

120-
/**
121-
* @throws FileHandlerException
122-
*/
119+
123120
public function close(): void
124121
{
125-
if (!$this->files) {
126-
throw new FileHandlerException('no files are opened');
127-
}
128122
foreach ($this->files as $file) {
129-
if (!fclose($file)) {
130-
throw new FileHandlerException('file was not closed');
131-
}
123+
fclose($file);
132124
}
133125
$this->resetFiles();
134126
}

src/FileHashChecker.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ public function verifyHash(string $filename, string $algo = self::ALGO_256): boo
6666
* @throws HashException|FileHandlerException
6767
*/
6868

69-
public function hashFile(string $filename, string $algo = self::ALGO_256): string
70-
{
69+
public function hashFile(
70+
string $filename,
71+
string $algo = self::ALGO_256,
72+
string $env = self::STORED_HASH_FILE
73+
): string {
7174
$this->validateFileName($filename);
7275
if (!in_array($algo, [self::ALGO_512, self::ALGO_256])) {
7376
throw new HashException('algorithm not supported');
@@ -77,7 +80,7 @@ public function hashFile(string $filename, string $algo = self::ALGO_256): strin
7780
throw new HashException('could not hash file');
7881
}
7982

80-
$storedHashesFile = $this->getParameter(self::STORED_HASH_FILE);
83+
$storedHashesFile = $this->getParameter($env);
8184

8285

8386
$file = fopen($storedHashesFile, 'a+');
@@ -111,7 +114,7 @@ public function hashFile(string $filename, string $algo = self::ALGO_256): strin
111114
* @param mixed $storedHashFile
112115
* @return void
113116
*/
114-
private function checkHeaderExists(mixed $storedHashFile): void
117+
public function checkHeaderExists(mixed $storedHashFile): void
115118
{
116119
$header = fgetcsv($storedHashFile);
117120

src/JsonFileHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public function getRows(
8686
array &$headers,
8787
array|false $hideColumns = false,
8888
int|false $limit = false
89-
): Generator
90-
{
89+
): Generator {
9190
$contents = $this->validateFile($filename);
9291

9392
$headers = array_keys($contents[0]);

src/TempFileHandler.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
namespace Rcsofttech85\FileHandler;
44

55
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
6+
use Rcsofttech85\FileHandler\Validator\FileValidatorTrait;
67

78
class TempFileHandler
89
{
10+
use FileValidatorTrait;
11+
12+
/**
13+
* @param string $tempFilePath
14+
* @param string $filename
15+
* @return void
16+
* @throws FileHandlerException
17+
*/
918
public function renameTempFile(string $tempFilePath, string $filename): void
1019
{
1120
if (!rename($tempFilePath, $filename)) {
@@ -39,6 +48,7 @@ public function writeRowToTempFile(string $tempFilePath, array $row): void
3948
* @param string|null $dirName
4049
* @param string|null $prefix
4150
* @return string|false
51+
* @throws FileHandlerException
4252
*/
4353
public function createTempFileWithHeaders(
4454
array $headers,
@@ -57,11 +67,10 @@ public function createTempFileWithHeaders(
5767
if (!$tempFilePath) {
5868
return false;
5969
}
60-
$tempFileHandle = fopen($tempFilePath, 'w');
61-
if ($tempFileHandle) {
62-
fputs($tempFileHandle, implode(',', $headers) . PHP_EOL);
63-
fclose($tempFileHandle);
64-
}
70+
$tempFileHandle = $this->openFileAndReturnResource($tempFilePath);
71+
72+
fputs($tempFileHandle, implode(',', $headers) . PHP_EOL);
73+
fclose($tempFileHandle);
6574

6675

6776
return $tempFilePath;

src/Validator/FileValidatorTrait.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer;
66
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
78

89
trait FileValidatorTrait
910
{
@@ -30,10 +31,6 @@ public function validateFileName(string $filename, string|null $path = null): st
3031
$filename = $absolutePath . DIRECTORY_SEPARATOR . $filename;
3132
}
3233

33-
34-
if (!file_exists($filename)) {
35-
throw new FileHandlerException('file not found');
36-
}
3734
return $filename;
3835
}
3936

@@ -51,44 +48,72 @@ public function sanitize(string $filename): string
5148
}
5249

5350
/**
51+
* @param string $filename
52+
* @param string $envVariable
53+
* @return bool
5454
* @throws FileHandlerException
5555
*/
5656
private function isFileSafe(string $filename, string $envVariable): bool
5757
{
5858
$safeFile = $this->getParameter($envVariable);
5959

60+
6061
if ($safeFile !== $filename) {
6162
return false;
6263
}
63-
if (!file_exists($safeFile)) {
64-
throw new FileHandlerException('env variable does not contain a valid file path');
65-
}
66-
6764

6865
return true;
6966
}
7067

7168
/**
72-
* @throws FileHandlerException
69+
* @param string $filename
70+
* @param string $envVariable
71+
* @return bool
7372
*/
7473
public function isFileRestricted(string $filename, string $envVariable): bool
7574
{
7675
return $this->isFileSafe($filename, $envVariable);
7776
}
7877

7978
/**
79+
* @param string $param
80+
* @return string
8081
* @throws FileHandlerException
8182
*/
8283
private function getParameter(string $param): string
8384
{
8485
$container = (new ServiceContainer())->getContainerBuilder();
86+
return $this->getParam($container, $param);
87+
}
8588

86-
$parameter = $container->getParameter($param);
87-
88-
if (!is_string($parameter)) {
89-
throw new FileHandlerException("{$param} is expected to be string");
89+
/**
90+
* @param ContainerBuilder $container
91+
* @param string $parameter
92+
* @return string
93+
* @throws FileHandlerException
94+
*/
95+
public function getParam(ContainerBuilder $container, string $parameter): string
96+
{
97+
$param = $container->getParameter($parameter);
98+
if (!is_string($param)) {
99+
throw new FileHandlerException("{$parameter} is not string type");
90100
}
91101

92-
return $parameter;
102+
return $param;
103+
}
104+
105+
/**
106+
* @param string $filename
107+
* @param string $mode
108+
* @return mixed
109+
* @throws FileHandlerException
110+
*/
111+
public function openFileAndReturnResource(string $filename, string $mode = 'w'): mixed
112+
{
113+
$file = fopen($filename, $mode);
114+
if (!$file) {
115+
throw new FileHandlerException('file is not valid');
116+
}
117+
return $file;
93118
}
94119
}

tests/Integration/StreamHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StreamHandlerTest extends BaseTest
1818
public static function setUpBeforeClass(): void
1919
{
2020
parent::setUpBeforeClass();
21-
static::$files = ["output.html", "output1.html", "output2.html"];
21+
static::$files = ["output.html", "output1.html", "output2.html", "profile"];
2222
}
2323

2424
public static function tearDownAfterClass(): void

0 commit comments

Comments
 (0)