Skip to content

Commit ca26b2a

Browse files
committed
fixes tests
1 parent 0bdd445 commit ca26b2a

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ build/
1010
.phpunit.result.cache
1111
/examples/test.log
1212
/examples/bad/test.log
13+
test.log
1314
CLAUDE.md
1415
/cache/
1516
/.claude/

tests/Mvc/ApplicationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,35 @@ protected function setUp() : void
2525
{
2626
parent::setUp();
2727

28+
// Clear any previous output buffers to ensure clean state
29+
while( ob_get_level() > 0 )
30+
{
31+
ob_end_clean();
32+
}
33+
34+
// Reset Registry singleton state to avoid pollution between tests
35+
$registry = Registry::getInstance();
36+
$registry->reset();
37+
2838
$Ini = new Yaml( './examples/config/neuron.yaml' );
2939
$this->App = new Application( "1.0.0", $Ini );
3040
}
3141

42+
protected function tearDown() : void
43+
{
44+
// Clean up $_SERVER state
45+
unset( $_SERVER['HTTP_CONTENT_TYPE'] );
46+
unset( $_SERVER['HTTP_AUTHORIZATION'] );
47+
48+
// Clean up any leftover output buffers that tests may have opened
49+
while( ob_get_level() > 0 )
50+
{
51+
ob_end_clean();
52+
}
53+
54+
parent::tearDown();
55+
}
56+
3257
/**
3358
* @throws \Exception
3459
*/
@@ -375,6 +400,10 @@ public function testRoutesPath()
375400
public function testBadRoutes()
376401
{
377402
$this->expectException( \Neuron\Core\Exceptions\Validation::class );
403+
404+
// Clear Registry to ensure clean state for this test
405+
Registry::getInstance()->reset();
406+
378407
$Ini = new Yaml( './examples/bad/neuron.yaml' );
379408
$App = new Application( "1.0.0", $Ini );
380409
}

tests/Mvc/Cache/Storage/RedisCacheStorageUnitTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,16 @@ public function testCustomPrefixIsUsed()
211211
public function testIsExpiredReturnsTrueWhenTtlIsNegative()
212212
{
213213
$MockRedis = $this->createMock( Redis::class );
214+
215+
// First exists() is called, return false to indicate key doesn't exist
214216
$MockRedis->expects( $this->once() )
215-
->method( 'ttl' )
217+
->method( 'exists' )
216218
->with( 'neuron_cache_test_key' )
217-
->willReturn( -2 ); // Key doesn't exist
219+
->willReturn( 0 ); // Redis exists returns 0 for non-existent keys
220+
221+
// ttl() should not be called since exists() returns false
222+
$MockRedis->expects( $this->never() )
223+
->method( 'ttl' );
218224

219225
$Storage = $this->createStorageWithMock( $MockRedis );
220226

@@ -225,6 +231,14 @@ public function testIsExpiredReturnsTrueWhenTtlIsNegative()
225231
public function testIsExpiredReturnsFalseWhenTtlIsPositive()
226232
{
227233
$MockRedis = $this->createMock( Redis::class );
234+
235+
// First exists() is called, return true to indicate key exists
236+
$MockRedis->expects( $this->once() )
237+
->method( 'exists' )
238+
->with( 'neuron_cache_test_key' )
239+
->willReturn( 1 ); // Redis exists returns 1 for existing keys
240+
241+
// Then ttl() is called and returns positive value
228242
$MockRedis->expects( $this->once() )
229243
->method( 'ttl' )
230244
->with( 'neuron_cache_test_key' )

tests/Mvc/Controllers/CacheInitializationTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,27 @@ public function testInitializeViewCacheReturnsExistingInstanceFromRegistry()
101101
public function testInitializeViewCacheReturnsNullWhenCacheDisabled()
102102
{
103103
// Setup settings with cache disabled
104-
$Settings = $this->createMock( \Neuron\Data\Setting\Source\ISettingSource::class );
105-
$Settings->method( 'get' )
104+
$SettingSource = $this->createMock( \Neuron\Data\Setting\Source\ISettingSource::class );
105+
$SettingSource->method( 'get' )
106106
->willReturnMap( [
107107
['cache', 'enabled', 'false'],
108108
['cache', 'storage', 'file'],
109109
['cache', 'path', 'cache/views'],
110110
['cache', 'ttl', '3600']
111111
] );
112-
112+
113+
// Wrap the mock source in a proper SettingManager
114+
$Settings = new \Neuron\Data\Setting\SettingManager( $SettingSource );
115+
113116
$Registry = Registry::getInstance();
114117
$Registry->set( 'Settings', $Settings );
115-
118+
116119
// Create test controller
117120
$Controller = new CacheInitTestController( new Application() );
118-
121+
119122
// Test initialization returns null
120123
$ViewCache = $Controller->testInitializeViewCache();
121-
124+
122125
$this->assertNull( $ViewCache );
123126
$this->assertNull( $Registry->get( 'ViewCache' ) );
124127
}

tests/Mvc/Requests/RequestTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function testRequest()
5757

5858
public function testProcessPayloadSuccess()
5959
{
60+
// Set required headers for the request
61+
setHeaders(
62+
[
63+
'Content-Type' => 'application/json'
64+
]
65+
);
66+
6067
$Request = new Request();
6168
$Request->loadFile( 'examples/config/requests/login.yaml' );
6269

@@ -144,6 +151,13 @@ public function testProcessPayloadFail()
144151

145152
public function testRequestPayload()
146153
{
154+
// Set required headers for the request
155+
setHeaders(
156+
[
157+
'Content-Type' => 'application/json'
158+
]
159+
);
160+
147161
$Request = new Request();
148162
$Request->loadFile( 'examples/config/requests/login.yaml' );
149163

@@ -179,6 +193,13 @@ public function testRequestPayload()
179193

180194
public function testNoProperties()
181195
{
196+
// Set required headers for the request
197+
setHeaders(
198+
[
199+
'Content-Type' => 'application/json'
200+
]
201+
);
202+
182203
$Request = new Request();
183204
$Request->loadFile( 'examples/config/requests/logout.yaml' );
184205

0 commit comments

Comments
 (0)