Skip to content

Commit de7e481

Browse files
committed
refactor: cleaning up test class
Signed-off-by: rahul <[email protected]>
1 parent 8e294e8 commit de7e481

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/FileHandler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ public function toJson(): string
8383
return json_encode($data);
8484
}
8585

86-
public function delete()
86+
/**
87+
* @throws FileHandlerException
88+
*/
89+
public function delete(string $filename): void
8790
{
88-
foreach ($this->files as $file) {
89-
if (!unlink($file)) {
90-
throw new FileHandlerException('could not delete file');
91-
}
91+
if (!file_exists($filename)) {
92+
throw new FileHandlerException('file does not exists');
9293
}
94+
unlink($filename);
9395
}
9496

9597
/**
@@ -120,7 +122,6 @@ private function getRows(): Generator
120122
}
121123
}
122124

123-
124125
/**
125126
* @throws FileHandlerException
126127
*/

tests/unit/FileHandlerTest.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,40 @@ class FileHandlerTest extends TestCase
1717
protected function setUp(): void
1818
{
1919
parent::setUp();
20-
2120
$this->fileHandler = new FileHandler();
22-
fopen(filename: "file", mode: "w");
21+
}
2322

23+
public static function setUpBeforeClass(): void
24+
{
25+
parent::setUpBeforeClass();
2426
$content = "Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year\n"
2527
. "Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008\n"
2628
. "Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010\n"
2729
. "Twilight,Romance,Independent,68,6.383363636,26,$702.17 ,2011";
2830

31+
fopen(filename: "file", mode: "w");
32+
fopen(filename: "file1", mode: "w");
2933
file_put_contents('movie.csv', $content);
3034
}
3135

36+
public static function tearDownAfterClass(): void
37+
{
38+
parent::tearDownAfterClass();
39+
40+
$files = ["file", "movie.csv", 'file1'];
41+
42+
foreach ($files as $file) {
43+
if (file_exists($file)) {
44+
unlink(filename: $file);
45+
}
46+
}
47+
}
48+
3249
protected function tearDown(): void
3350
{
3451
parent::tearDown();
3552

3653
$this->fileHandler = null;
37-
unlink(filename: "file");
38-
unlink(filename: "movie.csv");
3954
}
4055

4156

@@ -67,34 +82,34 @@ public function shouldThrowExceptionIfFileIsNotWritable()
6782
$this->fileHandler->write(data: "hello world");
6883
}
6984

85+
7086
#[Test]
71-
public function multipleFileCanBeWrittenSimultaneously()
87+
public function fileIsClosedProperly()
7288
{
7389
$this->fileHandler->open(filename: 'file');
74-
75-
$this->fileHandler->open(filename: 'file1', mode: 'w');
76-
7790
$this->fileHandler->write(data: "hello world");
91+
$this->fileHandler->close();
7892

79-
$this->assertEquals("hello world", file_get_contents(filename: 'file'));
80-
81-
$this->assertEquals("hello world", file_get_contents(filename: 'file1'));
82-
83-
unlink("file1");
93+
$this->expectException(TypeError::class);
94+
$this->fileHandler->write(data: "fwrite(): supplied resource is not a valid stream resource");
8495
}
8596

8697

8798
#[Test]
88-
public function fileIsClosedProperly()
99+
public function multipleFileCanBeWrittenSimultaneously()
89100
{
90101
$this->fileHandler->open(filename: 'file');
102+
103+
$this->fileHandler->open(filename: 'file1');
104+
91105
$this->fileHandler->write(data: "hello world");
92-
$this->fileHandler->close();
93106

94-
$this->expectException(TypeError::class);
95-
$this->fileHandler->write(data: "fwrite(): supplied resource is not a valid stream resource");
107+
$this->assertEquals("hello world", file_get_contents(filename: 'file'));
108+
109+
$this->assertEquals("hello world", file_get_contents(filename: 'file1'));
96110
}
97111

112+
98113
#[Test]
99114
#[DataProvider('provideMovieNames')]
100115
#[TestDox('search result with name $keyword exists in file.')]

0 commit comments

Comments
 (0)