1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: lenovo |
5
|
|
|
* Date: 6/19/2018 |
6
|
|
|
* Time: 1:56 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TimSDK\Tests\Container; |
10
|
|
|
|
11
|
|
|
use Pimple\Container; |
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use TimSDK\Tests\TestCase; |
14
|
|
|
use TimSDK\Foundation\Config; |
15
|
|
|
use Pimple\ServiceProviderInterface; |
16
|
|
|
use TimSDK\Foundation\Log\LogManager; |
17
|
|
|
use TimSDK\Container\ServiceContainer; |
18
|
|
|
|
19
|
|
|
class ServiceContainerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testBasicFeatures() |
22
|
|
|
{ |
23
|
|
|
$container = new ServiceContainer(); |
24
|
|
|
$this->assertNotEmpty($container->getProviders()); |
25
|
|
|
// __set, __get, offsetGet |
26
|
|
|
$this->assertInstanceOf(Config::class, $container['config']); |
27
|
|
|
$this->assertInstanceOf(Config::class, $container->config); |
28
|
|
|
$this->assertInstanceOf(Client::class, $container['httpClient']); |
29
|
|
|
$this->assertInstanceOf(Client::class, $container->httpClient); |
30
|
|
|
$this->assertInstanceOf(Client::class, $container[Client::class]); |
31
|
|
|
$this->assertInstanceOf(LogManager::class, $container['log']); |
32
|
|
|
$this->assertInstanceOf(LogManager::class, $container->log); |
33
|
|
|
$container['foo'] = 'foo'; |
34
|
|
|
$container->bar = 'bar'; |
|
|
|
|
35
|
|
|
$this->assertSame('foo', $container['foo']); |
36
|
|
|
$this->assertSame('bar', $container['bar']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testRegisterProviders() |
40
|
|
|
{ |
41
|
|
|
$container = new DummyContainerForProviderTest(); |
42
|
|
|
$this->assertSame('foo', $container['foo']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testStaticInstance() |
46
|
|
|
{ |
47
|
|
|
$app = new ServiceContainer(); |
48
|
|
|
ServiceContainer::setInstance($app); |
49
|
|
|
$this->assertEquals($app, ServiceContainer::getInstance()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testInstanceMethod() |
53
|
|
|
{ |
54
|
|
|
$app = new ServiceContainer(); |
55
|
|
|
$app->instance('foo', 'bar'); |
56
|
|
|
$this->assertSame('bar', $app['foo']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetConfig() |
60
|
|
|
{ |
61
|
|
|
$app = new ServiceContainer([ |
62
|
|
|
'foo' => 'bar' |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$config = $app->getConfig(); |
66
|
|
|
|
67
|
|
|
$this->assertInternalType('array', $app->getConfig()); |
68
|
|
|
$this->assertSame('bar', $config['foo']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testBasePath() |
72
|
|
|
{ |
73
|
|
|
$app = new ServiceContainer(); |
74
|
|
|
|
75
|
|
|
$this->assertSame(PROJECT_ROOT, $app->basePath()); |
76
|
|
|
$this->assertSame(PROJECT_ROOT . DIRECTORY_SEPARATOR . 'Cert', $app->basePath('Cert')); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
class DummyContainerForProviderTest extends ServiceContainer |
81
|
|
|
{ |
82
|
|
|
protected $providers = [ |
83
|
|
|
FooServiceProvider::class, |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
class FooServiceProvider implements ServiceProviderInterface |
88
|
|
|
{ |
89
|
|
|
public function register(Container $pimple) |
90
|
|
|
{ |
91
|
|
|
$pimple['foo'] = function () { |
92
|
|
|
return 'foo'; |
93
|
|
|
}; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|