Completed
Push — master ( b06472...80d940 )
by Ryan
10:54
created

ContainerTest::testSetterOk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
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
/**
16
 * @coversNothing
17
 */
18
class ContainerTest extends AbstractTestCase
19
{
20
    public function testConstruct(): void
21
    {
22
        $container1 = new Container();
23
        $this->assertSame(0, \count($container1));
24
25
        $container2 = new Container([]);
26
        $this->assertSame(0, \count($container2));
27
28
        $container3            = new Container();
29
        $container3['testing'] = static function (Container $c) {
30
            return true;
31
        };
32
        $this->assertSame(1, \count($container3));
33
    }
34
35
    public function testSetterOk(): void
36
    {
37
        $container = new Container();
38
39
        $container['testing'] = static function (Container $c) {
40
            return true;
41
        };
42
43
        $this->assertSame(1, \count($container));
44
    }
45
46
    public function testSetterNotOk(): void
47
    {
48
        $this->expectException(\SimplePie\Exception\ContainerException::class);
49
        $this->expectExceptionMessage('The container ID `testing` cannot be overwritten.');
50
51
        $container = new Container();
52
53
        $container['testing'] = static function (Container $c) {
54
            return true;
55
        };
56
        $container['testing'] = 'This is gonna fail.';
57
    }
58
59
    public function testSetterOk2(): void
60
    {
61
        $container = new Container();
62
63
        $container['testing'] = static function (Container $c) {
64
            return true;
65
        };
66
67
        unset($container['testing']);
68
69
        $container['testing'] = static function (Container $c) {
70
            return true;
71
        };
72
73
        $this->assertSame(1, \count($container));
74
    }
75
76
    public function testGetterNotOk(): void
77
    {
78
        $this->expectException(\SimplePie\Exception\NotFoundException::class);
79
        $this->expectExceptionMessage('The container ID `testing` does not exist.');
80
81
        $container = new Container();
82
        $get       = $container['testing'];
83
    }
84
85
    public function testGetterOk(): void
86
    {
87
        $container = new Container();
88
89
        $container['testing'] = static function (Container $c) {
90
            return true;
91
        };
92
93
        $this->assertTrue($container['testing']);
94
    }
95
96
    public function testGetterOk2(): void
97
    {
98
        $container = new Container();
99
100
        $container['testing'] = static function (Container $c) {
101
            return true;
102
        };
103
104
        $this->assertTrue($container->get('testing'));
105
    }
106
107
    public function testGetterNotOk2(): void
108
    {
109
        $this->expectException(\SimplePie\Exception\ContainerException::class);
110
        $this->expectExceptionMessage('The value `testing` MUST be a callable.');
111
112
        $container = new Container();
113
114
        $container['testing'] = true;
115
116
        $this->assertTrue($container['testing']);
117
    }
118
119
    public function testGetterNotOk3(): void
120
    {
121
        $this->expectException(\SimplePie\Exception\ContainerException::class);
122
        $this->expectExceptionMessage('The value `testing` MUST be a callable.');
123
124
        $container = new Container();
125
126
        $container['testing'] = 'testing';
127
128
        $this->assertTrue($container['testing']);
129
    }
130
131
    public function testHasOk(): void
132
    {
133
        $container = new Container();
134
135
        $container['testing'] = static function (Container $c) {
136
            return true;
137
        };
138
139
        $this->assertTrue($container->has('testing'));
140
    }
141
142
    public function testIterator(): void
143
    {
144
        $container = new Container();
145
146
        $container['testing'] = static function (Container $c) {
147
            return true;
148
        };
149
150
        foreach ($container as $k => $v) {
151
            $this->assertSame($k, 'testing');
152
            $this->assertInternalType('callable', $v);
153
            $this->assertTrue($v($container));
154
        }
155
    }
156
}
157