Completed
Branch master (95b3ba)
by Ryan
28:02 queued 13:03
created

ContainerTest::testSetterNotOK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2017 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Test\Unit;
12
13
use SimplePie\Container;
14
15
class ContainerTest extends AbstractTestCase
16
{
17
    public function testConstruct(): void
18
    {
19
        $container1 = new Container();
20
        $this->assertSame(0, \count($container1));
21
22
        $container2 = new Container([]);
23
        $this->assertSame(0, \count($container2));
24
25
        $container3            = new Container();
26
        $container3['testing'] = function (Container $c) {
27
            return true;
28
        };
29
        $this->assertSame(1, \count($container3));
30
    }
31
32
    public function testSetterOK(): void
33
    {
34
        $container = new Container();
35
36
        $container['testing'] = function (Container $c) {
37
            return true;
38
        };
39
40
        $this->assertSame(1, \count($container));
41
    }
42
43
    /**
44
     * @expectedException \SimplePie\Exception\ContainerException
45
     * @expectedExceptionMessage The container ID `testing` cannot be overwritten.
46
     */
47
    public function testSetterNotOK(): void
48
    {
49
        $container = new Container();
50
51
        $container['testing'] = function (Container $c) {
52
            return true;
53
        };
54
        $container['testing'] = 'This is gonna fail.';
55
    }
56
57
    public function testSetterOK2(): void
58
    {
59
        $container = new Container();
60
61
        $container['testing'] = function (Container $c) {
62
            return true;
63
        };
64
65
        unset($container['testing']);
66
67
        $container['testing'] = function (Container $c) {
68
            return true;
69
        };
70
71
        $this->assertSame(1, \count($container));
72
    }
73
74
    /**
75
     * @expectedException \SimplePie\Exception\NotFoundException
76
     * @expectedExceptionMessage The container ID `testing` does not exist.
77
     */
78
    public function testGetterNotOK(): void
79
    {
80
        $container = new Container();
81
        $get       = $container['testing'];
82
    }
83
84
    public function testGetterOK(): void
85
    {
86
        $container = new Container();
87
88
        $container['testing'] = function (Container $c) {
89
            return true;
90
        };
91
92
        $this->assertTrue($container['testing']);
93
    }
94
95
    public function testGetterOK2(): void
96
    {
97
        $container = new Container();
98
99
        $container['testing'] = function (Container $c) {
100
            return true;
101
        };
102
103
        $this->assertTrue($container->get('testing'));
104
    }
105
106
    /**
107
     * @expectedException \SimplePie\Exception\ContainerException
108
     * @expectedExceptionMessage The value `testing` MUST be a callable.
109
     */
110
    public function testGetterNotOK2(): void
111
    {
112
        $container = new Container();
113
114
        $container['testing'] = true;
115
116
        $this->assertTrue($container['testing']);
117
    }
118
119
    /**
120
     * @expectedException \SimplePie\Exception\ContainerException
121
     * @expectedExceptionMessage The value `testing` MUST be a callable.
122
     */
123
    public function testGetterNotOK3(): void
124
    {
125
        $container = new Container();
126
127
        $container['testing'] = 'testing';
128
129
        $this->assertTrue($container['testing']);
130
    }
131
132
    public function testHasOK(): void
133
    {
134
        $container = new Container();
135
136
        $container['testing'] = function (Container $c) {
137
            return true;
138
        };
139
140
        $this->assertTrue($container->has('testing'));
141
    }
142
143
    public function testIterator(): void
144
    {
145
        $container = new Container();
146
147
        $container['testing'] = function (Container $c) {
148
            return true;
149
        };
150
151
        foreach ($container as $k => $v) {
152
            $this->assertSame($k, 'testing');
153
            $this->assertInternalType('callable', $v);
154
            $this->assertTrue($v($container));
155
        }
156
    }
157
}
158