1
1
<?php
2
2
3
- namespace Saleh7 \Zatca \Tests ;
4
-
5
3
use PHPUnit \Framework \TestCase ;
6
4
use Saleh7 \Zatca \Storage ;
7
5
use Saleh7 \Zatca \Exceptions \ZatcaStorageException ;
8
6
9
7
class StorageTest extends TestCase
10
8
{
11
- private string $ testDir = __DIR__ . '/test_storage ' ;
12
- private string $ testFile = 'test.txt ' ;
13
9
private Storage $ storage ;
14
10
15
11
/**
16
- * Set up test environment.
12
+ * Set up the environment for each test.
13
+ * This will create a temporary storage directory.
17
14
*/
18
15
protected function setUp (): void
19
16
{
20
- // Set test storage directory
21
- Storage::setBasePath ($ this ->testDir );
22
- $ this ->storage = new Storage ();
23
-
24
- // Remove test directory before each test
25
- if (is_dir ($ this ->testDir )) {
26
- $ this ->deleteDirectory ($ this ->testDir );
17
+ // Initialize storage with a temporary directory
18
+ $ this ->storage = new Storage (__DIR__ . '/test_storage ' );
19
+
20
+ // Ensure the test directory exists (create it if it doesn't)
21
+ if (!is_dir (__DIR__ . '/test_storage ' )) {
22
+ mkdir (__DIR__ . '/test_storage ' , 0777 , true );
27
23
}
28
24
}
29
25
30
26
/**
31
27
* Clean up after each test.
28
+ * This will remove the test directory and its contents.
32
29
*/
33
30
protected function tearDown (): void
34
31
{
35
- // Remove test directory after each test
36
- if (is_dir ($ this ->testDir )) {
37
- $ this ->deleteDirectory ($ this ->testDir );
38
- }
32
+ $ this ->deleteDirectory (__DIR__ . '/test_storage ' );
39
33
}
40
34
41
35
/**
42
- * Recursively delete a directory.
43
- *
44
- * @param string $dir Directory path.
36
+ * Helper function to delete a directory and its contents.
45
37
*/
46
38
private function deleteDirectory (string $ dir ): void
47
39
{
48
- if (!is_dir ($ dir )) {
49
- return ;
50
- }
51
40
$ files = array_diff (scandir ($ dir ), ['. ' , '.. ' ]);
52
41
foreach ($ files as $ file ) {
53
- $ filePath = $ dir . DIRECTORY_SEPARATOR . $ file ;
54
- is_dir ($ filePath ) ? $ this ->deleteDirectory ($ filePath ) : unlink ($ filePath );
42
+ $ path = "$ dir/ $ file " ;
43
+ // Recursively delete files and directories
44
+ is_dir ($ path ) ? $ this ->deleteDirectory ($ path ) : unlink ($ path );
55
45
}
56
- rmdir ($ dir );
46
+ rmdir ($ dir ); // Remove the now-empty directory
57
47
}
58
48
59
49
/**
60
- * Test file writing and reading .
50
+ * Test that the put() method writes a file successfully .
61
51
*/
62
- public function testPutAndGet (): void
52
+ public function testPutWritesFileSuccessfully (): void
63
53
{
64
- $ content = "Hello, PHPUnit! " ;
65
- $ this ->storage ->put ($ this ->testFile , $ content );
54
+ $ content = 'Test content ' ;
55
+ $ path = 'test_file.txt ' ;
56
+
57
+ // Try to put the content into the file
58
+ $ result = $ this ->storage ->put ($ path , $ content );
59
+ $ this ->assertTrue ($ result ); // Assert the result is true
66
60
67
- $ this ->assertFileExists ($ this ->storage ->path ($ this ->testFile ));
68
- $ this ->assertSame ($ content , $ this ->storage ->get ($ this ->testFile ));
61
+ // Check that the file was written correctly
62
+ $ this ->assertFileExists (__DIR__ . '/test_storage/ ' . $ path );
63
+ $ this ->assertStringEqualsFile (__DIR__ . '/test_storage/ ' . $ path , $ content );
69
64
}
70
65
71
66
/**
72
- * Test appending data to a file.
67
+ * Test that the append() method appends data to a file correctly .
73
68
*/
74
- public function testAppend (): void
69
+ public function testAppendAppendsToFileSuccessfully (): void
75
70
{
76
- $ this ->storage ->put ($ this ->testFile , "Line 1 " );
77
- $ this ->storage ->append ($ this ->testFile , "\nLine 2 " );
78
-
79
- $ expectedContent = "Line 1 \nLine 2 " ;
80
- $ this ->assertSame ($ expectedContent , $ this ->storage ->get ($ this ->testFile ));
71
+ $ content = 'Initial content ' ;
72
+ $ appendContent = 'Appended content ' ;
73
+ $ path = 'test_append.txt ' ;
74
+
75
+ // Write initial content to the file
76
+ $ this ->storage ->put ($ path , $ content );
77
+
78
+ // Append content to the file
79
+ $ result = $ this ->storage ->append ($ path , $ appendContent );
80
+ $ this ->assertTrue ($ result ); // Assert append was successful
81
+
82
+ // Check that the content was correctly appended
83
+ $ expectedContent = $ content . $ appendContent ;
84
+ $ this ->assertStringEqualsFile (__DIR__ . '/test_storage/ ' . $ path , $ expectedContent );
81
85
}
82
86
83
87
/**
84
- * Test file existence check .
88
+ * Test that the get() method reads a file's content correctly .
85
89
*/
86
- public function testExists (): void
90
+ public function testGetReadsFileSuccessfully (): void
87
91
{
88
- $ this ->assertFalse ($ this ->storage ->exists ($ this ->testFile ));
92
+ $ content = 'File content ' ;
93
+ $ path = 'test_get.txt ' ;
89
94
90
- $ this ->storage ->put ($ this ->testFile , "Test content " );
91
- $ this ->assertTrue ($ this ->storage ->exists ($ this ->testFile ));
92
- }
95
+ // Write content to the file
96
+ $ this ->storage ->put ($ path , $ content );
93
97
94
- /**
95
- * Test path generation.
96
- */
97
- public function testPath (): void
98
- {
99
- $ expectedPath = $ this ->testDir . DIRECTORY_SEPARATOR . $ this ->testFile ;
100
- $ this ->assertSame ($ expectedPath , $ this ->storage ->path ($ this ->testFile ));
98
+ // Retrieve the file content
99
+ $ retrievedContent = $ this ->storage ->get ($ path );
100
+ $ this ->assertEquals ($ content , $ retrievedContent ); // Assert the content is the same
101
101
}
102
102
103
103
/**
104
- * Test setting and using a global base path .
104
+ * Test that the get() method throws an exception when the file does not exist .
105
105
*/
106
- public function testSetBasePath (): void
106
+ public function testGetThrowsExceptionIfFileDoesNotExist (): void
107
107
{
108
- $ newPath = __DIR__ . '/new_storage ' ;
109
- Storage::setBasePath ($ newPath );
110
- $ storage = new Storage ();
108
+ $ this ->expectException (ZatcaStorageException::class);
109
+ $ this ->expectExceptionMessage ('File not found. ' );
111
110
112
- $ this ->assertSame ($ newPath . DIRECTORY_SEPARATOR . $ this ->testFile , $ storage ->path ($ this ->testFile ));
111
+ $ path = 'non_existent_file.txt ' ;
112
+ $ this ->storage ->get ($ path ); // Attempt to read a non-existent file
113
113
}
114
114
115
115
/**
116
- * Test exception when reading a non-existent file.
116
+ * Test that the exists() method returns true for an existing file.
117
117
*/
118
- public function testGetThrowsExceptionForNonExistentFile (): void
118
+ public function testExistsReturnsTrueForExistingFile (): void
119
119
{
120
- $ this -> expectException (ZatcaStorageException::class) ;
121
- $ this -> expectExceptionMessage ( " File not found. " ) ;
120
+ $ content = ' Some content ' ;
121
+ $ path = ' existing_file.txt ' ;
122
122
123
- $ this ->storage ->get ('non_existent.txt ' );
123
+ // Write the content to the file
124
+ $ this ->storage ->put ($ path , $ content );
125
+
126
+ // Assert that the file exists
127
+ $ this ->assertTrue ($ this ->storage ->exists ($ path ));
124
128
}
125
129
126
130
/**
127
- * Test exception when writing to a non-writable directory .
131
+ * Test that the exists() method returns false for a non-existent file .
128
132
*/
129
- public function testPutThrowsExceptionOnFailure (): void
133
+ public function testExistsReturnsFalseForNonExistingFile (): void
130
134
{
131
- $ this ->expectException (ZatcaStorageException::class);
132
- $ this ->expectExceptionMessage ("Directory exists but is not writable. " );
133
-
134
- // Ensure test directory does not exist
135
- $ unwritableDir = sys_get_temp_dir () . '/unwritable_dir ' ;
136
- if (is_dir ($ unwritableDir )) {
137
- chmod ($ unwritableDir , 0755 );
138
- rmdir ($ unwritableDir );
139
- }
140
-
141
- // Create read-only directory
142
- mkdir ($ unwritableDir , 0444 ); // Read-only
143
-
144
- try {
145
- $ storage = new Storage ($ unwritableDir );
146
- $ storage ->put ('test.txt ' , 'Should fail ' );
147
- } finally {
148
- chmod ($ unwritableDir , 0755 ); // Restore permissions before deleting
149
- rmdir ($ unwritableDir );
150
- }
135
+ $ path = 'non_existing_file.txt ' ;
136
+ $ this ->assertFalse ($ this ->storage ->exists ($ path )); // Assert the file does not exist
151
137
}
152
138
153
139
/**
154
- * Test exception when trying to create a directory in a read-only filesystem .
140
+ * Test that ensureDirectoryExists() creates the directory if it does not exist .
155
141
*/
156
- public function testEnsureDirectoryExistsThrowsExceptionOnFailure (): void
142
+ public function testEnsureDirectoryExistsCreatesDirectoryIfNotExists (): void
157
143
{
158
- $ this -> expectException (ZatcaStorageException::class) ;
159
- $ this -> expectExceptionMessage ( " Parent directory is not writable. " ) ;
144
+ $ path = ' new_directory/test_file.txt ' ;
145
+ $ content = ' Test content in a new directory ' ;
160
146
161
- // Ensure test directory does not exist
162
- $ unwritableDir = sys_get_temp_dir () . '/unwritable_dir ' ;
163
- if (is_dir ($ unwritableDir )) {
164
- chmod ($ unwritableDir , 0755 );
165
- rmdir ($ unwritableDir );
166
- }
147
+ // Write content to a file in a non-existent directory
148
+ $ result = $ this ->storage ->put ($ path , $ content );
167
149
168
- // Create read-only directory
169
- mkdir ($ unwritableDir , 0444 ); // Read-only
170
-
171
- try {
172
- $ storage = new Storage ($ unwritableDir );
173
- $ storage ->put ('subdir/test.txt ' , 'Should fail ' );
174
- } finally {
175
- chmod ($ unwritableDir , 0755 ); // Restore permissions before deleting
176
- rmdir ($ unwritableDir );
177
- }
150
+ // Assert that the file was created and written
151
+ $ this ->assertTrue ($ result );
152
+ $ this ->assertFileExists (__DIR__ . '/test_storage/ ' . $ path );
178
153
}
179
- }
154
+ }
0 commit comments