Skip to content

Commit 77c53ba

Browse files
committed
Enhance BaseServices testing
1 parent 38bc9f5 commit 77c53ba

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

system/Config/BaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static function __callStatic(string $name, array $arguments)
184184
{
185185
$name = strtolower($name);
186186

187-
if (method_exists(__CLASS__, $name))
187+
if (method_exists(Services::class, $name))
188188
{
189189
return Services::$name(...$arguments);
190190
}

tests/system/Config/ServicesTest.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php namespace Config;
1+
<?php
2+
namespace Config;
3+
4+
use Tests\Support\HTTP\MockResponse;
25

36
class ServicesTest extends \CIUnitTestCase
47
{
@@ -227,4 +230,51 @@ public function testCallStatic()
227230
$this->assertInstanceOf(\CodeIgniter\Session\Session::class, $actual);
228231
}
229232

233+
/**
234+
* @runInSeparateProcess
235+
* @preserveGlobalState disabled
236+
*/
237+
public function testCallStaticDirectly()
238+
{
239+
// $actual = \CodeIgniter\Config\Services::SeSsIoN(null, false); // original
240+
$actual = \CodeIgniter\Config\Services::__callStatic('SeSsIoN', [null, false]);
241+
$this->assertInstanceOf(\CodeIgniter\Session\Session::class, $actual);
242+
}
243+
244+
/**
245+
* @runInSeparateProcess
246+
* @preserveGlobalState disabled
247+
*/
248+
public function testMockInjection()
249+
{
250+
Services::injectMock('response', new MockResponse(new App()));
251+
$response = service('response');
252+
$this->assertInstanceOf(MockResponse::class, $response);
253+
254+
Services::injectMock('response', new MockResponse(new App()));
255+
$response2 = service('response');
256+
$this->assertInstanceOf(MockResponse::class, $response2);
257+
258+
$this->assertEquals($response, $response2);
259+
}
260+
261+
/**
262+
* @runInSeparateProcess
263+
* @preserveGlobalState disabled
264+
*/
265+
public function testReset()
266+
{
267+
Services::injectMock('response', new MockResponse(new App()));
268+
$response = service('response');
269+
$this->assertInstanceOf(MockResponse::class, $response);
270+
271+
Services::reset(true); // reset mocks & shared instances
272+
273+
Services::injectMock('response', new MockResponse(new App()));
274+
$response2 = service('response');
275+
$this->assertInstanceOf(MockResponse::class, $response2);
276+
277+
$this->assertTrue($response !== $response2);
278+
}
279+
230280
}

0 commit comments

Comments
 (0)