From e000f2b359efb316caf4432f4e2ad7c1572b18e7 Mon Sep 17 00:00:00 2001 From: netkas Date: Sun, 29 Sep 2024 21:21:48 -0400 Subject: [PATCH] Add PHPUnit tests for TempFile class --- .idea/php.xml | 5 +++ bootstrap.php | 3 ++ phpunit.xml | 11 ++++++ tests/TempFile/TempFileTest.php | 62 +++++++++++++++++++++++++++++++++ tests/file_test.php | 17 --------- 5 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 bootstrap.php create mode 100644 phpunit.xml create mode 100644 tests/TempFile/TempFileTest.php delete mode 100644 tests/file_test.php diff --git a/.idea/php.xml b/.idea/php.xml index 3cf5373..c63a823 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -19,6 +19,11 @@ + + + + + diff --git a/bootstrap.php b/bootstrap.php new file mode 100644 index 0000000..3e8e4ab --- /dev/null +++ b/bootstrap.php @@ -0,0 +1,3 @@ + + + + tests + + + + + + + diff --git a/tests/TempFile/TempFileTest.php b/tests/TempFile/TempFileTest.php new file mode 100644 index 0000000..b5b5d38 --- /dev/null +++ b/tests/TempFile/TempFileTest.php @@ -0,0 +1,62 @@ +assertTrue(is_file($tempFile->getFilepath())); + $this->assertStringEndsWith('.tmp', $tempFile->getFilename()); + + // Test with custom options. + $customOptions = [ + Options::Extension => 'txt', + Options::Filename => 'testfile', + Options::Prefix => 'prefix_', + Options::Suffix => '_suffix', + Options::RandomLength => 5, + Options::Directory => sys_get_temp_dir(), + ]; + $tempFile = new TempFile($customOptions); + $this->assertStringEndsWith('.txt', $tempFile->getFilename()); + $this->assertStringStartsWith('prefix_', $tempFile->getFilename()); + $this->assertStringEndsWith('_suffix.txt', $tempFile->getFilename()); + $this->assertSame($customOptions[Options::Directory], dirname($tempFile->getFilepath())); + + // Test when a non-string and non-integer value is given to any options. + $customOptions[Options::Prefix] = []; + $this->expectException(\InvalidArgumentException::class); + new TempFile($customOptions); + + // Test when an invalid option is given. + $customOptions = ['invalid_option' => 'value']; + $this->expectException(\InvalidArgumentException::class); + new TempFile($customOptions); + + // Test when a directory that does not exist is given. + $customOptions = [Options::Directory => '/nonexistent/directory']; + $this->expectException(\InvalidArgumentException::class); + new TempFile($customOptions); + + // Test when a directory that is not writable is given. + $customOptions = [Options::Directory => '/']; + $this->expectException(\InvalidArgumentException::class); + new TempFile($customOptions); + + // Test if is writeable + $customOptions = [Options::Directory => sys_get_temp_dir()]; + $tempFile = new TempFile($customOptions); + $this->assertTrue(is_writable($tempFile->getFilepath())); + } +} \ No newline at end of file diff --git a/tests/file_test.php b/tests/file_test.php deleted file mode 100644 index 4f24699..0000000 --- a/tests/file_test.php +++ /dev/null @@ -1,17 +0,0 @@ - 'txt', - \TempFile\Options::Filename => 'test', - ]); - print(sprintf('Tempfile: %s', $temp->getFilepath()) . PHP_EOL); - - file_put_contents($temp, 'Hello, world!'); - print(sprintf('Filesize: %s', filesize($temp->getFilepath())) . PHP_EOL); - - sleep(10); - print('Exiting...' . PHP_EOL); - exit(0); \ No newline at end of file