DiTest::testMissingServiceThrowsException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vectorface\SnappyRouterTests\Di;
4
5
use PHPUnit\Framework\TestCase;
6
use Vectorface\SnappyRouter\Di\Di;
7
8
class DiTest extends TestCase
9
{
10
    /**
11
     * Tests the standard set/get methods of the DI.
12
     * @dataProvider setAndGetServiceProvider
13
     */
14
    public function testSetAndGetService($key, $element, $expected)
15
    {
16
        $di = new Di();
17
        $di->set($key, $element);
18
        // check that we get back what we expect
19
        $this->assertEquals(
20
            $expected,
21
            $di->get($key, false)
22
        );
23
        // check we get the same value if we use the cache
24
        $this->assertEquals(
25
            $expected,
26
            $di->get($key, true)
27
        );
28
        // and again if we force a "no cache" hit
29
        $this->assertEquals(
30
            $expected,
31
            $di->get($key, false)
32
        );
33
        $this->assertTrue($di->hasElement($key));
34
        $this->assertEquals(
35
            array($key),
36
            $di->allRegisteredElements()
37
        );
38
    }
39
40
    /**
41
     * Data provider for the method testSetAndGetService.
42
     */
43
    public function setAndGetServiceProvider()
44
    {
45
        return array(
46
            array(
47
                'HelloWorldService',
48
                'Hello world!',
49
                'Hello world!'
50
            ),
51
            array(
52
                'HelloWorldService',
53
                function () {
54
                    return 'Hello world!';
55
                },
56
                'Hello world!'
57
            )
58
        );
59
    }
60
61
    /**
62
     * Tests the methods for getting, setting and clearing the default
63
     * service provider.
64
     */
65
    public function testGetDefaultAndSetDefault()
66
    {
67
        Di::clearDefault(); // guard condition
68
        $di = Di::getDefault(); // get a fresh default
69
        $this->assertInstanceOf('Vectorface\SnappyRouter\Di\Di', $di);
70
71
        Di::setDefault($di);
72
        $this->assertEquals($di, Di::getDefault());
73
    }
74
75
    /**
76
     * Tests the exception is thrown when we ask for a service that has not
77
     * been registered.
78
     * @expectedException \Exception
79
     * @expectedExceptionMessage No element registered for key: TestElement
80
     */
81
    public function testMissingServiceThrowsException()
82
    {
83
        $di = new Di();
84
        $di->get('TestElement');
85
    }
86
}
87