Repository navigation
Expand file tree
/
Copy pathCommonSingleServiceTest.php
More file actions
96 lines (82 loc) · 2.26 KB
/
Copy pathCommonSingleServiceTest.php
File metadata and controls
96 lines (82 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
use CodeIgniter\Config\Services;
use CodeIgniter\Test\CIUnitTestCase;
final class CommonSingleServiceTest extends CIUnitTestCase
{
/**
* @dataProvider serviceNamesProvider
*
* @param string $service
*
* @return void
*/
public function testSingleServiceWithNoParamsSupplied(string $service): void
{
$service1 = single_service($service);
$service2 = single_service($service);
$this->assertSame(get_class($service1), get_class($service2));
$this->assertNotSame($service1, $service2);
}
/**
* @dataProvider serviceNamesProvider
*
* @param string $service
*
* @return void
*/
public function testSingleServiceWithAtLeastOneParamSupplied(string $service): void
{
$params = [];
$method = new ReflectionMethod(Services::class, $service);
if ($method->getNumberOfParameters() === 1)
{
$params[] = true;
}
else
{
$params[] = $method->getParameters()[0]->getDefaultValue();
}
$service1 = single_service($service, ...$params);
$service2 = single_service($service, ...$params);
$this->assertSame(get_class($service1), get_class($service2));
$this->assertNotSame($service1, $service2);
}
public function testSingleServiceWithAllParamsSupplied(): void
{
$cache1 = single_service('cache', null, true);
$cache2 = single_service('cache', null, true);
// Assert that even passing true as last param this will
// not create a shared instance.
$this->assertSame(get_class($cache1), get_class($cache2));
$this->assertNotSame($cache1, $cache2);
}
public function testSingleServiceWithGibberishGiven(): void
{
$this->assertNull(single_service('foo'));
$this->assertNull(single_service('bar'));
$this->assertNull(single_service('baz'));
$this->assertNull(single_service('caches'));
$this->assertNull(single_service('timers'));
}
public static function serviceNamesProvider(): iterable
{
$methods = (new ReflectionClass(Services::class))->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method)
{
$name = $method->getName();
$excl = [
'__callStatic',
'serviceExists',
'reset',
'injectMock',
'encrypter', // Encrypter needs a starter key
'session', // Headers already sent
];
if (in_array($name, $excl, true))
{
continue;
}
yield [$name];
}
}
}