Completed
Pull Request — dev (#11)
by
unknown
05:12 queued 01:17
created

DiTest::testSetAndGetService()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 3
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 PHPUnit_Framework_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