Skip to content

Commit e3b99a1

Browse files
committed
refactoring and updates tests.
1 parent efdf0c2 commit e3b99a1

File tree

10 files changed

+670
-192
lines changed

10 files changed

+670
-192
lines changed

src/Mvc/Controllers/Base.php

Lines changed: 128 additions & 129 deletions
Large diffs are not rendered by default.

tests/Mvc/Cache/FileCacheStorageTest.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,65 @@ public function testIsExpired()
8383

8484
public function testClear()
8585
{
86-
// Skip this test with vfsStream due to limitations with directory operations
87-
if( strpos( vfsStream::url( 'cache' ), 'vfs://' ) === 0 )
86+
// Use real filesystem for this test due to vfsStream limitations with recursive directory clearing
87+
$TempDir = sys_get_temp_dir() . '/neuron_test_file_cache_' . uniqid();
88+
mkdir( $TempDir, 0777, true );
89+
90+
try
91+
{
92+
$RealStorage = new FileCacheStorage( $TempDir );
93+
94+
$RealStorage->write( 'key1', 'content1', 3600 );
95+
$RealStorage->write( 'key2', 'content2', 3600 );
96+
$RealStorage->write( 'key3', 'content3', 3600 );
97+
98+
$this->assertTrue( $RealStorage->exists( 'key1' ) );
99+
$this->assertTrue( $RealStorage->exists( 'key2' ) );
100+
$this->assertTrue( $RealStorage->exists( 'key3' ) );
101+
102+
$this->assertTrue( $RealStorage->clear() );
103+
104+
$this->assertFalse( $RealStorage->exists( 'key1' ) );
105+
$this->assertFalse( $RealStorage->exists( 'key2' ) );
106+
$this->assertFalse( $RealStorage->exists( 'key3' ) );
107+
}
108+
finally
109+
{
110+
// Clean up temp directory
111+
if( is_dir( $TempDir ) )
112+
{
113+
$this->recursiveRemoveDirectory( $TempDir );
114+
}
115+
}
116+
}
117+
118+
/**
119+
* Helper method to recursively remove a directory
120+
*/
121+
private function recursiveRemoveDirectory( string $Dir ): void
122+
{
123+
if( !is_dir( $Dir ) )
88124
{
89-
$this->markTestSkipped( 'Clear test skipped due to vfsStream limitations' );
90125
return;
91126
}
92-
93-
$this->Storage->write( 'key1', 'content1', 3600 );
94-
$this->Storage->write( 'key2', 'content2', 3600 );
95-
$this->Storage->write( 'key3', 'content3', 3600 );
96-
97-
$this->assertTrue( $this->Storage->exists( 'key1' ) );
98-
$this->assertTrue( $this->Storage->exists( 'key2' ) );
99-
$this->assertTrue( $this->Storage->exists( 'key3' ) );
100-
101-
$this->assertTrue( $this->Storage->clear() );
102-
103-
$this->assertFalse( $this->Storage->exists( 'key1' ) );
104-
$this->assertFalse( $this->Storage->exists( 'key2' ) );
105-
$this->assertFalse( $this->Storage->exists( 'key3' ) );
127+
128+
$Items = array_diff( scandir( $Dir ), [ '.', '..' ] );
129+
130+
foreach( $Items as $Item )
131+
{
132+
$Path = $Dir . DIRECTORY_SEPARATOR . $Item;
133+
134+
if( is_dir( $Path ) )
135+
{
136+
$this->recursiveRemoveDirectory( $Path );
137+
}
138+
else
139+
{
140+
unlink( $Path );
141+
}
142+
}
143+
144+
rmdir( $Dir );
106145
}
107146

108147
public function testSubdirectoryCreation()

tests/Mvc/Cache/GarbageCollectionTest.php

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,65 @@ public function testGcHandlesEmptyCache()
156156

157157
public function testGcRemovesEmptySubdirectories()
158158
{
159-
// Note: This test may be skipped due to vfsStream limitations
160-
if( strpos( vfsStream::url( 'cache' ), 'vfs://' ) === 0 )
159+
// Use real filesystem for this test due to vfsStream limitations with subdirectory removal
160+
$TempDir = sys_get_temp_dir() . '/neuron_test_gc_' . uniqid();
161+
mkdir( $TempDir, 0777, true );
162+
163+
try
164+
{
165+
$Storage = new FileCacheStorage( $TempDir );
166+
167+
// Write and let expire
168+
$Storage->write( 'test', 'content', 1 );
169+
sleep( 2 );
170+
171+
// Run GC
172+
$Storage->gc();
173+
174+
// Check subdirectories are cleaned up
175+
$Items = scandir( $TempDir );
176+
$NonDotItems = array_filter( $Items, function( $Item ) {
177+
return $Item !== '.' && $Item !== '..';
178+
});
179+
180+
$this->assertCount( 0, $NonDotItems, 'Empty subdirectories should be removed' );
181+
}
182+
finally
183+
{
184+
// Clean up temp directory
185+
if( is_dir( $TempDir ) )
186+
{
187+
$this->recursiveRemoveDirectory( $TempDir );
188+
}
189+
}
190+
}
191+
192+
/**
193+
* Helper method to recursively remove a directory
194+
*/
195+
private function recursiveRemoveDirectory( string $Dir ): void
196+
{
197+
if( !is_dir( $Dir ) )
161198
{
162-
$this->markTestSkipped( 'Subdirectory removal test skipped due to vfsStream limitations' );
163199
return;
164200
}
165-
166-
$Storage = new FileCacheStorage( vfsStream::url( 'cache' ) );
167-
168-
// Write and let expire
169-
$Storage->write( 'test', 'content', 1 );
170-
sleep( 2 );
171-
172-
// Run GC
173-
$Storage->gc();
174-
175-
// Check subdirectories are cleaned up
176-
$Items = scandir( vfsStream::url( 'cache' ) );
177-
$NonDotItems = array_filter( $Items, function( $Item ) {
178-
return $Item !== '.' && $Item !== '..';
179-
});
180-
181-
$this->assertCount( 0, $NonDotItems, 'Empty subdirectories should be removed' );
201+
202+
$Items = array_diff( scandir( $Dir ), [ '.', '..' ] );
203+
204+
foreach( $Items as $Item )
205+
{
206+
$Path = $Dir . DIRECTORY_SEPARATOR . $Item;
207+
208+
if( is_dir( $Path ) )
209+
{
210+
$this->recursiveRemoveDirectory( $Path );
211+
}
212+
else
213+
{
214+
unlink( $Path );
215+
}
216+
}
217+
218+
rmdir( $Dir );
182219
}
183220
}

tests/Mvc/Cache/Storage/RedisCacheStorageTest.php renamed to tests/Mvc/Cache/Storage/RedisCacheStorageIntegrationTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
use PHPUnit\Framework\TestCase;
77

88
/**
9-
* Tests RedisCacheStorage implementation.
10-
* These tests will be skipped if Redis extension is not installed or Redis server is not running.
9+
* Integration tests for RedisCacheStorage implementation.
10+
* These tests require a running Redis server and will be skipped if:
11+
* - Redis extension is not installed
12+
* - Redis server is not running at 127.0.0.1:6379
13+
*
14+
* To run only integration tests: ./vendor/bin/phpunit --group integration
15+
* To exclude integration tests: ./vendor/bin/phpunit --exclude-group integration
16+
*
17+
* @group integration
1118
*/
12-
class RedisCacheStorageTest extends TestCase
19+
class RedisCacheStorageIntegrationTest extends TestCase
1320
{
1421
private ?RedisCacheStorage $Storage = null;
1522
private bool $RedisAvailable = false;

0 commit comments

Comments
 (0)