Skip to content

Commit b5a9819

Browse files
committed
auto file creation and delete for testing purpose.
1 parent b4056b1 commit b5a9819

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/FileHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class FileHandler
1313

1414
public function open(
1515
string $filename,
16-
string $mode = 'w',
16+
string $mode = "r+",
1717
bool $include_path = false,
1818
$context = null
19-
): mixed {
19+
): self {
2020
$file = fopen($filename, $mode, $include_path, $context);
2121

2222
if (!$file) {
@@ -25,7 +25,7 @@ public function open(
2525

2626
$this->files[] = $file;
2727

28-
return $file;
28+
return $this;
2929
}
3030

3131

tests/FileHandlerTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,28 @@ protected function setUp(): void
1717
parent::setUp();
1818

1919
$this->fileHandler = new FileHandler();
20+
fopen(filename: "file", mode: "w");
21+
2022
}
2123

2224
protected function tearDown(): void
2325
{
2426
parent::tearDown();
2527

2628
$this->fileHandler = null;
29+
unlink(filename: "file");
2730
}
2831

2932

3033
#[Test]
3134
#[TestDox("file was written successfully!")]
3235
public function file_successfully_written()
3336
{
34-
$this->fileHandler->open(filename: 'file.txt');
37+
$this->fileHandler->open(filename: 'file');
3538

3639
$this->fileHandler->write(data: "hello world");
3740

38-
$this->assertEquals(expected: "hello world", actual: file_get_contents(filename: 'file.txt'));
41+
$this->assertEquals(expected: "hello world", actual: file_get_contents(filename: 'file'));
3942
}
4043

4144
#[Test]
@@ -44,15 +47,14 @@ public function should_throw_exception_if_file_is_not_Found()
4447
{
4548
$this->expectException(FileNotFoundException::class);
4649
$this->expectExceptionMessage('File not found');
47-
$this->fileHandler->open(filename: 'unknown', mode: 'r');
50+
$this->fileHandler->open(filename: 'unknown');
4851
}
4952

5053
#[Test]
5154
#[TestDox("should throw an exception if file is not writable")]
5255
public function should_throw_exception_if_file_is_not_writable()
5356
{
54-
55-
$this->fileHandler->open(filename: 'file', mode: 'r');
57+
$this->fileHandler->open(filename: 'file',mode: 'r');
5658

5759
$this->expectException(CouldNotWriteFileException::class);
5860
$this->expectExceptionMessage('Error writing to file');
@@ -65,13 +67,15 @@ public function multiple_file_can_be_written_simultaneously()
6567
{
6668
$this->fileHandler->open(filename: 'file');
6769

68-
$this->fileHandler->open(filename: 'file1');
70+
$this->fileHandler->open(filename: 'file1',mode: 'w');
6971

7072
$this->fileHandler->write(data: "hello world");
7173

7274
$this->assertEquals("hello world", file_get_contents(filename: 'file'));
7375

7476
$this->assertEquals("hello world", file_get_contents(filename: 'file1'));
77+
78+
unlink("file1");
7579
}
7680

7781

@@ -85,11 +89,6 @@ public function file_is_closed_properly()
8589

8690
$this->expectException(TypeError::class);
8791
$this->fileHandler->write(data: "fwrite(): supplied resource is not a valid stream resource");
88-
89-
90-
91-
9292
}
9393

94-
9594
}

0 commit comments

Comments
 (0)