summaryrefslogtreecommitdiff
path: root/tests/Unit/Container/ContainerTest.php
blob: f0ba24e74fe7415e4d06b22c75ed7d80090e5b51 (plain)
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
97
98
<?php

namespace Engelsystem\Test\Config;

use Engelsystem\Container\Container;
use PHPUnit\Framework\TestCase;

class ContainerTest extends TestCase
{
    /**
     * @covers \Engelsystem\Container\Container::get
     */
    public function testGet()
    {
        $container = new Container();
        $class = new class
        {
        };

        $container->instance('foo', $class);
        $this->assertSame($class, $container->get('foo'));
    }

    /**
     * @covers \Engelsystem\Container\Container::get
     * @expectedException \Engelsystem\Container\NotFoundException
     */
    public function testGetException()
    {
        $container = new Container();

        $container->get('not.registered.service');
    }

    /**
     * @covers \Engelsystem\Container\Container::instance
     * @covers \Engelsystem\Container\Container::resolve
     */
    public function testInstance()
    {
        $container = new Container();
        $class = new class
        {
        };

        $container->instance('foo', $class);
        $this->assertSame($class, $container->get('foo'));
    }

    /**
     * @covers \Engelsystem\Container\Container::has
     */
    public function testHas()
    {
        $container = new Container();

        $this->assertFalse($container->has('test'));

        $class = new class
        {
        };

        $container->instance('test', $class);
        $this->assertTrue($container->has('test'));
    }

    /**
     * @covers \Engelsystem\Container\Container::singleton
     */
    public function testSingleton()
    {
        $container = new Container();
        $class = new class
        {
        };

        $container->singleton('foo', $class);
        $this->assertSame($class, $container->get('foo'));
        $this->assertSame($class, $container->get('foo'));
    }

    /**
     * @covers \Engelsystem\Container\Container::setInstance
     * @covers \Engelsystem\Container\Container::getInstance
     */
    public function testContainerSingleton()
    {
        $container0 = new Container();
        $container = Container::getInstance();

        $this->assertNotSame($container0, $container);

        $container1 = new Container;
        Container::setInstance($container1);

        $this->assertSame($container1, Container::getInstance());
    }
}