Skip to content

Commit c4156bd

Browse files
authored
Report (#49)
test coverage Signed-off-by: rahul <[email protected]>
1 parent 30b1e2e commit c4156bd

File tree

8 files changed

+270
-76
lines changed

8 files changed

+270
-76
lines changed

phpunit.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" colors="true"
4-
bootstrap="tests/bootstrap.php" cacheDirectory=".phpunit.cache">
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
cacheDirectory=".phpunit.cache"
6+
colors="true">
57
<php>
68
<ini name="display_errors" value="1"/>
79
<ini name="error_reporting" value="1"/>
@@ -13,5 +15,9 @@
1315

1416
</testsuite>
1517
</testsuites>
16-
<coverage/>
18+
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
1723
</phpunit>

src/JsonFileHandler.php

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,27 @@ public function toArray(
3838
private function validateFile(string $filename): array
3939
{
4040
$filename = $this->validateFileName($filename);
41-
$jsonContents = $this->getFileContents($filename);
42-
$contents = $this->parseJson($jsonContents);
43-
if (!$contents) {
44-
throw new FileHandlerException('could not parse json');
45-
}
46-
$this->validateJsonData($contents);
47-
return $contents;
41+
return $this->getValidJsonData($filename);
4842
}
4943

5044

5145
/**
5246
* @param string $filename
53-
* @return string
47+
* @return array<int,array<string,string>>
5448
* @throws FileHandlerException
5549
*/
56-
private function getFileContents(string $filename): string
50+
public function getValidJsonData(string $filename): array
5751
{
5852
$jsonContents = file_get_contents($filename);
5953
if (!$jsonContents) {
6054
throw new FileHandlerException("{$filename} is not valid");
6155
}
62-
return $jsonContents;
63-
}
6456

65-
/**
66-
* @param string $jsonData
67-
* @return array<int,array<string,string>>|false
68-
*/
69-
private function parseJson(string $jsonData): array|false
70-
{
71-
$data = json_decode($jsonData, true);
72-
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) {
73-
return false;
57+
$data = json_decode($jsonContents, true);
58+
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data[0])) {
59+
throw new FileHandlerException('could not decode json');
7460
}
75-
return $data;
76-
}
7761

78-
/**
79-
* @param array<int,array<string,string>>|false $data
80-
* @return void
81-
* @throws FileHandlerException
82-
*/
83-
private function validateJsonData(array|false $data): void
84-
{
85-
if (empty($data) || !is_array($data[0])) {
86-
throw new FileHandlerException(json_last_error_msg());
87-
}
8862

8963
$firstArrayKeys = array_keys($data[0]);
9064

@@ -95,16 +69,30 @@ private function validateJsonData(array|false $data): void
9569
throw new FileHandlerException('Inconsistent JSON data');
9670
}
9771
}
72+
73+
return $data;
9874
}
9975

10076
/**
101-
* @param array<int,array<string,string>> $contents
102-
* @param array<int<0,max>,int> $indices
77+
* @param string $filename
78+
* @param array<int,string> $headers
79+
* @param array<string>|false $hideColumns
10380
* @param int|false $limit
10481
* @return Generator
82+
* @throws FileHandlerException
10583
*/
106-
private function getProcessedContent(array $contents, array $indices, int|false $limit = false): Generator
84+
public function getRows(
85+
string $filename,
86+
array &$headers,
87+
array|false $hideColumns = false,
88+
int|false $limit = false
89+
): Generator
10790
{
91+
$contents = $this->validateFile($filename);
92+
93+
$headers = array_keys($contents[0]);
94+
$indices = is_array($hideColumns) ? $this->setColumnsToHide($headers, $hideColumns) : [];
95+
10896
$count = 0;
10997
$shouldLimit = is_int($limit);
11098

@@ -122,26 +110,4 @@ private function getProcessedContent(array $contents, array $indices, int|false
122110
}
123111
}
124112
}
125-
126-
/**
127-
* @param string $filename
128-
* @param array<string> $headers
129-
* @param array<string,string>|false $hideColumns
130-
* @param int|false $limit
131-
* @return Generator
132-
* @throws FileHandlerException
133-
*/
134-
public function getRows(
135-
string $filename,
136-
array &$headers,
137-
array|false $hideColumns = false,
138-
int|false $limit = false
139-
): Generator {
140-
$contents = $this->validateFile($filename);
141-
142-
$headers = array_keys($contents[0]);
143-
$indices = is_array($hideColumns) ? $this->setColumnsToHide($headers, $hideColumns) : [];
144-
145-
return $this->getProcessedContent($contents, $indices, $limit);
146-
}
147113
}

src/StreamHandler.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ private function stream(string $streamUrl, string $outputFilename): Fiber
4949

5050
Fiber::suspend();
5151
}
52-
} catch (Throwable $e) {
53-
throw new StreamException();
5452
} finally {
5553
fclose($stream);
5654
fclose($outputFile);
5755
}
5856
});
5957
}
6058

61-
/**
62-
*/
6359
public function initiateConcurrentStreams(): self
6460
{
6561
foreach ($this->streamUrls as $outputFile => $streamUrl) {
@@ -72,6 +68,7 @@ public function initiateConcurrentStreams(): self
7268
}
7369

7470
/**
71+
* @return $this
7572
* @throws StreamException
7673
* @throws Throwable
7774
*/
@@ -81,7 +78,6 @@ public function start(): self
8178
throw new StreamException("No fibers available to start");
8279
}
8380

84-
/** @var Fiber $fiber */
8581
foreach ($this->fibers as $fiber) {
8682
$fiber->start();
8783
}
@@ -98,7 +94,6 @@ public function resume(bool $resumeOnce = false): void
9894
throw new StreamException("No fibers are currently running");
9995
}
10096

101-
/** @var Fiber $fiber */
10297
foreach ($this->fibers as $fiber) {
10398
while (!$fiber->isTerminated()) {
10499
$fiber->resume();
@@ -110,4 +105,9 @@ public function resume(bool $resumeOnce = false): void
110105

111106
$this->fibers = [];
112107
}
108+
109+
public function resetFibers(): void
110+
{
111+
$this->fibers = [];
112+
}
113113
}

src/TempFileHandler.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ public function writeRowToTempFile(string $tempFilePath, array $row): void
3636

3737
/**
3838
* @param array<string> $headers
39-
* @return string
39+
* @param string|null $dirName
40+
* @param string|null $prefix
41+
* @return string|false
4042
*/
41-
public function createTempFileWithHeaders(array $headers): string|false
42-
{
43-
$tempFilePath = tempnam(sys_get_temp_dir(), 'tempfile_');
43+
public function createTempFileWithHeaders(
44+
array $headers,
45+
string|null $dirName = null,
46+
string|null $prefix = null
47+
): string|false {
48+
if (null === $dirName) {
49+
$dirName = sys_get_temp_dir();
50+
}
51+
if (null === $prefix) {
52+
$prefix = 'tempfile_';
53+
}
54+
55+
$tempFilePath = $this->getTempName($dirName, $prefix);
56+
4457
if (!$tempFilePath) {
4558
return false;
4659
}
@@ -53,4 +66,12 @@ public function createTempFileWithHeaders(array $headers): string|false
5366

5467
return $tempFilePath;
5568
}
69+
70+
public function getTempName(string $directory, string $prefix): string|false
71+
{
72+
if (!is_dir($directory)) {
73+
return false;
74+
}
75+
return tempnam($directory, $prefix);
76+
}
5677
}

tests/Integration/StreamHandlerTest.php

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#[Group("integration")]
1414
class StreamHandlerTest extends BaseTest
1515
{
16+
private StreamHandler|null $streamHandler = null;
17+
1618
public static function setUpBeforeClass(): void
1719
{
1820
parent::setUpBeforeClass();
@@ -24,6 +26,36 @@ public static function tearDownAfterClass(): void
2426
parent::tearDownAfterClass();
2527
}
2628

29+
protected function tearDown(): void
30+
{
31+
parent::tearDown();
32+
$this->streamHandler = null;
33+
}
34+
35+
/**
36+
* @return void
37+
* @throws StreamException
38+
* @throws Throwable
39+
*/
40+
#[Test]
41+
public function resumeAtOnceWorkingProperly(): void
42+
{
43+
$data = "<html><body>hello world</body></html>";
44+
file_put_contents("profile", $data);
45+
$this->streamHandler = new StreamHandler([
46+
"output.html" => "profile"
47+
48+
]);
49+
50+
$this->streamHandler->initiateConcurrentStreams()->start()->resume(resumeOnce: true);
51+
$content = file_get_contents('output.html');
52+
if (!$content) {
53+
$this->fail('file has no content');
54+
}
55+
56+
$this->assertStringContainsString($data, $content);
57+
}
58+
2759
/**
2860
* @param array<string,string> $urls
2961
* @return void
@@ -34,8 +66,8 @@ public static function tearDownAfterClass(): void
3466
#[DataProvider('streamDataProvider')]
3567
public function streamAndWriteToFile(array $urls): void
3668
{
37-
$stream = new StreamHandler($urls);
38-
$stream->initiateConcurrentStreams()->start()->resume();
69+
$this->streamHandler = new StreamHandler($urls);
70+
$this->streamHandler->initiateConcurrentStreams()->start()->resume();
3971

4072
$files = array_keys($urls);
4173
foreach ($files as $file) {
@@ -46,24 +78,77 @@ public function streamAndWriteToFile(array $urls): void
4678
}
4779
}
4880

81+
/**
82+
* @param string $outputFile
83+
* @param string $url
84+
* @return void
85+
* @throws StreamException
86+
* @throws Throwable
87+
*/
4988

5089
#[Test]
5190
#[DataProvider('wrongStreamDataProvider')]
5291
public function throwExceptionIfUrlIsInvalid(string $outputFile, string $url): void
5392
{
54-
$stream = new StreamHandler([$outputFile => $url]);
93+
$this->streamHandler = new StreamHandler([$outputFile => $url]);
94+
95+
96+
$this->expectException(StreamException::class);
97+
$this->streamHandler->initiateConcurrentStreams()->start()->resume();
98+
}
99+
100+
/**
101+
* @return void
102+
* @throws StreamException
103+
* @throws Throwable
104+
*/
105+
106+
#[Test]
107+
public function throwExceptionIfNoFibersAreAvailable(): void
108+
{
109+
$this->streamHandler = new StreamHandler([
110+
"output.html" =>
111+
"https://gist.github.com/rcsofttech85/629b37d483c4796db7bdcb3704067631#file-gistfile1-txt",
112+
113+
]);
114+
115+
$this->expectException(StreamException::class);
116+
$this->expectExceptionMessage('No fibers available to start');
117+
$this->streamHandler->resetFibers();
118+
$this->streamHandler->start();
119+
}
120+
121+
122+
/**
123+
* @return void
124+
* @throws StreamException
125+
* @throws Throwable
126+
*/
127+
#[Test]
128+
public function throwExceptionIfNoFibersAreAvailableToResume(): void
129+
{
130+
$this->streamHandler = new StreamHandler([
131+
"output.html" =>
132+
"https://gist.github.com/rcsofttech85/629b37d483c4796db7bdcb3704067631#file-gistfile1-txt",
55133

134+
]);
56135

57136
$this->expectException(StreamException::class);
58-
$stream->initiateConcurrentStreams()->start()->resume();
137+
$this->expectExceptionMessage('No fibers are currently running');
138+
$this->streamHandler->resetFibers();
139+
$this->streamHandler->resume();
59140
}
60141

142+
/**
143+
* @return void
144+
* @throws StreamException
145+
*/
61146
#[Test]
62147
public function throwExceptionIfEmptyDataProvided(): void
63148
{
64149
$this->expectException(StreamException::class);
65150
$this->expectExceptionMessage('No stream URLs provided.');
66-
new StreamHandler([]);
151+
$this->streamHandler = new StreamHandler([]);
67152
}
68153

69154
/**

tests/unit/FileHashCheckerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,17 @@ public function shouldAddRecordIfNewFileIsHashed(): void
111111

112112
$this->assertTrue($isVerified);
113113
}
114+
115+
/**
116+
* @return void
117+
* @throws FileHandlerException
118+
* @throws HashException
119+
*/
120+
#[Test]
121+
public function shouldThrowExceptionIfInvalidAlgoProvided(): void
122+
{
123+
$this->expectException(HashException::class);
124+
$this->expectExceptionMessage('algorithm not supported');
125+
$this->fileHash->hashFile('sample', 'invalid');
126+
}
114127
}

0 commit comments

Comments
 (0)