Completed
Push — master ( 3a7482...36f3d7 )
by Jim
08:32
created

FooServiceProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * @expectedException \InvalidArgumentException
81
     * @expectedExceptionMessage Unknow version.
82
     */
83
    public function testGetVersion()
84
    {
85
        $app = new ServiceContainer();
86
87
        $app->version();
88
    }
89
}
90
91
class DummyContainerForProviderTest extends ServiceContainer
92
{
93
    protected $providers = [
94
        FooServiceProvider::class,
95
    ];
96
}
97
98
class FooServiceProvider implements ServiceProviderInterface
99
{
100
    public function register(Container $pimple)
101
    {
102
        $pimple['foo'] = function () {
103
            return 'foo';
104
        };
105
    }
106
}
107